【问题标题】:How to slow down box2d body linear or angular velocity如何减慢box2d体的线速度或角速度
【发布时间】:2016-01-04 10:59:00
【问题描述】:

我有一个模拟弹跳球的圆形动态体,我将恢复原状设置为 2,它只是失控到它不会停止上下弹跳的程度。所以我想使用 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);

当球的线速度达到 80 或以上时,我将其线性阻尼设置为 50,然后它就会进入超慢动作。谁能解释一下阻尼的工作原理以及如何正确使用.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);

【问题讨论】:

  • 请告诉我们提供的解决方案是否对您有用,如果有任何问题发表评论,祝您好运

标签: java libgdx box2d velocity


【解决方案1】:

这是我通常用来限制身体速度的方法:

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

祝你好运

【讨论】:

    【解决方案2】:

    您的代码设置了强大的线性阻尼,但从不释放它,因此当球达到特定速度时,它会切换到它的行为就像它被粘住一样的状态。

    我宁愿通过在每一帧中检查和重置它来限制最大速度:

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

    线性阻尼的工作原理

    当物体移动不太快时,线性阻尼模仿称为drag 的现象。基本上每时每刻都用下面的等式来描述:

    dragInducedForce = -dragCoefficient * velocity;
    

    【讨论】:

    • 谢谢我试过你的代码,但球的线速度仍然超过 maxLenght2
    • 每帧都执行sn-p吗?
    • 对不起,我是新手,sn-p怎么执行?
    • @StormAsdg 将您问题中的代码替换为我的答案中的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多