How to slow box2d body speed or angular speed

I have a dynamic circle-shaped body that mimics a bouncing ball, I set the restitution to 2, and it just gets out of control until it stops jumping up and down. So I want to slow down linear speed or angular speed with 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 speed of the ball reaches 80 or higher, I set its linear damping to 50, then it just goes in super slow motion. Can someone please explain to me how Damping works and how to use the .setLinearDamping() method .setLinearDamping() , thanks.

EDIT

This is what I did if the linear speed exceeded what I needed, sets the ball by linear damping to 20, if it does not always set it to 0.5f. This creates and leads to the fact that gravity changes constantly and instantly. However, @ minos23's answer is correct, because it mimics the ball more naturally, you just need to set MAX_VELOCITY what 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); 
+5
source share
2 answers

Here is what I do to limit my body speed:

 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 and it will limit the speed of the body of the ball you make

Good luck.

+2
source

Your code sets strong linear damping, but never releases it, so when the ball reaches a certain speed, it will switch to the state in which it behaves as if it is glued together.

I would prefer to limit the maximum speed by checking and resetting it, if necessary, in each frame:

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

How linear damping works

Linear attenuation mimics a phenomenon called drag when objects do not move too fast. This is mainly described by the following equation at every moment:

 dragInducedForce = -dragCoefficient * velocity; 
+1
source

Source: https://habr.com/ru/post/1239777/


All Articles