如何在 THREE.js 中找到两个向量之间的旋转矩阵 [英] How to find rotation matrix between two vectors in THREE.js
问题描述
我正在寻找在 THREE.js 中的两个定义向量之间找到旋转矩阵的方法.
I'm looking for the way to find rotation matrix between two defined vectors in THREE.js.
例如
v1 = new THREE.Vector3(1, 1, 1)
v2 = new THREE.Vector3(1, 1, -1)
我需要这个旋转矩阵在下一步中旋转整个对象.
I need this rotation matrix to rotate whole object in next step.
推荐答案
您可以像这样定义两个单位长度向量 v1
和 v2
的旋转:>
You can define a rotation from two unit-length vectors v1
and v2
like so:
var quaternion = new THREE.Quaternion(); // create one and reuse it
quaternion.setFromUnitVectors( v1, v2 );
就您而言,您需要先对向量进行归一化.
In your case, you need to normalize your vectors first.
然后,您可以使用以下模式将该旋转应用于对象:
You can then apply that rotation to an object using the following pattern:
var matrix = new THREE.Matrix4(); // create one and reuse it
matrix.makeRotationFromQuaternion( quaternion );
object.applyMatrix( matrix );
或者,如果你不需要矩阵,你可以直接应用四元数:
Alternatively, if you do not require the matrix, you can just apply the quaternion directly:
object.applyQuaternion( quaternion );
three.js r.86
three.js r.86
这篇关于如何在 THREE.js 中找到两个向量之间的旋转矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!