Math
LSL has integers and floats, plus vector and rotation types for 3D work. Rotations are quaternions; you rarely need their components, because conversion functions translate them to and from Euler angles, axes and angle-axis form.
These building blocks cover projectile trajectories, look-at rotations, simple inverse kinematics with trigonometry, noise and procedural patterns, and numeric integration for custom physics. Applying the results to objects is covered by Movement and Physics.
Concepts
- Basic math: absolute values,
rounding,powers,rootsandlogarithms- Integer and float operators follow the usual rules; dividing integers truncates
- Statistics over a list of numbers in one call
- Angles are in radians
DEG_TO_RADandRAD_TO_DEGconvertPI,TWO_PIandPI_BY_TWOare constants
- Vectors hold positions, velocities, scales and colors
Length,normalisationanddistancefunctions, plus the dot (*) and cross (%) operators, cover most vector math
- Rotations (quaternions)
- Multiply rotations to combine them, and multiply a vector by a rotation to rotate it
- Convert to and from Euler angles,
axesand angle-axis, and find the rotation between two directions
- Random numbers
llFrandreturns a random float up to a limit- Shuffle lists, and make unique identifiers
- Interpolation and color
Functions and events
integer | llAbs(integer val) |
float | llFabs(float val) |
integer | llRound(float val) |
float | llPow(float base, float exponent) |
float | llSqrt(float val) |
float | llSin(float theta) |
float | llAtan2(float y, float x) |
float | llVecMag(vector vec) |
vector | llVecNorm(vector vec) |
float | llVecDist(vector vec_a, vector vec_b) |
rotation | llEuler2Rot(vector vec) |
vector | llRot2Euler(rotation q) |
rotation | llRotBetween(vector start_vec, vector end_vec) |
float | llFrand(float mag) |
Rotations
Quaternions combine by multiplication; convert them with the Euler, axis and angle-axis functions.
More on the Second Life Wiki.
Math on the wiki
The wiki’s category of math functions.
Scripting Details
Basic math
Section titled “Basic math”integer | llAbs(integer val)Returns the absolute (positive) integer value of val. |
float | llFabs(float val)Returns the absolute (positive) value of val. |
integer | llRound(float val)Returns val rounded to the nearest integer. Halfway values are rounded toward infinity. |
integer | llFloor(float val)Returns val rounded toward negative infinity. In other words, returns the largest integer less than or equal to val. |
integer | llCeil(float val)Returns val rounded toward positive infinity. In other words, returns the smallest integer greater than or equal to val. |
float | llPow(float base, float exponent)Returns base raised to the power exponent. If result is imaginary, returns NaN. |
float | llSqrt(float val)Returns the square root of val. If negative, return NaN. |
float | llLog(float val)Returns natural (base e) logarithm of val. If negative, return 0.0. |
float | llLog10(float val)Returns base-10 (common) logarithm of val. If negative, return 0.0. |
integer | llModPow(integer base, integer exponent, integer modulus)Returns base raised to the power exponent, modulo modulus (i.e., (b^e)%m). All inputs are wrapped to unsigned 32-bit integer range [0..4294967295]. Output is wrapped to signed 32-bit integer range [-2147483648..2147483647]. Will never overflow, unlike (b^e), which can overflow to inf. |
float | llListStatistics(integer operation, list src)Returns the numeric result of the statistical aggregate function operation (a LIST_STAT_* constant) on the numeric list src. |
Angles and trigonometry
Section titled “Angles and trigonometry”float | llSin(float theta)Returns the sine of theta. Theta is in radians. |
float | llCos(float theta)Returns the cosine of theta. Theta is in radians. |
float | llTan(float theta)Returns the tangent of theta. Theta is in radians. |
float | llAsin(float val)Returns the arcsine of val in radians. |
float | llAcos(float val)Returns the arccosine of val in radians. |
float | llAtan2(float y, float x)Returns the arctangent of y/x in radians, using the signs to determine the quadrant. Note the argument order: Y is the first parameter, X is the second parameter. |
float | PI = 3.14159265Mathematical constant pi, representing the number of radians in a half circle (semi-circle). When used in sensor functions, it specifies a full sphere scan. |
float | TWO_PI = 6.28318530Mathematical constant pi*2, representing the number of radians in a full circle. |
float | PI_BY_TWO = 1.57079633Mathematical constant pi/2, representing the number of radians in a quarter circle. When used in sensor functions, it specifies a hemisphere scan. |
float | DEG_TO_RAD = 0.017453293Constant 0.017453293 (precise value is PI/180). Multiply a value in degrees by this number to convert it to radians. |
float | RAD_TO_DEG = 57.2957795180/PI. Multiply a value in radians by this number to convert it to degrees. |
Vectors
Section titled “Vectors”float | llVecMag(vector vec)Returns the magnitude (geometric length) of vec. |
vector | llVecNorm(vector vec)Returns the normalized unit vector pointing the same direction as vec. If <0, 0, 0>, return <0, 0, 0>. |
float | llVecDist(vector vec_a, vector vec_b)Returns a float representing the undirected, non-negative distance between vectors vec_a and vec_b. |
vector | ZERO_VECTOR = <0.0, 0.0, 0.0>A vector constant representing <0.0, 0.0, 0.0>. This is the default value for vector variables. |
Rotations
Section titled “Rotations”rotation | llEuler2Rot(vector vec)Returns the quaternion representation of the Euler angles (in radians) within vec. |
vector | llRot2Euler(rotation q)Returns a vector of Euler angles (roll, pitch, yaw) of q. The angles will be in radians. |
rotation | llAxisAngle2Rot(vector axis, float angle)Returns the rotation that rotates angle radians around the axis vector. |
float | llRot2Angle(rotation q)Returns the angle, in radians, that q rotates. |
vector | llRot2Axis(rotation q)Returns the unit vector axis that q rotates around. |
rotation | llAxes2Rot(vector fwd, vector left, vector up)Returns the rotation defined by the coordinate axes fwd, left, and up. |
vector | llRot2Fwd(rotation q)Returns the unit vector pointing toward positive X (forward) in the coordinate space of rotation q. Equivalent to <1, 0, 0> * q. |
vector | llRot2Left(rotation q)Returns the unit vector pointing toward positive Y (left) in the coordinate space of rotation q. Equivalent to <0, 1, 0> * q. |
vector | llRot2Up(rotation q)Returns the unit vector pointing toward positive Z (up) in the coordinate space of rotation q. Equivalent to <0, 0, 1> * q. |
rotation | llRotBetween(vector start_vec, vector end_vec)Returns the shortest-path quaternion that rotates start_vec onto end_vec. |
float | llAngleBetween(rotation start_rot, rotation end_rot)Returns the angle, in radians, between rotations start_rot and and end_rot. |
rotation | ZERO_ROTATION = <0.0, 0.0, 0.0, 1.0>A rotation constant representing an identity rotation (causes no change). This is the default value for rotation variables. |
Random numbers
Section titled “Random numbers”float | llFrand(float mag)Returns a pseudo-random float in the range [0.0, mag) or (mag, 0.0] depending on the sign of mag. The value is inclusive of 0.0 but exclusive of mag. |
list | llListRandomize(list src, integer stride)Returns a randomized copy of the list src by blocks of size stride. If the list length is not perfectly divisible by stride, no randomization occurs. |
key | llGenerateKey()Generates and returns a unique versioned UUID key (utilizing SHA-1 hashing). Due to being versioned, it will not return NULL_KEY; however, the exact UUID version is an implementation detail that should not be relied upon. |
vector | llLinear2sRGB(vector color)Returns a sRGB colorspace vector converted from the linear RGB colorspace argument. |
vector | llsRGB2Linear(vector color)Returns a linear RGB colorspace vector converted from the sRGB colorspace argument. |
Related
Section titled “Related”See also Page integer typePage float typeCategory Math
See also Category Math TrigPlanned Recipe: simple inverse kinematics with trigonometry
See also Page vector typeCategory VectorCategory Math 3DPlanned Recipe: projectile trajectories
See also Page rotation typeCategory QuaternionPage Guide: constraining rotationsPage Guide: interpolation and easingSL Wiki LSL Rotation
See also Planned Recipe: Perlin noise maps
See also SL Wiki LSL MathFeature PhysicsFeature MovementPage Guide: interpolation and easingPlanned Recipe: numeric integration for custom physics