To set the elements of the rotation type directly, compute the

The following function performs this in LSL:

rotation make_quaternion( vector axis, float angle ) { vector unit_axis = llVecNorm( axis ); float sine_half_angle = llSin( angle/2 ); float cosine_half_angle = llCos( angle/2 ); rotation quat; quat.x = sine_half_angle * unit_axis.x; quat.y = sine_half_angle * unit_axis.y; quat.z = sine_half_angle * unit_axis.z; quat.s = cosine_half_angle; return quat; }

This example duplicates the function

For a rotation around just the x-axis, the

Similarly, a rotation around just the y-axis or just the z-axis is also simpler to compute:

A rotation of angle 0 around any axis is represented the same way. Consider that the sine of 0 is 0, while the cosine of 0 is 1. As a result, no matter what axis is chosen, the null rotation is equal to

To find the inverse of a rotation, negate the last element of the

To find the inverse, negate the first 3 elements:

If you negate all 4 elements of a

"

This is so inappropriate. I understand why folks think this is funny, but come on. Why scare foks off? Quanternions aren't out of reach for most of us. Just takes a little patience.

Additional Resources:

Mathworld

Delphi3D

GameDev.net

Programming Rotations with Quaternions

Michel Isner's guide to Quaternions - arguably the best and clearest tutorial on rotation representations.

Rotation | Types

Comments [Hide comments/form]

For those of us who are too lazy ATM to think about quaternions for real, can we get some shortcuts verified?

How to express the net result of two quat rotations combined? Is it addition, multiplication, or what? With normal rotations, I can sum the triples. For instance, <PI, 0, 0> + <0, PI, 0> = <PI, PI, 0>

How can I "combine" quaternions?

How to express the net result of two quat rotations combined? Is it addition, multiplication, or what? With normal rotations, I can sum the triples. For instance, <PI, 0, 0> + <0, PI, 0> = <PI, PI, 0>

How can I "combine" quaternions?

-- GenePoole (2005-03-16 13:05:08)

rotation myRot = llGetRot(); vector degRot = <180, 0 ,0>; // Rotate 180 degrees on x-axis. degRot *= DEG_TO_RAD; rotation targQuat = llEuler2Rot(degRot); llSetRot(myRot * targQuat); // Rotate 180 degrees on global x-axis llSetRot(targQuat * myRot); // Rotate 180 degrees on local x-axis. llSetRot(targQuat); // Set the rotation to 180 degrees on the global x-axis.

You multiply them together.

For example, if you wanted to rotate 180 degrees on the global x axis, then 180 degrees on the global z-axis, you'd say:

rotation xRoted = llGetRot() * llEuler2Rot(<180, 0, 0> * DEG_TO_RAD);

rotation zRoted = xRoted * llEuler2Rot(<0, 0, 180> * DEG_TO_RAD);

llSetRot(zRoted);

Im not exactly sure how several multiplications in series work on rotations...

-- ChristopherOmega (2005-03-16 14:17:51)

I recall that

a * (b * c) == (a * b) * c

a * b != b * a

but only when

(a.x * a.x + a.y * a.y + a.z * a.z + a.s * a.s) == (b.x * b.x + b.y * b.y + b.z * b.z + b.s * b.s) == (c.x * c.x + c.y * c.y + c.z * c.z + c.s * c.s)

remember that SL is using 32-bit floats, each operation will result in inacuraccies being introduced.

see LibraryRotationFunctions

a * (b * c) == (a * b) * c

a * b != b * a

but only when

(a.x * a.x + a.y * a.y + a.z * a.z + a.s * a.s) == (b.x * b.x + b.y * b.y + b.z * b.z + b.s * b.s) == (c.x * c.x + c.y * c.y + c.z * c.z + c.s * c.s)

remember that SL is using 32-bit floats, each operation will result in inacuraccies being introduced.

see LibraryRotationFunctions

-- BlindWanderer (2005-04-02 00:52:01)

I've been reading all day about this, and I'm still confused.

Wikipedia has a pretty good page about some properties quaternions have that make them useful for rotations her. And the math is mostly clear to me.

But I'm confused about what each element in a quaternion represents, if it's possible to visualise it (something like the right hand thumb rule for Euler rotations). I've seen sites describe it as a vector pointing towards the target combined with a scalar for roll, but that doesn't seem to be true for SL? Or I simply failed at testing it.

Wikipedia has a pretty good page about some properties quaternions have that make them useful for rotations her. And the math is mostly clear to me.

But I'm confused about what each element in a quaternion represents, if it's possible to visualise it (something like the right hand thumb rule for Euler rotations). I've seen sites describe it as a vector pointing towards the target combined with a scalar for roll, but that doesn't seem to be true for SL? Or I simply failed at testing it.

-- StevenKramer (2006-07-30 11:53:51)

They are supposed to represent a rotation in four-dimensional space - so they are hard to visualise directly. You can, however, split the rotation up into its axis of rotation and angle rotated about that angle (using llRot2Axis and llRot2Angle). Quats arent meant to be visualized directly from their values - they are used primarily because of their efficiancy.

-- ChristopherOmega (2006-07-30 12:55:57)

The way I understand it... you have two components, an angle and a unit vector (axis). If the object you are rotating is a record, the angle is how many radians the record rotates, and the unit vector is the direction perpendicular to the plane of rotation, a stick poking through the center hole. You rotate the record the angle of rotation and tilt it to point the label in the direction of the unit vector.

-- AsiraSakai (2006-12-09 08:39:25)

I believe you are describing axis-angle notation Asira.

-- BlindWanderer (2006-12-10 11:55:16)

Attach a comment to this page: