简体   繁体   中英

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

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

Your code sets up a strong linear damping, but never releases it, so when the ball reaches a specific speed, it will switch to a state in which it behaves like it's glued.

I would rather limit the maximum speed by checking and resetting it if necessary in every frame:

float maxLength2 = 80*80;
Vector2 v = ball.getLinearVelocity();
if (v.len2() > maxLength2) {
    v.setLength2(maxLength2);
}

How linear damping works

Linear damping imitates the phenomenon called drag when the objects moves not too fast. It is basically described by the following equation in every moment:

dragInducedForce = -dragCoefficient * velocity;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM