<p><strong>Can someone please explain the math behind converting a direction-vector or lookAt-vector into a quaternion rotation?</strong></p>
<p><strong>What I am trying to do?</strong>
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.</p>
<p><strong>What I found so far?</strong>
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)</p>
<ol>
<li><a href="https://gamedev.stackexchange.com/questions/149006/direction-vector-to-quaternion">https://gamedev.stackexchange.com/questions/149006/direction-vector-to-quaternion</a></li>
<li><a href="https://stackoverflow.com/questions/15873996/converting-a-direction-vector-to-a-quaternion-rotation">https://stackoverflow.com/questions/15873996/converting-a-direction-vector-to-a-quaternion-rotation</a></li>
<li><a href="https://answers.unity.com/questions/631836/how-i-can-converting-a-direction-vector-to-quatern.html" rel="nofollow noreferrer">https://answers.unity.com/questions/631836/how-i-can-converting-a-direction-vector-to-quatern.html</a></li>
<li><a href="http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/" rel="nofollow noreferrer">http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/</a></li>
<li><a href="https://math.stackexchange.com/questions/1765799/convert-direction-vector-to-euler-angles">https://math.stackexchange.com/questions/1765799/convert-direction-vector-to-euler-angles</a></li>
</ol>
<p><strong>What I am Not looking for?</strong></p>
<ul>
<li>way of calculating quaternion rotation from a (transformation) matrix.</li>
<li>engine based approach eg: <code>Quaternion.LookRotation(relativePos, Vector3.up)</code></li>
</ul>
<p>Thank you!</p>

CC BY-SA 4.0
2020-10-15T08:13:46.693Z
Here you go: http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
lightxbulb
CC BY-SA 4.0
2020-10-16T04:24:40.657Z
@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.
user2259784
CC BY-SA 4.0
2020-10-16T07:49:30.060Z
Or you're reading it wrong and it is 0,0,90. Which would be correct.
lightxbulb
<mathematics>
Explanation of converting a look-at / direction vector to quaternion rotation?
user2259784
# Explanation of converting a look-at / direction vector to quaternion rotation?

(Asked by: username_0 on 2020-10-15)

**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!

**Tags:** mathematics

> **Comments:**
> **username_1**: Here you go: http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final  
> **username_0**: @username_1: 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.  
> **username_1**: Or you're reading it wrong and it is 0,0,90. Which would be correct.  

---

## 0 answers

