# Making The Player Stop Bouncing And Stop Sinking In The Ground In A Platformer XNA

Heres a problem i've been having. I have a platformer, and whenever my player hits the ground he keeps bounding. Here's the falling code

iceBoy.moveY++;
iceBoy.position.Y += moveY;


Now heres the collision code

if (HitGround())
{

iceBoy.moveY = -4;

{
iceBoy.moveY = -25;
}

touchGround = false;
}


i tried lowering the iceBoy.moveY = -4, or iceBoy.moveY -= 1, but then ice boy just falls through the ground never to be seen again. How to i make it so he stops bouncing and without him falling staright through a block?

UPDATE 1: At just -4, he bounces little enough too make it look kind of good, but he sinks through the ground first then floats back up and it looks really bad. How can i fix this easily, with just a few lines of code

• Why do you set the moveY 2 times, then subtract one? – MichaelHouse Feb 26 '13 at 23:25

Correlating the bounce to the fall speed will help you. When you detect a collision and need to bounce, try setting the bounce this way:

iceBoy.moveY = -(iceBoy.moveY * .7);
if(abs(iceBoy.moveY) < BOUNCE_MIN_VALUE)
iceBoy.moveY = 0;


Assuming the moveY value is positive from iceBoy's decent (you may want to add some checks for that), this code will reduce the bounce speed every time. So there won't be as much time to build up speed on the way back down, so the next time the reduced bounce is even smaller and so on. You'll want to cut this off at some point so you're not bouncing forever.

More complicated would be to implement a real bounce. Either using a physics engine, or extending your simple implementation. Then make sure to add drag to the equation so that the bouncing settles.

Find out more about implementing that here. (Apply a lot of drag if you want to come to a stop quickly).

It looks like you have 2 variables with the same name. iceBoy.moveY which you set in the HitGround() routine and a local variable called simply moveY which is what actually is getting added to the position of your object and never gets changed in the HitGround() routine.