7

I've got a pair of vectors. How can I create a quaternion that rotates from one to the other?

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • possible duplicate of [Finding quaternion representing the rotation from one vector to another](http://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another) – Boann Jul 10 '14 at 16:17

1 Answers1

12

A unit quaternion q = cos(F)+u*sin(F) represents the rotation of vector v by the angle 2*F about axis u.

If your vectors are v and w, then we should normalize them, then calculate the angle between them as 2*F=ArcCos(Dot(v, w)). Rotation axis direction vector u = Normalize(VectorProduct(v, w)). Now we can build required rotation quaternion.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • 3
    It might also be a good idea to normalize the rotation axis **u** after it's been computed to sustain a unit quaternion, as the cross product of two unit vectors is only normalized for orthogonal input vectors. – Christian Rau Apr 20 '12 at 09:29
  • @Christian Rau You are right, I've missed this normalization. Added. – MBo Apr 20 '12 at 09:55
  • 9
    Note that the case v = −w needs special handling. – Gareth Rees Apr 21 '12 at 12:13