gpt4 book ai didi

java - libgdx box2d body - 为什么 body 在到达接触点位置后摇晃?

转载 作者:行者123 更新时间:2023-11-30 08:35:25 28 4
gpt4 key购买 nike

问题

If the bullet reaches its destination the body is shaking, back and fort from destination to previous position then back again to destination and so on.. and so fort... Strange behavior

Visual

示例代码

Vector2 targetPosition =
// Copied target position and subtracted by bullet position
Vector2 targetDirection = targetPosition.cpy().sub(bulletPosition);

float distance = bulletPosition.dst(targetPosition);

float speed = 16;
Vector2 velocity = targetDirection
.cpy() // Copied target direction
.nor() // normalize to avoid getting the direction as speed
.scl(speed); // scaled by speed
// the distance is not accurate, so we get the time step as defined precision
float DEFINED_PRECISION = Constants.TIME_STEP;
// check if the bullet is near or maybe match the touch point
if(distance >= DEFINED_PRECISION) {
// move the bullet
body.setLinearVelocity(velocity);
} else {
// stop the bullet
body.setLinearVelocity(0,0);
}

最佳答案

可能是你的 DEFINED_PRECISION 太低了 - 你应该在每一步中注销 body 的 位置(甚至通过添加类似 System.out.println(body. getPosition()); 在你的循环中)并检查它是否更大。

当时的情况是

  1. body 在目标点之前并且它的距离大于DEFINED_PRECISION所以它正在向前移动
  2. body 在目标点之后,它的距离大于DEFINED_PRECISION,所以它正在向后
  3. 移动
  4. body 在目标点之前并且它的距离大于DEFINED_PRECISION...

这就是它颤抖的原因:)

首先,您应该更改您的DEFINED_PRECISION - 检查一帧中移动了多少 body ,这个值除以2应该是 DEFINED_PRECISION(因为两帧之间的 body 和目标之间存在最大距离)。另外我猜想将目标的位置直接设置为 body 比将 velocity 设置为 (0,0) 更好

    else {
body.setTransform(target.getPosition().x, target.getPosition().y, body.getAngle());
}

当然,如果你的步幅不是很大——那么变化将是不可见的,最终位置将恰好是目标的位置

关于java - libgdx box2d body - 为什么 body 在到达接触点位置后摇晃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38239532/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com