Skip to content

Instantly share code, notes, and snippets.

@CaptainPRICE
Last active June 23, 2017 14:27
Show Gist options
  • Save CaptainPRICE/418750b896f49d36567f4be049384e34 to your computer and use it in GitHub Desktop.
Save CaptainPRICE/418750b896f49d36567f4be049384e34 to your computer and use it in GitHub Desktop.
GLua "Angle.SnapTo" function for Expression 2.
#ifndef normalizeAngle(number)
### <summary>Normalizes angle (-180 <= A <= 180).</summary>
### <param name="A">The angle to normalize, in degrees.</param>
### <returns>Returns the normalized angle, in the range of -180 to 180 degrees.</returns>
function number normalizeAngle(A)
{
return (A + 180) % 360 - 180
} # End of function number=normalizeAngle(number)
#endif
#ifndef angle:snapTo(number, number)
### <summary>Snaps the angle to nearest interval of degrees (this function will modify the original angle).</summary>
### <param name="Component">The component/axis to snap. Can be either 1 for pitch, 2 for yaw or 3 for roll.</param>
### <param name="Degrees">The target angle snap interval.</param>
### <returns>Returns the snapped angle.</returns>
function angle angle:snapTo(Component, Degrees)
{
if (Degrees == 0) { error("The snap degrees must be non-zero") }
This[Component] = normalizeAngle(round(This[Component] / Degrees) * Degrees)
return This
} # End of function angle=angle:snapTo(number, number)
#endif
#ifndef angle:snapTo(string, number)
### <summary>Snaps the angle to nearest interval of degrees (this function will modify the original angle).</summary>
### <param name="Component">The component/axis to snap. Can be either "p"/"pitch", "y"/"yaw" or "r"/"roll".</param>
### <param name="Degrees">The target angle snap interval.</param>
### <returns>Returns the snapped angle.</returns>
function angle angle:snapTo(Component:string, Degrees)
{
switch (Component:lower())
{
case "p",
case "pitch",
return This:snapTo(1, Degrees)
case "y",
case "yaw",
return This:snapTo(2, Degrees)
case "r",
case "roll",
return This:snapTo(3, Degrees)
default,
error("You must choose a valid component of Angle")
}
} # End of function angle=angle:snapTo(string, number)
#endif
# Example Usage
print(ang(0, 92.567, 0):snapTo("yaw", 90)) # [0,90,0]
print(ang(0, 115, 0):snapTo("y", 45)) # [0,135,0]
print(ang(12, 98, 167):snapTo("p", 30):snapTo("y", 45):snapTo("r", 45)) # [0,90,-180]
local Ang = ang(-60.99, -130, -47)
Ang:snapTo("pitch", _PI):snapTo("yaw", 60):snapTo("roll", 5)
print(Ang) # [-59.690260418206,-120,-45]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment