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.

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;


    if (gamepadState.IsButtonDown(Buttons.A))
    {
        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

share|improve this question
5  
Why do you set the moveY 2 times, then subtract one? –  Byte56 Feb 26 '13 at 23:25

2 Answers 2

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.

share|improve this answer

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).

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.