加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 01:04:42 所属栏目:Java 来源:网络整理
导读:我有一个圆形动态的身体,模拟一个弹跳球,我将恢复原状设置为2,它只是失去控制,它不会停止上下弹跳.所以我想使用阻尼减慢球的线性或角速度. if(ball.getLinearVelocity().x = 80 || ball.getLinearVelocity().y = 80) ball.setLinearDamping(50)else if(ball.

我有一个圆形动态的身体,模拟一个弹跳球,我将恢复原状设置为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()方法,谢谢.

编辑

这就是我所做的,如果线速度超出了我的需要,它将球线性阻尼设置为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);
最佳答案
这是我用来限制身体速度的方法:

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

祝好运

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读