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.

Alright, so I've got hold of some simple physics mechanics, and am currently trying to implement bouncing. Based on the first answer of this question, I've developed the following algorithm:

    Vector2 velocity;

    float gravity;
    float elasticity;

    Hitbox dummy;

    // ---

    void bounce() {     
        velocity.Y += gravity;

        if (dummy.ThereWasContact.South) velocity.Y *= -elasticity;

Gravity can be any value, and Elasticity is always between 0.0f and 1.0f. Velocity.Y's initial value can also be anything.

Weight factors aside, which would determine a minimal rebound force for the object to be able to leave the ground again and (probably) aren't related to the problem, this is apparently fallacious.

What happens here is somewhat like this:

enter image description here

If I drop an object from a height of 128 units, for example, 0u marking ground height, with a gravity of 1u and elasticity of 0.9, the object would bounce once, reach a maximum height of ~126u from the rebound, and from then on keep alternating on maximum heights between this and ~122u.

This example is based on one of my own experiments, in which I drop an object from a height of 64 pixels and there is a floor at 192 pixels. The maximum height will keep going from ~66 pixels to ~70.

What am I missing? Does weight after all do more than I think? If I need to explain something better or details appear to be missing, let me know.

share|improve this question
Each bounce needs to subtract some small amount of energy from the ball. This accounts for the internal friction of the flexing of the ball and the sound and heat generated from the bounce. –  Byte56 Jan 31 '13 at 20:58

2 Answers 2

up vote 3 down vote accepted

Whenever the ball hit the ground, first you add gravity, then you invert the direction, thus the ball is going to spring back with greater speed. Let's step through a case where elasticity is 1, gravity is 1 and the ball's initial speed is 10:

  1. The ball moves 10 and hit the ground.
  2. The speed is increased to 11.
  3. The ball's speed is changed to -11.

Next update:

  1. The ball moves -11.
  2. The speed is decreased to -10.

So the ball impacted the ground with speed 10 and left with speed -11, next time it hit the ground it will be at speed 11, and it will leave at speed -12 etc.

A very simple fix is to split gravity application into two, half before collision detection and half after.

The future

Your next error will be that the ball get stuck in the ground. This happens because you let the ball pass into the ground before registering a collision, and sometimes the ball will have lost so much speed that the following move doesn't take it out of the ground, so your collision detection invert its direction again. The easy fix is to check that the ball is actually moving towards the object that it is colliding with before inverting the direction.

share|improve this answer
Sorry to not include such detail, but I'm not letting the ball pass into the ground. The Hitbox's MoveY() method resolves the collision - if it hits Point B which is between its starting position Point A and destination Point C, it will be sent back to Point B. –  Mutoh Jan 31 '13 at 21:47
With this aside --- Should I, then, add Gravity/2 before the collision, and Gravity/2 after the collision? –  Mutoh Jan 31 '13 at 21:47
Yep, that is it. –  eBusiness Jan 31 '13 at 21:58
Of course, ideally you would calculate an amount of bounceback in the same frame that the ball hit the ground and apply gravity based on how much of that frame was spent going up and down respectively. –  eBusiness Jan 31 '13 at 22:05
If anyone else is struggling to get it right, I wrote a pseudocode implementation (with the operations in the right order) here. –  Anko Jul 19 '13 at 8:46

Your 'elasticity' should be removing energy from the system so that it settles down. The first thing that comes to mind given that that isn't working, and that you mention measuring in pixels, is: make sure that your velocity is floating-point, not an integer.

If that isn't the problem, then the first thing I would do to debug is calculate the energy (ignoring mass) of the ball: pow(velocity.Length(), 2) + position.Y * gravity. If your units are right and your simulation is good, this should slowly decrease with each bounce. If it doesn't, then the bounce is a problem. If it increases continuously while not bouncing, then your basic simulation step is wrong.

share|improve this answer
Alright then, I will try this out (and yes, the position is measured with floats). Pardon my ignorance, though, but how would I calculate the magnitude of the velocity? –  Mutoh Jan 31 '13 at 21:25
@Mutoh Your Vector2 type should have an operation to do that (might be called length instead); if it doesn't, get a better one or write one. Any basic "vectors for computer graphics" reference should have all the details if you really need them. –  Kevin Reid Jan 31 '13 at 21:48
Indeed, it has a Length() method. Thank you. –  Mutoh Jan 31 '13 at 21:52

Your Answer


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.