# Making a Box2D ball bounce to constant height

I want to create a game like Doodle Jump. I'm using libgdx and Box2d. I needadvice on how to make my ball bounce constantly and with the same height.

I've tried setting the ball's body restitution to 1 so it can jump constantly, but when I jump on a block the "impulse" is smaller. I also tried to detect the collision, then apply an impulse but it didn't work either.

How can I make the ball bounce to the same height every time?

• You may want to consider rolling out your own "physics" instead of using box2d. A game like doodle jump wouldn't require too much work to accomplish without a physics engine. Feb 28 '13 at 17:57

When the ball (red trajectory curve) hits a block higher up (blue rectangle), it will be moving slower than when it hit a block lower down (green rectangle). Hence why it doesn't bounce any higher than where it came from.

Physically, this makes sense.

Doodle Jump obviously isn't physically accurate. If you want an effect like in Doodle Jump where the impulse from a collision is always the same, you should set the vertical velocity to some value yourself whenever collisions happen, with ball.speed.y = 7; or similar.

Box2D worlds use damping and friction, consider that the world may be applying a damping to your objects as they are simulated.

I'd also like to throw in to the mix that it may not be necessary to use a physics engine for this. Depending on how your game collisions are handled (specifically, the collision response) you may find you can create a very simply bounding box system over which you have complete control. In fact, in "doodle jump" style games it's even easier. Rather than worrying about detecting collisions in box2d and working out whether the collision should be disabled or not (e.g. you hit a platform from underneath), the doodle jump scenario actually a lot simpler. If your ball is travelling "down" (y is decreasing) and it passes the y value of a platform, and it's within the left/right bounds of a platform, then it collides. The only really "complex" thing is the parabolic flight path you'd take after, but that's just a tiny bit of math (http://en.wikipedia.org/wiki/Parabola#Equation_in_Cartesian_coordinates).

Collisions with other items in the world that don't require a response (e.g. hitting a bonus) can be easily solved with just bounding boxes.

Box2D is phenomenal, but often overkill if you have simple requirements.. IMO.

Remember (float value = velocity.y) velocity of the ball before it collides with platform, and set after bounce: ball.velocity.y = value * (-1)

Maybe you will be interesting to look in this posts:
first post
second post

P.S. I can't submit code here, please look in "first post" in last comment. There is code how I resolved similar issue.