I am rendering graphics myself using OpenGL, therefore I need to implement math library myself. I am looking for function Quaternion::LookRotation(forward, up)
, given a forward and up vector, it shall create a quaternion rotating in this direction.
I've found this answer http://answers.unity3d.com/questions/467614/what-is-the-source-code-of-quaternionlookrotation.html for LookRotation and it works well providing we are using Unity's coordinate system (left handed): Forward: (0, 0, 1)
, Up: (0, 1, 0)
, Right (1, 0, 0)
.
In this coordinate system the function works as expected:
var dir = new Vector3(1, 0, 1).normalized;
var quaternion = Quaternion.LookRotation(dir, Vector3.Up); // Up == (0, 1, 0)
var backToDirection = quaternion * Vector3.Forward; // Forward == (0, 0, 1)
// backToDirection equals to dir
However, in my "engine", I am using right handed, Z-up coordinate system: Forward: (1, 0, 0)
, Up: (0, 0, 1)
, Right: (0, -1, 0)
.
Unless I am wrong, the very same code (with corrected LookRotation) should still work:
var dir = new Vector3(1, 0, 1).normalized;
var quaternion = Quaternion.LookRotation(dir, Vector3.Up); // new Up == (0, 0, 1)
var backToDirection = quaternion * Vector3.Forward; // new Forward == (1, 0, 0)
// backToDirection should be equal to dir, but obviously `LookRotation` needs to be changed
However, for this to work, LookRotation
needs to be adjusted accordingly. The source code for LookRotation
posted at answers.unity3d doesn't explain why, neither I could find a good explanation how to do it. Could anyone give me a hint what needs to be changed there in order to make it work in a different coordinate system?