Knowing if a body is touching the ground?

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've decided not to go for a complicated way (though probably a lot better 😉) like Nopstick showed, but rather just check the Y-velocity. There's a minimal chance that you will be able to double-jump when at the peak of your jump, but after 10 minutes of holding the jump button, I decided it

NEVER.
HAPPENS.

Code:
//(c++)
//class variable
bool isMidair;

//update function
if ( body->GetLinearVelocity().y != 0 ) {
    isMidAir = true;
} else {
    isMidAir = false;
}
I guess that would work if your game has no slopes the player can run up/down, and he is standing still.
irresistible force wrote:
I guess that would work if your game has no slopes the player can run up/down, and he is standing still.


You are ever so correct - I'm currently doing the approach nopstick posted.

Original thread: https://box2d.org/forum/viewtopic.php?t=7141