Difference between revisions of "Category:LSL Rotation"

From Second Life Wiki
Jump to navigation Jump to search
m
(made more in-depth like the other data type category pages)
Line 1: Line 1:
{{LSL Header|ml=*}}{{LSLC|}}{{LSLC|Types}}
{{LSL Header|ml=*}}{{LSLC|}}{{LSLC|Types}}
The [[rotation]] page is where you can find detailed information about how to work with rotations.
A rotation is a data {{LSLGC|Types|type}} that contains a set of four {{LSLGC|Float|float}} values.


Functions in this category fall into two categories: Rotations in the purely mathematical sense and Rotations as in prim / object rotation.
===Components===
Each element can be accessed individually by appending .x, .y, .z or .s to the variable name.


Quick Tip! To reverse a rotation (say something is facing north and you want it to face south, e.g. 180 degrees difference), add pi to the z value of the euler vector:
<syntaxhighlight lang="lsl2">
rotation rot = <1, 2, 3, 4>;
float x = rot.x; // 1.0
float y = rot.y; // 2.0
float z = rot.z; // 3.0
float s = rot.s; // 4.0
</syntaxhighlight>


<source lang="lsl2">llEuler2Rot(<0.0, 0.0, PI>) * llGetRot()</source>
Note that components can be only accessed with rotation variables. Attempting to access the components of a rotation literal or a return value from a function is illegal.
<syntaxhighlight lang="lsl2">
// These are not valid component accesses
float f1 = <0.707, 0, 0, 0.707>.x;
float f2 = llGetRot().s;
</syntaxhighlight>
 
===Operators===
Rotations support the following operations:
==== Addition, Operator "+" ====
Note that this is only component-wise addition and does ''not'' combine two rotations in a way that is meaningful for rotation calculations.
<syntaxhighlight lang="lsl2">
rotation r = <1, 2, 3, 4> + <0.5, 0.5, 0.5, 0.5>; // <1.5, 2.5, 3.5, 4.5>
</syntaxhighlight>
 
==== Subtraction, Operator "-" ====
Note that this is only component-wise subtraction and does ''not'' combine two rotations in a way that is meaningful for rotation calculations.
<syntaxhighlight lang="lsl2">
rotation r = <1, 2, 3, 4> - <0.5, 0.5, 0.5, 0.5>; // <0.5, 1.5, 2.5, 3.5>
</syntaxhighlight>
 
==== Composition, Operators "*" and "/" ====
Composition combines two rotations into a new rotation representing the combined action of both, unlike addition/subtraction. "/" rotates in the opposite direction to "*".
Composition is not commutative, i.e. the order of the composition matters.
<syntaxhighlight lang="lsl2">
// 90 degree rotation around x-axis
rotation r1 = llEuler2Rot(<PI_BY_TWO, 0, 0>); // <0.70711, 0, 0, 0.70711>
// 90 degree rotation around z-axis
rotation r2 = llEuler2Rot(<0, 0, PI_BY_TWO>); // <0, 0, 0.70711, 0.70711>
// combined rotation
rotation r3 = r1*r2; // <0.5, 0.5, 0.5, 0.5>
// combined rotation, r2 in reverse direction
rotation r4 = r1/r2; // <0.5, -0.5, -0.5, 0.5>
// note the non-commutativity: different result from r1*r2
rotation r5 = r2*r1; // <0.5, -0.5, 0.5, 0.5>
</syntaxhighlight>
 
==== Vector Rotation, Operators "*" and "/" ====
Produces a rotated vector. "/" rotates in the opposite direction to "*".
Vectors can be rotated only from the right side: rotation*vector is illegal.
<syntaxhighlight lang="lsl2">
// vector pointing 1 unit forward (x), 2 units left (y), 0 units up (z)
vector v = <1, 2, 0>;
// see r1-r5 from composition example above
vector vr1 = v*r1; // <1, 0, 2> - rotated 90 degrees around x-axis
vector vr2 = v*r2; // <-2, 1, 0> - rotated 90 degrees around z-axis
vector vr3 = v*r3; // <0, 1, 2> - rotated 90 degrees around x, then 90 degrees around z
vector vr4 = v*r4; // <0, -1, 2> - rotated 90 degrees around x, then -90 degrees around z
vector vr4 = v*r5; // <-2, 0, 1> - rotated 90 degrees around z, then 90 degrees around x
</syntaxhighlight>
 
==Uses==
Functions in this category fall into two categories: rotations in the purely mathematical sense and rotations as in prim / object rotation.
 
The [[rotation]] page is where you can find detailed information about how to work with rotations. [[Quaternion]] is also an acceptable alias for variables of rotation type, and the article explains some further mathematical details behind them.
 
Rotations are also the most memory-efficient way of storing non-rotation related bundles of 4 [[float]] values, especially inside [[list]]s.
 
==Useful Snippets==
To reverse a rotation (say something is facing north and you want it to face south, e.g. 180 degrees difference), add pi to the z value of the euler vector:
<syntaxhighlight lang="lsl2">llEuler2Rot(<0.0, 0.0, PI>) * llGetRot()</syntaxhighlight>

Revision as of 08:08, 18 October 2023

A rotation is a data type that contains a set of four float values.

Components

Each element can be accessed individually by appending .x, .y, .z or .s to the variable name.

rotation rot = <1, 2, 3, 4>;
float x = rot.x; // 1.0
float y = rot.y; // 2.0
float z = rot.z; // 3.0
float s = rot.s; // 4.0

Note that components can be only accessed with rotation variables. Attempting to access the components of a rotation literal or a return value from a function is illegal.

// These are not valid component accesses
float f1 = <0.707, 0, 0, 0.707>.x;
float f2 = llGetRot().s;

Operators

Rotations support the following operations:

Addition, Operator "+"

Note that this is only component-wise addition and does not combine two rotations in a way that is meaningful for rotation calculations.

rotation r = <1, 2, 3, 4> + <0.5, 0.5, 0.5, 0.5>; // <1.5, 2.5, 3.5, 4.5>

Subtraction, Operator "-"

Note that this is only component-wise subtraction and does not combine two rotations in a way that is meaningful for rotation calculations.

rotation r = <1, 2, 3, 4> - <0.5, 0.5, 0.5, 0.5>; // <0.5, 1.5, 2.5, 3.5>

Composition, Operators "*" and "/"

Composition combines two rotations into a new rotation representing the combined action of both, unlike addition/subtraction. "/" rotates in the opposite direction to "*". Composition is not commutative, i.e. the order of the composition matters.

// 90 degree rotation around x-axis
rotation r1 = llEuler2Rot(<PI_BY_TWO, 0, 0>); // <0.70711, 0, 0, 0.70711>
// 90 degree rotation around z-axis
rotation r2 = llEuler2Rot(<0, 0, PI_BY_TWO>); // <0, 0, 0.70711, 0.70711>
// combined rotation
rotation r3 = r1*r2; // <0.5, 0.5, 0.5, 0.5>
// combined rotation, r2 in reverse direction
rotation r4 = r1/r2; // <0.5, -0.5, -0.5, 0.5>
// note the non-commutativity: different result from r1*r2
rotation r5 = r2*r1; // <0.5, -0.5, 0.5, 0.5>

Vector Rotation, Operators "*" and "/"

Produces a rotated vector. "/" rotates in the opposite direction to "*". Vectors can be rotated only from the right side: rotation*vector is illegal.

// vector pointing 1 unit forward (x), 2 units left (y), 0 units up (z)
vector v = <1, 2, 0>;
// see r1-r5 from composition example above
vector vr1 = v*r1; // <1, 0, 2> - rotated 90 degrees around x-axis
vector vr2 = v*r2; // <-2, 1, 0> - rotated 90 degrees around z-axis
vector vr3 = v*r3; // <0, 1, 2> - rotated 90 degrees around x, then 90 degrees around z
vector vr4 = v*r4; // <0, -1, 2> - rotated 90 degrees around x, then -90 degrees around z
vector vr4 = v*r5; // <-2, 0, 1> - rotated 90 degrees around z, then 90 degrees around x

Uses

Functions in this category fall into two categories: rotations in the purely mathematical sense and rotations as in prim / object rotation.

The rotation page is where you can find detailed information about how to work with rotations. Quaternion is also an acceptable alias for variables of rotation type, and the article explains some further mathematical details behind them.

Rotations are also the most memory-efficient way of storing non-rotation related bundles of 4 float values, especially inside lists.

Useful Snippets

To reverse a rotation (say something is facing north and you want it to face south, e.g. 180 degrees difference), add pi to the z value of the euler vector:

llEuler2Rot(<0.0, 0.0, PI>) * llGetRot()