Last active
March 24, 2020 23:02
-
-
Save thatcosmonaut/1bad9da8fffab62d65a48b6dce79a99e to your computer and use it in GitHub Desktop.
a LookAt helper method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assumes that the input vectors are normalized and orthogonal | |
public static Quaternion LookAt(in Vector3 forward, in Vector3 up) | |
{ | |
var right = Vector3.Cross(up, forward); | |
Quaternion quaternion; | |
quaternion.W = (float)Math.Sqrt(1.0f + right.X + up.Y + forward.Z) * 0.5f; | |
var w4_recip = 1.0f / (4.0f * quaternion.W); | |
quaternion.X = (forward.Y - up.Z) * w4_recip; | |
quaternion.Y = (right.Z - forward.X) * w4_recip; | |
quaternion.Z = (up.X - right.Y) * w4_recip; | |
return quaternion; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment