Title: Box2D - how to prevent continuous jumping while next to a wall
Tags: java;android;game-physics;libgdx
Question: Greetings stackoverflow community!
I am working on an Android game that uses Box2D for collision detection and physics. Right now, I'm implementing a basic platformer system. Jumping is in place, but it has a small glitch. The following function is meant to determine if a player is on the ground. Simple, right? Well, it works okay, unless the player is right next to a wall, in which case it'll just let him constantly jump up, as the function will still return true, even if the wall isn't directly below the player.
I have tried adding additional x-checks to make sure that I'm colliding with something that's directly under me, but to no avail.
private boolean isGrounded(float deltaTime) {
List<Contact&gt; contactList = world.getContactList();
for(int i = 0; i < contactList.size(); i++) {
Contact contact = contactList.get(i);
if(contact.isTouching() &amp;&amp; (contact.getFixtureA() == sensorFixture ||
contact.getFixtureB() == sensorFixture)) {
Vector2 pos = body.getPosition();
WorldManifold manifold = contact.getWorldManifold();
boolean below = true;
Vector2 cpoint;
for(int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
cpoint = manifold.getPoints()[j];
below &amp;= (
(cpoint.y < pos.y - 0.15f)
&amp;&amp; (cpoint.x &gt; pos.x - w/2 + 3f)
&amp;&amp; (cpoint.x < pos.x + w/2 - 3f)
);
if(below) return true;
}
return false;
}
return false;
So, does anyone have any pointers towards what I could do to make the isGrounded check more efficient?
Thanks in advance! :)

Here is the accepted answer: I'm not familiar with Box2d, but my first thought would be to check the orientation of the object you are in contact with. A quick check of the documentation will tell you that WorldManifold has a getNormal which provides a local normal that points from fixtureA to fixtureB. From there you compare the orientation to the X-axis or Y-axis using a dot product. Then you can threshold how steep a surface you want to let the player be able to jump on. You may need to convert the local normal to world space.
Sources:
Box2d Manual
LibGDX Docs
Comment for this answer: Thanks! :D It seems that this simple bit of code does the trick: `if(c.getWorldManifold().getNormal().y > 0)
return true;` Ah, and a foot-sensor-based approach is what I had before (an alternate implementation since this one wasn't working) functioned great too! I'll test both systems and if there's any significant difference between them, I'll post it here. Thanks again!


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:12/11/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android><game-physics><libgdx>', 'Title': 'Box2D - how to prevent continuous jumping while next to a wall', 'ViewCount': 1138, 'id': 8467069, 'user_AboutMe': '<p>I mostly deal with petty webapps and drunken bugs. Been too long since I\'ve had a good game dev project.</p>\n\n<p>In my spare time, I\'m either on Coursera (doing courses or working as a Community TA) or working on side projects, such as <a href="https://github.com/AndreiBarsan/uShell" rel="nofollow noreferrer">µShell</a> or <a href="https://github.com/AndreiBarsan/Yeti" rel="nofollow noreferrer">Yeti</a>.</p>\n', 'user_AccountId': 1051458.0, 'user_DisplayName': 'Andrei Bârsan', 'user_Reputation': 3444.0, 'user_Views': 685.0, 'user_id': 1055295.0}}