Difference between revisions of "Category:LSL Rotation"

From Second Life Wiki
Jump to navigation Jump to search
(made more in-depth like the other data type category pages)
(reordered since addition/subtraction are not very useful; added footnote about what rot div actually means mathematically)
Line 22: Line 22:
===Operators===
===Operators===
Rotations support the following operations:
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, 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 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 operands matters.
Composition is not commutative, i.e. the order of the composition matters.
<syntaxhighlight lang="lsl2">
<syntaxhighlight lang="lsl2">
// 90 degree rotation around x-axis
// 90 degree rotation around x-axis
Line 49: Line 36:
rotation r5 = r2*r1; // <0.5, -0.5, 0.5, 0.5>
rotation r5 = r2*r1; // <0.5, -0.5, 0.5, 0.5>
</syntaxhighlight>
</syntaxhighlight>
* Rotation multiplication is true [[quaternion]] multiplication, but division is multiplication by the {{wikipedia|Quaternion#Conjugation,_the_norm,_and_reciprocal|conjugate}} instead.<ref>I.e. rotation division is <code>q/p = qp̅</code>. To get true quaternion division, <code>q/p = qp̅/‖p‖²</code>, you must divide the result by the squared magnitude of the divisor <code>m = ‖p‖² = p.x*p.x+p.y*p.y+p.z*p.z+p.s*p.s</code>. Since LSL doesn't support multiplying/dividing a rotation by a float, the scaling must be done componentwise, e.g. <code>p = <p.x/m, p.y/m, p.z/m, p.s/m></code> or by multiplying by a scalar rotation <code>p = p*<0, 0, 0, 1.0/m></code>.</ref>


==== Vector Rotation, Operators "*" and "/" ====
==== Vector Rotation, Operators "*" and "/" ====
Line 62: Line 50:
vector vr4 = v*r4; // <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
vector vr4 = v*r5; // <-2, 0, 1> - rotated 90 degrees around z, then 90 degrees around x
</syntaxhighlight>
==== Addition and Subtraction, Operators "+" and "-" ====
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 ra = <1, 2, 3, 4> + <0.5, 0.5, 0.5, 0.5>; // <1.5, 2.5, 3.5, 4.5>
rotation rs = <1, 2, 3, 4> - <0.5, 0.5, 0.5, 0.5>; // <0.5, 1.5, 2.5, 3.5>
</syntaxhighlight>
</syntaxhighlight>


Line 74: Line 69:
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:
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>
<syntaxhighlight lang="lsl2">llEuler2Rot(<0.0, 0.0, PI>) * llGetRot()</syntaxhighlight>
== Footnotes ==
<references/>

Revision as of 04:40, 28 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:

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 operands 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>
  • Rotation multiplication is true quaternion multiplication, but division is multiplication by the "Wikipedia logo"conjugate instead.[1]

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

Addition and Subtraction, Operators "+" and "-"

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

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

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()

Footnotes

  1. I.e. rotation division is q/p = qp̅. To get true quaternion division, q/p = qp̅/‖p‖², you must divide the result by the squared magnitude of the divisor m = ‖p‖² = p.x*p.x+p.y*p.y+p.z*p.z+p.s*p.s. Since LSL doesn't support multiplying/dividing a rotation by a float, the scaling must be done componentwise, e.g. p = <p.x/m, p.y/m, p.z/m, p.s/m> or by multiplying by a scalar rotation p = p*<0, 0, 0, 1.0/m>.