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 am implementing a simple side scrolling platform game. I am using the following strategy while coding the physics:

  • Gravity constantly acts on the character.
  • When the character is touching the floor, a normal reaction is exerted by the floor.

I face the following problem:

If the character is initially at a height, he acquires velocity in the -Y direction. Thus, when he hits the floor, he falls through even though normal force is being exerted. I could fix this by setting the Y velocity to 0, and placing him above the floor if he has collided with it. But this often leads to the character getting stuck in the floor or bouncing around it. Is there a better approach ?

share|improve this question
    
Can't say I know the right answer, but I've heard that most people will take the character and cast a ray downward into the terrain to detect if the player is on top or near enough and then set the players height to match the terrain. But again I just heard that and never implemented it (that is why this is a comment and not an answer) –  Benjamin Danger Johnson Oct 29 '12 at 16:51
add comment

1 Answer 1

up vote 3 down vote accepted

Your first problem is that you do not want the floor to be exerting a literal force on the character. If you are using the term 'force' in its true sense, that means that it will change velocity over time, which is not what you want when your character hits the ground (or when any collision occurs, for that matter). What you are actually looking for is an impulse, or an immediate change in velocity.

For reference, I recommend some articles written by Chris Hecker.

Your proposed fix of setting the y-velocity to 0 and adjusting the position above the ground is certainly a valid solution, since that would be an implementation of an impulse. For the general case (referring to Hecker's articles) you would multiply by the coefficient of restitution to calculate the new y-velocity. The problems you are describing indicate that there might be something else wrong with your code.

The other problem you might be facing is the contact problem: when one body is only contacting another, and not actually colliding or intersecting with it. The biggest problems arise when stacking objects on top of each other. This is actually difficult to solve, but alternate timestep schemes such as the one proposed in this paper open the door for solutions. I'm afraid I may be going on the scope of the question though so I'll stop there...

To sum up, I think you have the correct fix, and there is another bug in your code. It could be the order in which you carry out the collision response operations.

share|improve this answer
add comment

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.