将球体a旋转到b点自身

时间:2012-08-29 18:48:48

标签: javascript math webgl three.js

我试图找出如何将球体从A点自身旋转到b点自身。我找到了一些Unity3d代码:

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.rotation *= rot; //Multiply rotations to get the resultant rotation

通过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;

谢谢!

2 个答案:

答案 0 :(得分:3)

假设球体以原点为中心,AB被归一化(即单位长度)。然后计算cross product C = A×B。这是表示旋转轴的矢量。轮换的角度由θ = cos-1(A∙B)给出,其中A∙B是单位向量AB的{​​{3}}。请注意,在这种情况下,角度通常为弧度,而不是度数。

如果球体在某个点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θ作为构造将用于定义旋转的四元数的参数。此旋转将以原点为中心,因此在应用之前,请按-P进行平移,然后应用旋转,然后通过P进行平移。

一个注意事项:此处可能存在符号错误。如果它不起作用,那是因为交叉产品的符号与点积的符号不匹配。如果这是一个问题,只需颠倒交叉产品中参数的顺序来修复它。

答案 1 :(得分:0)

var c = group.rotation.y;
var d = -b * (Math.PI / 180)%(2 * Math.PI);
var e = Math.PI / 2 * -1;
group.rotation.y = c % (2 * Math.PI);
group.rotation.x = a * (Math.PI / 180) % Math.PI;
group.rotation.y= d+e;

其中a =纬度,b =经度,group = Object3D(或球体)