Table of Contents

Start Functions Events Constants Types Language Articles

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

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