1
\$\begingroup\$

I have a game with some ball and I want to make it bounce. I already have movement, gravity, collisions implemented, I only need to make it bounce. Thing is, using this code:

player.getVelocity().y *= Player.ENERGY_LOSS;
player.getVelocity().y = -player.getVelocity().y;

indeed it makes the ball bounce but obviously the .y will get to very small values causing the ball to 'vibrate' and never stop.

How can I 'clamp' this value? Let's say I just want the ball to bounce just 2 - 3 times?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Most physics engines have a minimum velocity at which point it is snapped to zero and moved to a "not moving" set of objects, for example 1e-5 m/s. Obviously the value will depend on the scale of your game.

For example:

if(player.getVelocity().magnitude() < 1.0e-5) player.setVelocity(float3(0,0,0));
\$\endgroup\$
1
  • \$\begingroup\$ I'm not using a physics engine since I only need to move left, right + jump. I'll try something based on what you said. \$\endgroup\$
    – erasmus77
    Commented Oct 26, 2014 at 8:49
0
\$\begingroup\$

I solved the problem by 'rounding' player.getVelocity.y value

player.getVelocity().y *= Player.ENERGY_LOSS;
player.getVelocity().y = -player.getVelocity().y;
player.getVelocity().y = (float) Math.round(player.getVelocity().y);

so now, depending on the height the ball falls it bounces from as low as 0 times to 2 - 3 - 4 depending on the height. Of course it no longer vibrates because player.getVelocity().y no longer gets to really low values and istead when it's low enough it gets rounded straight to 0 (stop).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .