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.

When a player touches a ground peice in a game, he seems to sink through the ground then slowly rise back up, and when he has risen back up, he is very jittery.

 if (touchingTheGround())
 {
     player.velocity.Y -= 2;
     touchground = false;
 }

the collision method sets the variable touchground to true if the player touches a ground peice, and if i dont set it to false, the collision detection wont work at all. i set the players velocity to move 2 up because when he's not touching a platform, his velocity.Y += 1. How do i achieve a way so that the player

  1. Does not sink in the ground before moving up
  2. Stops jittering when its position is higher than the ground its touching
share|improve this question
3  
The problem is that the value "2" drags the y components value to get past the 0 value. So you're first slowing down if it's in a plus value. 2 is a constant that doesn't really care for how much you've been penetrating the tile. Neither does it care from which side you're coming from. You are also assuming that after the velocity is changed, you're no longer touching the tile. You're missing a few steps. –  Sidar Mar 16 '13 at 15:12
    
As Sidar rightly mentions, you will need to calculate this above the ground. –  Jason Coombes Mar 18 '13 at 6:08

1 Answer 1

Think about these cases:

  1. The player is in the air
    • we want them to fall (increase velocity.Y)
  2. The player is "inside" the ground
    • we want them to instantly stop falling (set velocity.Y to zero)
    • we want them to instantly move up onto the surface of the ground (set position.Y to the correct value)

Here is one idea for fixing the code you showed to make it work for case 2

if (touchingTheGround())
 {
     player.velocity.Y = 0; //stop falling
     while (isPlayerStillTouchingTheGround()) { //Need to re-do the collision check
        player.position.Y -= 1; //move until they are no longer inside the ground
     }
     touchground = false;
 }

First we stop the player from falling. Second, we move the player up until they are not in the ground any more. This all happens in the single frame, so from the player's point of view, it is instant.

Note that you have to be able to repeat the collision check over and over in one frame, so we can tell when we have moved the player far enough to be back above the ground.

Repeating the collision check is inefficient - it would be better if you could get the position of the top of the ground, and move the player there in one step.

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.