Unofficial LSL Reference

[[functions:llatan2]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Function: llAtan2

float llAtan2(float y, float x)

Return the angle of the 2D vector given by x and y. Note that the arguments are in reverse order: the first is y, the second is x.

Parameters

y

The y component of the 2D vector.

x

The x component of the 2D vector.

Return value

Float number with the angle of the vector.

Notes

  • If both components are zero, the result is zero.
  • The result is always between -PI and PI, except if one of the inputs is NaN, in which case it returns NaN as well.
  • This function distinguishes zero and minus zero, in that e.g. llAtan2(-0.0, -1) gives -PI, not PI.
  • The name and order of parameters of this function come from the fact that the angle of a 2D vector with a nonzero x coordinate is given by the arctangent (inverse tangent) of y/x. The order y, x is customarily used in many programming languages.
  • The convention in navigation is that zero degrees is North (or forward, if expressing a relative angle), and the angle grows clockwise, so 90 degrees is East (or right), and so on. This differs from the convention in mathematics, where zero degrees is East, etc. To use the navigation convention, only "un-inverting" x and y is needed, e.g. llAtan2(1, 2) is the angle of the vector (1, 2) expressed with this convention.

Short examples

float angle;
angle = llAtan2(0, 1); // Sets angle to 0 because that's the angle of the vector (1, 0).
angle = llAtan2(-3, 0); // Sets angle to -PI_BY_TWO, because that's the angle of the vector (0, -3).
angle = llAtan2(1.3, 2.3); // Sets angle to approx. 0.514451 as that's the angle of the vector (2.3, 1.3)
angle = llAtan2(0, 0); // Sets angle to 0.
angle = llAtan2(0, -1); // Sets angle to PI, because that's the angle of the vector (-1, 0).
angle = llAtan2(-0.0, -2); // Sets angle to -PI.
angle = llAtan2((float)"NaN", 0);  // Sets angle to NaN (Not a Number) because one input is NaN.

Complete examples

The following example will output the heading of a prim, in degrees:

llAtan2-example.lsl
default
{
    state_entry()
    {
        vector fwd = <1,0,0>*llGetLocalRot();

        float heading = llAtan2(fwd.y, fwd.x);

        llOwnerSay("Heading=" + (string)(heading * RAD_TO_DEG) + "°");
    }
}

See also

  • llTan calculates the tangent of the argument.
  • llAsin calculates the inverse sine of the argument.
  • llSin calculates the sine of the argument.
  • llAcos calculates the inverse cosine of the argument.
  • llCos calculates the cosine of the argument.
  • llAngleBetween calculates the angle between two rotations.
  • llRot2Angle takes the angle part of a rotation.
  • Other math functions.