Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slow down box2d body linear or angular velocity

I have a circle shape dynamic body that simulates a bouncing ball, I set the restitution to 2 and it just gets out of control to the point it does't stop from bouncing up and down. So I want to slow down the ball linear or angular velocity using Damping.

if(ball.getLinearVelocity().x >= 80 || ball.getLinearVelocity().y >= 80)
            ball.setLinearDamping(50)
else if(ball.getLinearVelocity().x <= -80 || ball.getLinearVelocity().y <=-80)
            ball.setLinearDamping(50);

When the linear velocity of the ball reaches 80 or above I set its linear Damping to 50 then it just goes super slow motion. Can someone please explain me how Damping works and how to use .setLinearDamping() method properly, thanks.

EDIT

This is what I did, if the linear velocity exceeded from what I need it sets the ball linear Damping to 20 if not always set it to 0.5f. This creates and effect that the gravity changes constantly and instantly. However @minos23 answer is correct because it simulates the ball more naturally you just need to set the MAX_VELOCITY that you need.

 if(ball.getLinearVelocity().y >= 30 || ball.getLinearVelocity().y <= -30)
            ball.setLinearDamping(20);
        else if(ball.getLinearVelocity().x >= 30 || ball.getLinearVelocity().x <= -30)
            ball.setLinearDamping(20);
        else
            ball.setLinearDamping(0.5f);
like image 978
Kevin Bryan Avatar asked Jan 04 '16 10:01

Kevin Bryan


People also ask

What is the correct transformation between linear velocity and angular velocity?

From the knowledge of circular motion, we can say that the magnitude of the linear velocity of a particle travelling in a circle relates to the angular velocity of the particle ω by the relation υ/ω= r, where r denotes the radius. At any instant, the relation v/ r = ω applies to every particle that has a rigid body.


1 Answers

here is what i uselly do to limit the velocity of the body :

if(ball.getLinearVelocity().x >= MAX_VELOCITY)
      ball.setLinearVelocity(MAX_VELOCITY,ball.getLinearVelocity().y)
if(ball.getLinearVelocity().x <= -MAX_VELOCITY)
      ball.setLinearVelocity(-MAX_VELOCITY,ball.getLinearVelocity().y);
if(ball.getLinearVelocity().y >= MAX_VELOCITY)
      ball.setLinearVelocity(ball.getLinearVelocity().x,MAX_VELOCITY)
if(ball.getLinearVelocity().y <= -MAX_VELOCITY)
      ball.setLinearVelocity(ball.getLinearVelocity().x,-MAX_VELOCITY);

try this code inside the render() method please and it will limit the speed of the ball body that you're making

Good luck

like image 51
Netero Avatar answered Nov 10 '22 15:11

Netero