1
\$\begingroup\$

I have written some jumping code for my player in a platformer game. At the moment it has some basic logic which says that the player can jump if he is on the ground. The pseudocode looks something like this:

### Input Handling ###
if(jumpButtonPressed){
    if(onGround){
        jump
    }
}

### Player Update Loop ###
onGround = false
movePlayer
resolveCollisions
if(player collided with ground){
      onGround = true
}

This works fine for basic static tile levels, but when the player is standing on a descending platform, or moving down a downward slope for instance, it becomes very difficult to jump. The reason for this (in the case of the descending platform) is that the platform has moved from underneath him in that frame, so for that moment he is not actually colliding with the ground, and therefore the condition to be able to jump is not fulfilled.

Is there a general way to add some leniency into the jump mechanic so that the player can jump even if they are not technically on the ground for that exact frame? This seems like it should be quite a common problem but I cannot seem to find any similar questions on stackexchange or google.

\$\endgroup\$
1
  • \$\begingroup\$ Sounds more like you need to clean up your collision detection with the player and the ground. Or make a separate slightly larger bounding box that acts as a collision detector for the player jump. \$\endgroup\$
    – House
    Oct 16, 2013 at 14:24

1 Answer 1

1
\$\begingroup\$

The best solution for this is largely going to be a product of tweaking and testing. If you've seen the documentary Indie Game, one of the designers/programmers of Super Meat Boy talks about all of the time he spent endlessly tweaking jumping and wall sliding, until it felt right.

There may not be a perfect solution, and moving platforms have long posed an issue for physics, but here is something you can try. Rather than using a boolean to keep track of whether you are touching the ground, try using a timer. Every time the player has a collision with the ground, reset the timer, and allow him to jump while the timer is non-zero. Jumping should kill the timer, obviously. The time should be very short, and it may allow characters to jump in the air just after running off a platform, which you may see as good or bad.

An alternative, depending on the implementation of gravity, is just tracking a frame count (like 2 or 3 frames). I don't like this as much because it's performance-dependent, but it could be that the character is actually standing on the descending platform only every-other frame, and this would resolve that. (collide & become still, then the platform drops and he has a collision-less fall).

You could, as a third solution, check to see how your physics deals with the character once he hits the platform. Namely, does it set his y-velocity to 0, or to the y-velocity of the object he had a collision with? If the answer is 0, adjusting that could be sufficient.

\$\endgroup\$
3
  • \$\begingroup\$ I've also heard an option for moving platforms is to have the platform move any objects standing on it at the same time as the platform moves. This would make your player think that it is always on the ground. This may not work in all implementations \$\endgroup\$ Oct 16, 2013 at 17:13
  • \$\begingroup\$ This sounds like it is asking for lots of other physics issues, but I suppose that depends on your physics implementation. In my own project, I built all the physics from the ground up (it's only 2D), and this solution wouldn't really fit, but I wouldn't be surprised if it was more compatible with others. I just prefer to find ways to avoid circumventing a consistent physics model. \$\endgroup\$
    – TASagent
    Oct 16, 2013 at 17:19
  • \$\begingroup\$ True, with a physics engine, the method I described won't really work as there are many extra variables involved. But if you are doing simple physics it should work just fine. \$\endgroup\$ Oct 16, 2013 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .