In my game, I make the player jump by applying a force. Unfortunately, this leads to the jump button being able to be pressed even when the player is in the air. Is there a way to determine if the body is touching the ground (and not rubbing on a side or something) ?
Thanks!
You can use collision callbacks to detect a collision with fixtures that you previously tagged (user data) as ground.
You can attach a small sensor fixture under the player to detect when something is under them.
You can look at the normals in the manifold data in the contact to determine if the other fixture is under the player (not recommended).
Also, you can do something like this too, to find out what shapes are at a certain point:
(i.e. define a point where the player's feet would be )
var maxShapes:int = 10;
var testAABB = new AABB();
testAABB.upperBound = player x, player y
testAABB.lowerBound = player.x + 0.001, player y + 0.001;
Shapes[] shapesUnderCharacter = world.queryAABB( AABB, maxShapes )
for each ( Shape s in shapesUnderCharacter ){
if ( s.getBody() == groundBody ){
grounded = true;
}
}
Basically what you're doing is making a tiny tiny bounding box, and
seeing what shapes overlap it.
I guess that would work if your game has no slopes the player can run up/down, and he is standing still.