Three.js - 将网格速度转换为网格旋转

Three.js - Convert mesh velocity to mesh rotation

假设我有一个 three.js 网格和一个速度矢量3。

在别处改变速度,然后添加到 mesh.position 每帧。

我想要的是 mesh.rotation 与速度相对应,即网格是一个箭头,始终指向它前进的方向。

这是我尝试过的两种方法 - 让网格旋转是的,但只是角度和向后的角度不对等等。

//this doesn't work
mesh.rot.x = Math.atan2( vel.y, vel.z );
mesh.rot.y = -Math.atan2( vel.x, vel.z );
mesh.rot.z = Math.atan2( vel.x, vel.y );

//got this from a semi-related Whosebug question; also doesn't work
mesh.rot.y = Math.asin( vel.z );
mesh.rot.z = Math.atan2( vel.y, vel.x );

var bankVec = new Vector3( -vel.y, vel.x, 0 );
var upVec = new Vector3( dot( bankVec, vel ) );

mesh.rot.x = Math.atan2(dot(bankVec, vel) / dot(upVec, vel),abs(bankVec)*abs(upVec));

我不太懂矢量数学。

我现在正处于任意更改符号和交换参数的阶段,希望最好。因此,我将不胜感激任何关于我应该如何做的解释。

不是真的在找代码,只是在寻找点积、叉积、atan2 等的相关方程。

您可以从归一化速度矢量构造旋转矩阵。归一化的向量是 1 个轴。然后你以某种方式选择第二轴。然后你做一个叉积来找到第三个轴。最后在第一个和第三个轴之间进行叉积以获得最终的第二个轴。

然后你从这 3 个向量构造一个 3x3 矩阵(我现在不记得它们的顺序,但类似于 [x1,y1,z1,x2,y2,z2,x3,y3,z3]) .

另一种方法是四元数class:

.setFromUnitVectors ( vFrom, vTo )

Sets this quaternion to the rotation required to rotate direction vector vFrom to direction vector vTo. Adapted from http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors. vFrom and vTo are assumed to be normalized.

http://threejs.org/docs/#Reference/Math/Quaternion

由于内部three.js喜欢使用四元数,这可能是一个很好的方法。如果你的箭头是向上的箭头,你可能需要 vFrom=[0,1,0], vTo=velocityVector.normalize();