0
$\begingroup$

Can someone please explain the math behind converting a direction-vector or lookAt-vector into a quaternion rotation?

What I am trying to do? I am trying to create a custom aim-constraint for a DCC (Maya. Also, Maya already provides one but I have reason to write a custom one). I have the world-space positions of eye and destination using which I can calculate direction vector but I need to calculate quaternion/euler rotation to rotate the object so that I can look at the destination.

What I found so far? There are couple of thread I found and read so far but some provide engine based approach, some do not provide explanation of math behind it and a few didn't work when converted into code. I am listing few of them here (for reference)

  1. https://gamedev.stackexchange.com/questions/149006/direction-vector-to-quaternion
  2. https://stackoverflow.com/questions/15873996/converting-a-direction-vector-to-a-quaternion-rotation
  3. https://answers.unity.com/questions/631836/how-i-can-converting-a-direction-vector-to-quatern.html
  4. http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
  5. https://math.stackexchange.com/questions/1765799/convert-direction-vector-to-euler-angles

What I am Not looking for?

  • way of calculating quaternion rotation from a (transformation) matrix.
  • engine based approach eg: Quaternion.LookRotation(relativePos, Vector3.up)

Thank you!

$\endgroup$
  • $\begingroup$ Here you go: lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final $\endgroup$ – lightxbulb Oct 15 '20 at 8:13
  • $\begingroup$ @lightxbulb: Thanks for pointing me to the link. I tried the code but it doesn't give correct rotation for some reason. For example if v = (0,1,0) and u= (5,0,0), quaternion value function gives is : 'Quaternion : ', 0.7071067811865475, 0.0, 0.0, 0.7071067811865475 'Euler : ', 90.0, 0.0, 0.0 (in degrees) which is not correct. $\endgroup$ – user2259784 Oct 16 '20 at 4:24
  • $\begingroup$ Or you're reading it wrong and it is 0,0,90. Which would be correct. $\endgroup$ – lightxbulb Oct 16 '20 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.