Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently using Box2D and my character is a bunch of fixtures in Box2D. I was wondering if, to determine if a player is falling, detecting a change in Y position would be appropriate?

Are there maybe other ways that I'm not thinking of? How is this sort of thing usually detected?

share|improve this question
2  
Wouldn't a player have a downward velocity to indicate falling? –  The Communist Duck Jun 16 '11 at 14:10
1  
What's "falling" in your case anyway? Is it a simple downward velocity? Is it a non-presence of a solid surface immediately beneath? Are you "falling" when your character jumps off a building and is still travelling upward, but is obviously going to hit the concrete a few seconds from now? –  Anko Jun 16 '11 at 14:23
    
@Aku faling is anytime it would not make sense for him to jump (since no surface to jump off of) –  Milo Jun 16 '11 at 14:30
3  
Normally a physics engine should give you ways to check the normal of a collision. If it is 'up' this means your player is standing on something, and should be able to jump. Inversely if he is not colliding, or if none of the collision normals are pointing 'up' then he is falling. –  Jonathan Connell Jun 16 '11 at 15:21
    
@Duck and @3nixios sounds like good answers, why not make it .. ehm answers ;) I, for one would upvote –  Maik Semder Jun 16 '11 at 15:59

6 Answers 6

up vote 9 down vote accepted

(From my comment.) I am not familiar with Box2D, but normally a physics engine should give you ways to check the normal of a collision.

If it is 'up' this means your player is standing on something, and should be able to jump.

Inversely if he is not colliding, or if none of the collision normals are pointing 'up' then he is falling.

share|improve this answer

Very naively and simply, you could check if the player has a y-velocity (velocity, not speed) which is > 0, assuming you're using (0,0) as top left.

share|improve this answer

The way I handle this is each player has an isJumping boolean which, when they jump, is set to true. When they collide with a surface, I check if it's something they could jump off of and other stipulations such as are they standing flat, etc. and set it to false.

Then, when they try to jump, if the surface they're on is good and isJumping == false I allow them to jump, otherwise they can't jump. I think this is better than calculating collisions every time you want to see if they're jumping.

Edit
As @BlueRaja pointed out below, if the user walks off the edge, isJumping will still be false. You can fix this with the Contact Listener's EndContact method (see sect 9.4 Box2D docs. You just need to set the isJumping property to true when the collision with the world ceases.

share|improve this answer
2  
What if they walk off the edge without jumping? isJumping will be false, but they still shouldn't be allowed to jump –  BlueRaja - Danny Pflughoeft Jun 16 '11 at 16:34
    
@BlueRaja - Good point. I forgot about that, I also have a check on breaking collisions. See EndContact in 9.4 from the Box2d Docs. Edit Updated answer. –  Richard Marskell - Drackir Jun 16 '11 at 19:28

You need two booleans, isColidingWithGround and isJumping

When the user presses the jump button, if isColidingWithGround is true, and isJumping is false, then call jump() which sets isJumping to true

If the player's y velocity is positive then isJumping equals false

If the player is coliding with the ground, then isColidingWithGround equals true, else isColidingWithGround equals false.

Sometimes the player is still in contact with the ground after the initial jump impulse, and I find that with using these two flags, it prevents jump() from being called multiple times when jump is pressed.

share|improve this answer
    
Using this method, would be interesting to check if the jump key was pressed when the player was falling, and if it was pressed like 200ms before touching the ground, call jump() again. –  JCM Jul 25 '13 at 18:43

This may or may not apply, but we would detect a difference between jumping/landing and falling so we knew when to swap to a falling animation, do damage for falling too long (not too far ;)) and the like.

It was pretty much done by tracking how long a object was without a reference point. Anything on the ground, or any other object, had a reference to what it was standing on.. Not standing on anything? Then you were in the air, and after 1-2 seconds, you were 'falling' instead of gracefully landing :)

share|improve this answer

Move all relevant components in your "bunch of fixtures" (which together make the player) downward by a small amount, and check for collisions with anything you consider possible to jump off of. If there is no collision, the player is falling. Otherwise, he isn't. Don't forget to move the components back up by that same amount.

If your character is humanoid, you should probably move and check only the feet of the character, as otherwise your character may end up being able to jump even when standing on his head!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.