我试图弄清楚如何将球体从 A 点旋转到 B 点。我找到了一些Unity3d代码:
Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.rotation *= rot; //Multiply rotations to get the resultant rotation
via http://answers.unity3d.com/questions/21921/rotate-point-a-on-sphere-to-point-b-on-sphere.html http://answers.unity3d.com/questions/21921/rotate-point-a-on-sphere-to-point-b-on-sphere.html但我不知道如何在 Three.js 中实现它。
这是我的代码:
var s = sphere mesh
var va = 1st start vector
var vb = 2nd end vector;
var qa = new THREE.Quaternion(va.x, va.y, va.z, 1);
var qb = new THREE.Quaternion(vb.x, vb.y, vb.z, 1);
var qc = new THREE.Quaternion();
THREE.Quaternion.slerp(qa, qb, qc, 1);
s.useQuaternion = true;
s.quaternion = qc;
Thanks!
Assume the sphere is centered at the origin and A
and B
are normalized (i.e. unit length). Then compute the cross product http://en.wikipedia.org/wiki/Cross_product C = A×B
. This is your vector representing your rotation axis. The angle for your rotation is given by θ = cos-1(A∙B)
where A∙B
is the dot product http://en.wikipedia.org/wiki/Dot_product of the unit vectors A
and B
. Note that the angle in this case is typically in radians, not degrees.
如果球体以某个点为中心P
不在原点或其半径不是单位长度,您必须在导出旋转之前平移和缩放 A 和 B。这看起来像:
A' ← (A-P)/|A-P| // Normalized version of A
B' ← (B-P)/|B-P| // Normalized version of B
V ← A'×B' // Axis about which to rotate the system
θ ← cos-1(A'∙B') // Angle by which to rotate
然后您可以使用V
and θ
作为构造四元数的参数,您将使用它来定义旋转。此旋转将以原点为中心,因此在应用它之前,请先平移 -P,然后应用旋转,然后再平移 P。
需要注意的是:这里可能存在符号错误。如果不起作用,那是因为叉积的符号与点积的符号不匹配。如果这是一个问题,只需颠倒叉积中参数的顺序即可修复它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)