Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Getting quaternion from forward and up

Started by SunTzu Jun 17, 2007 at 3:19 AM 1 replies 3.7k views
Original Post
SunTzu
SunTzu
I have a (unit) forward vector and a (unit) up vector (known to be orthogonal to one another). How do I get the quaternion that rotates to such an orientation? In other words: there is some quaternion that would transform (0,1,0) into the up vector and (0,0,1) into the forward vector. How do I calculate what that quaternion is? I'm actually implementing this in C#/XNA so have all the .NET and XMA framework functions available (none of them appear to provide this automatically), but I can easily translate from other languages or straight mathematics if required. Quaternions confuse the heck out of me. :-)
DaBookshah
DaBookshah
I'm no expert, and I don't really understand quaternions either, but here's an idea:

You can easily represent the rotation transformation as a 3x3 matrix. If you want to transform (1,0,0) into (a,b,c), (0,1,0) into (d,e,f) and (0,0,1) into (g,h,i), then your matrix is

a d g
b e h
c f i

(in the standard maths notation). Then you just need to convert this orthogonal matrix into a quaternion. Just use google - "quaternion rotation matrix" or "quaternion orthogonal matrix", I turned up this. I think that does it?
SunTzu
SunTzu
Brilliant. Rate++.

Especially easy as the .NET framework provides Matrix.Forward, Matrix.Up and Matrix.Right as properties, and Quaternion.CreateFromRotationMatrix as well. It was just a question of taking the step from "transform (0,1,0) into the up vector" etc., into "transform (0,1,0) into (d,e,f)... then your matrix is..." etc. Many thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.