Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Last active March 24, 2020 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thatcosmonaut/1bad9da8fffab62d65a48b6dce79a99e to your computer and use it in GitHub Desktop.
Save thatcosmonaut/1bad9da8fffab62d65a48b6dce79a99e to your computer and use it in GitHub Desktop.
a LookAt helper method
// 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