Skip to content

Instantly share code, notes, and snippets.

@ykafia
Created December 22, 2021 22:20
Show Gist options
  • Save ykafia/9579569c26e2724f1a35afab0449b72a to your computer and use it in GitHub Desktop.
Save ykafia/9579569c26e2724f1a35afab0449b72a to your computer and use it in GitHub Desktop.
Look at
public static class EntityExtensions
{
public static void LookAt(this Entity e, Vector3 target)
{
float azimuth = GetLookAtAngles(e.Transform.Position, target, out float altitude);
var result = Quaternion.RotationYawPitchRoll(azimuth, -altitude, 0);
e.Transform.Rotation = result;
}
public static Vector3 Position(this Entity e) => e.Transform.Position;
private static float GetLookAtAngles(Vector3 source, Vector3 destination, out float altitude)
{
var x = source.X - destination.X;
var y = source.Y - destination.Y;
var z = source.Z - destination.Z;
altitude = (float)Math.Atan2(y, Math.Sqrt(x * x + z * z));
var azimuth = (float)Math.Atan2(x, z);
return azimuth;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment