Difference between revisions of "Rotation"

From Second Life Wiki
Jump to navigation Jump to search
Line 1: Line 1:
{{LSL Header}}
== rotation ==
== rotation ==



Revision as of 21:36, 27 January 2007

rotation

In LSL the rotation type is used for returning and setting the 3D rotation of objects. Note that rotations are not the same as vectors, they consist of 4 floats which allow operations on rotations to be more easily done, while avoiding some problems combining vectors have. For a more technical discussion of why rotations are used and what they are, see the links below, this page is about how to use rotations.

In LSL all rotations are done according to the "right hand rule". Make a fist with your right hand, thumb extended in the direction of the positive direction of an axis, Your fingers will curl around in the positive direction. When you're editing an object, the 3 axes arrows point in the positive direction for each axis.

To get the rotation of an unlinked prim, use:

<lsl>rotation myRot = llGetRot();</lsl>

Combining Rotations

To combine rotations, you use the multiply and divide operators for rotations. Don't try to use addition or subtraction operators on rotations, they aren't defined.

Unlike other types such as floats, the order the operations is done in is important. The reason for this is simple, the order you do rotations in is important in RL as well as SL. For example, if you had a dart with 4 feathers, started from rotation <0, 0, 0> with its tail on the origin, it would lie on the X axis with its point aimed in the positive X direction, its feathers along the Z and Y axes, and the axes of the dart and the axes of the world would be aligned. We're going to rotate it 45 degrees around X and 30 degrees around Y, but in different orders.

First, after rotating 45 deg around X the dart would still be on the X axis, unmoved, just turned along its long axis, so the feathers would be at 45 deg to the axes. Then rotating 30 deg around Y would move it in the XZ plane to point down 30 deg from the X axis (remember the right hand rule for rotations means a small positive rotation around Y moves the point down). The dart winds up pointing 30 deg down, in the same vertical plane it started in, but turned around its own long axis so the feathers are no longer up and down.

If you did it the other way, first rotating 30 deg in Y, the dart would rotate down in the XZ plane, but notice that it no longer is on the X axis, its X axis and the world's aren't aligned any more. Now a 45 degree rotation around the X axis would pivot the dart around its tail, the point following a 30 deg cone whose axis is along the positive world X axis, for 45 degrees up and to the right. If you were looking down the X axis, it would pivot from pointing 30 deg below the X axis, up and to the right, out of the XZ plane, to a point below the 1st quadrant in the XY plane, its feathers rotating as it went.

Clearly this is a different result from the first rotation, but the order of rotation is the only thing changed.

To do a constant rotation you need to define a rotation value which can be done by creating a vector with the X, Y, Z angles in radians as components (called an Euler angle), then converting that to a rotation.

NOTE: angles in LSL are in radians not degrees, but you can easily convert by using the built in constants RAD_TO_DEG and DEG_TO_RAD. For a 30 degree rotation around the X axis you might use:

<lsl>rotation rot30X = llEuler2Rot(<30, 0,0> * DEG_TO_RAD);</lsl>

Local vs Global rotations

Local rotations are ones done around the axes embedded in the object itself forward/back, left/right, up/down, irrespective of how the object is rotated in the world. Global rotations are ones done around the world axes, North/South, East/West, Higher/Lower. You can see the difference by rotating a prim, then edit it and change the axes settings between local and global.

In LSL, the difference between doing a local or global rotation is the order the rotations are evaluated in the statement.

This does a local rotation by putting the constant 30 degree rotation to the left of the object's rotation. It is like the first operation in the first example above, just twisting the dart around its own long axis.

<lsl>rotation localRot = rot30X * myRot;</lsl>

To do a global rotation, use the same values, but in the opposite order. This is like the second operation in the second example, the dart rotating up and to the right around the world X axis.

<lsl>rotation globalRot = myRot * rot30X;</lsl>

You may want to think about this difference by considering that rotations are done in evaluation order, that is left to right except for parenthesized expressions.

In the localRot case, what happened was that starting from <0, 0, 0>, the rot30X was done first, rotating the prim around the world X axis, but since when it's unrotated, the local and global axes are identical it has the effect of doing the rotation around the object's local X axis. Then the second rotation myRot was done which rotated the prim to its original rotation, but now with the additional X axis rotation baked in. What this looks like is that the prim rotated in place around its own X axis, a local rotation.

In the globalRot case, again starting from <0, 0, 0>, first the object is rotated to its original rotation, but the object's axes and the world's axes are no longer aligned! So, now the second rotation rot30x does exactly what it did in the local case, rotates the object 30 degrees around the world X axis, but the effect is to rotate the object through a cone around the world X axis since the object's X axis and the world's X axis aren't the same this time. What this looks like is that the prim pivoted 30 degrees around the world X axis, hence a global rotation.

Division of rotations has the effect of doing the rotation in the opposite direction, multiplying by a 330 degree rotation is the same as dividing by a 30 degree rotation.