java – 如何减慢box2d体的线性或角速度

前端之家收集整理的这篇文章主要介绍了java – 如何减慢box2d体的线性或角速度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个圆形动态的身体,模拟一个弹跳球,我将恢复原状设置为2,它只是失去控制,它不会停止上下弹跳.所以我想使用阻尼减慢球的线性或角速度.@H_301_2@

@H_301_2@

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()方法,谢谢.@H_301_2@

编辑@H_301_2@

这就是我所做的,如果线速度超出了我的需要,它将球线性阻尼设置为20,如果不总是设置为0.5f.这会产生并影响重力不断变化的瞬间变化.但是@minos23的答案是正确的,因为它更自然地模拟球,你只需要设置你需要的MAX_VELOCITY.@H_301_2@

@H_301_2@

 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);
最佳答案
这是我用来限制身体速度的方法:@H_301_2@

@H_301_2@

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()方法中尝试此代码,它将限制您正在制作的球体的速度@H_301_2@

祝好运@H_301_2@

原文链接:https://www.f2er.com/java/437247.html

猜你在找的Java相关文章