3

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!

Jisike
  • 383
  • 1
  • 3
  • 11
  • Just out of curiosity, is there anything in the answer I provided below which doesn't satisfy your needs? If there is some way it could be more useful to you, I'd like to help. – andand Aug 30 '12 at 16:41
  • 1
    A code example in 3.js/js syntax and not math syntax would be great! – Jisike Sep 04 '12 at 16:48
  • There's a couple of issues with this. First, I'm not familiar with 3.js. I'm actually in the process of learning js for myself, and don't have all of the bits straight enough in my head at this point to do justice to it. The other, and perhaps larger issue regards whether you understand the concept below. If you don't you are probably better served by developing that understanding rather than being given code which you will be unable to maintain at some later date. So, do you understand what cross and dot products are? If not I can expand upon those concepts and you can code it yourself. – andand Sep 04 '12 at 18:42
  • @andand as this question is a few years old, do you have any threejs knowledge to help with the syntax now? – Neil Dec 14 '15 at 14:10
  • @neil I haven't learned threejs yet... I originally answered this question based on the math tag, not any of the others. – andand Dec 14 '15 at 14:44
  • @andand I can almost understand it but need to relate it to the methods in threejs, would you be able to help? – Neil Dec 14 '15 at 15:48

2 Answers2

3

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.

andand
  • 17,134
  • 11
  • 53
  • 79
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;

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

GK90
  • 31
  • 4