rotating a sphere a to b point on itself

女生的网名这么多〃 提交于 2019-12-10 19:47:26

问题


Im trying to figure out how to rotate a sphere from point A on itself to point b on itself. I found some Unity3d code:

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 but I can't figure out how to implement it in Three.js.

Here's my code:

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!


回答1:


Assume the sphere is centered at the origin and A and B are normalized (i.e. unit length). Then compute the 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 of the unit vectors A and B. Note that the angle in this case is typically in radians, not degrees.

If the sphere is centered at some point P not at the origin or its radius is not unit length, you will have to translate and scale A and B before derive the rotation. This looks like:

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

You can then use V and θ as your arguments for constructing the quaternion you will use for defining your rotation. This rotation will be centered at the origin, so before applying it, translate by -P, then apply the rotation, and translate back by P.

One note: There may be a sign error in here. If it doesn't work, it's because the sign of the cross product doesn't match with the sign of the dot product. Just reverse the order of the arguments in the cross product to fix it if this is a problem.




回答2:


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;

where a= latitude, b= longitude,group=Object3D(or sphere)



来源:https://stackoverflow.com/questions/12184589/rotating-a-sphere-a-to-b-point-on-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!