Created
December 22, 2021 22:20
-
-
Save ykafia/9579569c26e2724f1a35afab0449b72a to your computer and use it in GitHub Desktop.
Look at
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
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