我很难想办法解决这个问题。我想要做的是,用速度把一个物理物体移向另一个物体,直到它到达并撞击到另一个物体。(想想AI跟在玩家后面)
一种解决办法是:
body.velocity.x = target.position.x-body.position.x;
body.velocity.z = target.position.z-body.position.z;不过,这也有问题。一种是速度更高,取决于两个物体之间的距离。我更喜欢固定的速度。另一种方法是使用以下示例:Position a body in cannon.js relative to local rotation
但是在这种情况下,我需要一种旋转四元数的方法,所以它面对目标位置,这只知道物体的两个位置。
所以问题是,我如何计算物体的速度或方向,这样它就可以跟随并与目标物体碰撞,用一个固定的速度/速度?
发布于 2018-04-01 15:12:08
要得到一个固定的速度,.normalize()你的速度矢量,然后规模(.mult())的结果,你想要的速度。结果将是一个始终具有所需长度的向量。
要获得一个四元数,使你的身体在一个特定的方向,你可以使用Quaternion.setFromVectors(u,v)。该方法创建一个四元数,它将旋转u,因此它指向与v相同的方向。如果将u设置为正向向量,将v设置为希望身体查看的方向,则将得到正确的“查看”行为。请注意,“前进”向量可能与您的游戏不同。
// Compute direction to target
var direction = new CANNON.Vec3();
target.position.vsub(body.position, direction);
direction.y = 0;
direction.normalize();
// Get the rotation between the forward vector and the direction vector
var forward = new CANNON.Vec3(0,0,1);
body.quaternion.setFromVectors(forward, direction);
// Multiply direction by 10 and store in body.velocity
var fixedSpeed = 10;
direction.mult(fixedSpeed,body.velocity);https://stackoverflow.com/questions/49565413
复制相似问题