如何减慢 box2d body 线性或 angular 速度
How to slow down box2d body linear or angular velocity
我有一个模拟弹跳球的圆形动态 body,我将恢复原状设置为 2,它就失去了控制,无法停止上下弹跳。所以我想使用阻尼减慢球的线性或 angular 速度。
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);
当球的线速度达到80或以上时,我将其线性阻尼设置为50,然后它就会超级慢动作。谁能解释一下 Damping 是如何工作的以及如何正确使用 .setLinearDamping()
方法,谢谢。
编辑
这就是我所做的,如果线速度超过我需要的,它会将球线性阻尼设置为 20,如果不总是将其设置为 0.5f。这会产生并影响重力不断且瞬间变化的效果。然而@minos23 的答案是正确的,因为它更自然地模拟球,你只需要设置你需要的 MAX_VELOCITY。
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);
你的代码设置了很强的线性阻尼,但从不释放它,所以当球达到特定速度时,它会切换到一种状态,在该状态下它的行为就像粘在了一起。
我宁愿通过检查并在必要时在每一帧中重置它来限制最大速度:
float maxLength2 = 80*80;
Vector2 v = ball.getLinearVelocity();
if (v.len2() > maxLength2) {
v.setLength2(maxLength2);
}
线性阻尼的工作原理
线性阻尼模仿物体移动不太快时称为drag的现象。基本上每时每刻都用下面的等式来描述:
dragInducedForce = -dragCoefficient * velocity;
这是我用来限制 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);
请在 render() 方法中尝试此代码,它会限制您制作的球 body 的速度
祝你好运
我有一个模拟弹跳球的圆形动态 body,我将恢复原状设置为 2,它就失去了控制,无法停止上下弹跳。所以我想使用阻尼减慢球的线性或 angular 速度。
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);
当球的线速度达到80或以上时,我将其线性阻尼设置为50,然后它就会超级慢动作。谁能解释一下 Damping 是如何工作的以及如何正确使用 .setLinearDamping()
方法,谢谢。
编辑
这就是我所做的,如果线速度超过我需要的,它会将球线性阻尼设置为 20,如果不总是将其设置为 0.5f。这会产生并影响重力不断且瞬间变化的效果。然而@minos23 的答案是正确的,因为它更自然地模拟球,你只需要设置你需要的 MAX_VELOCITY。
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);
你的代码设置了很强的线性阻尼,但从不释放它,所以当球达到特定速度时,它会切换到一种状态,在该状态下它的行为就像粘在了一起。
我宁愿通过检查并在必要时在每一帧中重置它来限制最大速度:
float maxLength2 = 80*80;
Vector2 v = ball.getLinearVelocity();
if (v.len2() > maxLength2) {
v.setLength2(maxLength2);
}
线性阻尼的工作原理
线性阻尼模仿物体移动不太快时称为drag的现象。基本上每时每刻都用下面的等式来描述:
dragInducedForce = -dragCoefficient * velocity;
这是我用来限制 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);
请在 render() 方法中尝试此代码,它会限制您制作的球 body 的速度
祝你好运