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

标签 javascript vector three.js

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

速度在其他地方发生改变,然后每帧添加到网格位置。

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

这是我尝试过的两种方法 - 使网格旋转,是的,但只是以错误的 Angular 和向后旋转等等。

//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 stackoverflow 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])。

另一种方法是四元数类:

.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();

关于javascript - Three.js - 将网格速度转换为网格旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175145/

相关文章:

java - Lucene TermFrequenciesVector

c++ - QTreeWidgetItem : How can I get the selected item?

node.js - 如何将 Amazon polly TTS 和语音标记添加到 MAYA 中构建的 3D 模型中?

javascript - 用户界面路由器 : How should you prevent toState from showing while you're checking authorization?

javascript - AngularJS 在具有隔离范围的指令中使用 ng-change - 转向基于组件的架构

c++ - 如何为std::vector调整大小以分配默认值

serialization - 在 ThreeJS 中序列化相机状态

javascript - onmouseover onmouseout 图像变化液体效果

javascript - 跨站脚本攻击,麻烦

javascript - 使用javascript的动态下拉按钮