Player moving up, is he jumping or climbing?

In a 2D physics-based platformer game that has ladders in it, how do you determine whether the player moving up is caused by a jump or him climbing a ladder, such that you know what animation to play?

And in general, obviously the direction vector is not enought to determine the animation to play: how do you also determine the cause of the movement (so you know the correct sprite to use)?

• If a player is on a ladder, is he really in the air? Dec 29 '11 at 9:08

I have a personal logic issue on a regular basis that you seem to be expressing here. It's that sometimes you're looking at the logic and programming from the wrong direction entirely. In this case, you seem to not be realizing that from the beginning you know which is happening, because of the logic code. You should already know, programmatically speaking, whether he's on the ladder or not.

Think about it (as I said, I often have this problem). The games logic should know you're on the ladder, supposing that you're being a smart programmer. For a better explanation, I have a file archive type, and wasted about an hour of a day trying to figure out how to get the size of the data and I never once stopped to think that, when I load in the file, I now know its size :P.

The reason why this relates so exclusively, is that you're looking at it as "Now that I'm in the air, how do I figure out if I'm on a ladder or jumping?"... When you should really think "I'm about to get in the air, but by which method FIRST, so that I can use it for the animations use."

• Simple e.g.: OnJump() change your jumping boolean to true, and then if you're in the air and jumping is false, then you're either falling or on a ladder, etc. Dec 24 '11 at 23:44

The physics system neither knows nor cares why the player is moving in a particular direction. It is your game that caused the physics system to move the player that way, and it is therefore the responsibility of your game to keep track of that. The player entity should have some state on it that will tell if it is jumping, climbing, running, etc. You shouldn't be trying to figure this out based on what happened with the physics system.

Seems that what ever event that is triggered on the collision that sets the players vertical velocity could also be used to set a flag specifying what animation to use.

If you are using a 3rd party library that is too restrictive with what info it gives you it would be possible to surround the ladder with a rectangle and if the player moving up and contained in that rectangle then do the climbing animation.

• This actually brings up a good point I was going to address later into my own games development, and that is how to change the speed of an animation based on the physics. I have characters that I want to flail their arms procedurally, faster based on how fast they're falling. And I hadn't ever thought of a good way to do it in depth (was thinking over time), but you're velocity idea is much better. +1 for a general usable answer, and not being specific to only his case. Dec 25 '11 at 2:48

When setting the jump velocity also set the characters state to STATE_JUMPING. Doing this you always know in which state the player is in and can act accordingly by checking the players state.