# ABSTRACT 3D ROTATION (no trig)

NO THANKS to anybody out there, I have FINALLY derived a few SIMPLE equations to handle 3D abstract rotation. I had to take a quaternion class (class = object definition) that somebody made and go through it for an hour to pull out a couple equations.

HERE is a way to track rotation separately without modifying the original data, and WITHOUT USING TRIG:

A rotation is {w,x,y,z} (a quaternion), such that {x,y,z} is an axis and w is the angle of rotation.

A point is {x,y,z}.

To rotate a rotation {w1,x1,y1,z1} by another rotation {w2,x2,y2,z2}, the result is:

{ w1*w2 - x1*x2 - y1*y2 - z1*z2,
w1*x2 + w2*x1 + y1*z2 - z1*y2,
w1*y2 + w2*y1 + z1*x2 - x1*z2,
w1*z2 + w2*z1 + x1*y2 - y1*x2 }

The inverse of a rotation (used in the next equation) is:

{ w * 1/(w*w + x*x + y*y + z*z),
x * -1/(w*w + x*x + y*y + z*z),
y * -1/(w*w + x*x + y*y + z*z),
z * -1/(w*w + x*x + y*y + z*z) }

...and finally, to rotate a point by the rotation, do this:

1) make a new rotation like this (1=rotation, 2=point):
{ -x1*x2 - y1*y2 - z1*z2,
w1*x2 + y1*z2 - z1*y2,
w1*y2 + z1*x2 - x1*z2,
w1*z2 + x1*y2 - y1*x2 }

2) rotate that new rotation by its inverse
3) the rotated point is {x,y,z} from that new rotation (last 3 parts)

You always keep track of a rotation, and you only rotate a COPY of whatever you are using as the model.

EXAMPLES:
A rotation 5 degrees around the X-axis looks like: {5*180/PI, 1, 0, 0}
A rotation 5 degrees around the Y-axis looks like: {5*180/PI, 0, 1, 0}
A rotation 5 degrees around the Z-axis looks like: {5*180/PI, 0, 0, 1}
In other words, a rotation W degrees around the axis described by the point {X,Y,Z} looks like: {W*180/PI, X, Y, Z}

Note that rotation is in radians; degrees are converted to radians by going degrees*180/PI.

You can use anything besides degrees; just multiply it by half of whatever a full rotation is and multiply by PI (or a full rotation and 2*PI)

From what I can tell, quaternions tell what axis to rotate around to get to a position, rather than specifying what axis to rotate TO (that method is inaccurate, since you may need to rotate around that axis; that's two rotations!}. I don't know WHY quaternions work, they just do; the only draw back of quaternions is that they are supposed to be impossible to visualize what rotation it represents, or how it even finds the correct axis and stuff.

A quaternion is actually a complex number like a+bi, but it consists of
a + bi + cj + dk. Here are some of its properties:

i^2 = j^2 = k^2 = ijk = -1
ij = -ji = k
ik = -ki = j
jk = -kj = i

It's hard to understand.
Quaternions can also be used in various other applications like physics

• I forgot to mention; if you want to check this stuff out yourself, here is where I got my information (which I had to re-write and everything) (You have to do a google search for both):

Google: "CS184: Using Quaternions to Represent Rotation". It tells what operations to use and stuff, but it gets very confusing where it explains how to do the operations (I had to get that from the next site)

Google: "OOP Meets Algebra". This guy made a quaternion class to represent the rotation for a cube that rotates along 3 rotations that you can toggle on/off. (I had to go through function after function to pull out the correct equations).

In short, to rotate a point P by a quaternion q, you go q*P*q^-1 and ignore the w in the result; to rotate a rotation, just go q1*q2 (note, this is NOT the same as q2*q1!)
• A complex number can be written as

C = a + j*b (the classical form)

or in a bit more geometrical form as

C = A*cos(t+Phi) or C = A*cos(t) + j*sin(t).

pow(E,j*t*w) = cos(w*t) + j*sin(w*t) (w is greek omega )

So you can represent a complex number with a length and a angle as well.By converting a point in 2d-space to a complex number you can do rotation(angle-calculations) and possibly scaling(length-calculations,but im not sure with this).

If I remember right a quaternion has just more imaginar axis than complex numbers.so there should be ways to convert the classical form of a quaternion to one with angles and lengths.

Its highly possible (plz anybody correct me if Im wrong) that this library performs the multiplication at least one time with a cos/sin operation because each imaginary axis must stay independent from the others.

I think a better (and more understandable?) way for vector-calculations are the good,old school matrices.But its always good,to get new inspiration to see other ways to do the job
• NOPE! no trig! I don't know why it works, it just does.

There is NO TRIG involved. You just create those new arrays using those formulas, and it should work.
• Hmm,its indeed a bit complicated.Your first article says something about 'euler-angles'.Is it possible that this is the quaterion equalation of the euler-identity?

complex number CAN be written in a trigonometric way and the separate multiplacation of real and imaginary parts are a bit similar to matrices with angles.

Euler-Identity:

POW(E,j*x) = cos(x) + j*sin(x) = C

C is a complex number with the length 1 and an angle depending on x.

I will check a math-book about quaterions tommorow,its realy interesting.But I think they are mathematicly nearly the same as a 4x4-matrices with trig.-ops.
Complex numbers,however,have a lot to do with angles and trigonometry,they can be used to describe harmonical functions (sin/cos) with the E-function and in some other ways.

C1*C2 can be done in a lot of different ways,eachone giving a correct result.however,some ways are easier than other

• [b][red]This message was edited by gautam at 2004-4-27 10:11:12[/red][/b][hr]
Quaternions are pretty old in 3D games. The main reason for them is to avoid gimbal lock.

The link below pretty much answers most quaternions questions. Secondly if you can acquire Game Programming Gems 1, read the info about quaternions, it also show you mathematical formulas and derivations to a certain extent.

http://www.flipcode.com/documents/matrfaq.html

And umm to do rotation, sin, cos functions are pretty much used in quaternions.
http://www.flipcode.com/documents/matrfaq.html#Q56
http://www.flipcode.com/documents/matrfaq.html#Q57

And if you use DirectX, it has quaternion functions in its API. Creating them however is no problem.

: NO THANKS to anybody out there, I have FINALLY derived a few SIMPLE equations to handle 3D abstract rotation. I had to take a quaternion class (class = object definition) that somebody made and go through it for an hour to pull out a couple equations.
:
: HERE is a way to track rotation separately without modifying the original data, and WITHOUT USING TRIG:
:
: A rotation is {w,x,y,z} (a quaternion), such that {x,y,z} is an axis and w is the angle of rotation.
:
: A point is {x,y,z}.
:
: To rotate a rotation {w1,x1,y1,z1} by another rotation {w2,x2,y2,z2}, the result is:
:
: { w1*w2 - x1*x2 - y1*y2 - z1*z2,
: w1*x2 + w2*x1 + y1*z2 - z1*y2,
: w1*y2 + w2*y1 + z1*x2 - x1*z2,
: w1*z2 + w2*z1 + x1*y2 - y1*x2 }
:
: The inverse of a rotation (used in the next equation) is:
:
: { w * 1/(w*w + x*x + y*y + z*z),
: x * -1/(w*w + x*x + y*y + z*z),
: y * -1/(w*w + x*x + y*y + z*z),
: z * -1/(w*w + x*x + y*y + z*z) }
:
: ...and finally, to rotate a point by the rotation, do this:
:
: 1) make a new rotation like this (1=rotation, 2=point):
: { -x1*x2 - y1*y2 - z1*z2,
: w1*x2 + y1*z2 - z1*y2,
: w1*y2 + z1*x2 - x1*z2,
: w1*z2 + x1*y2 - y1*x2 }
:
: 2) rotate that new rotation by its inverse
: 3) the rotated point is {x,y,z} from that new rotation (last 3 parts)
:
: You always keep track of a rotation, and you only rotate a COPY of whatever you are using as the model.
:
: EXAMPLES:
: A rotation 5 degrees around the X-axis looks like: {5*180/PI, 1, 0, 0}
: A rotation 5 degrees around the Y-axis looks like: {5*180/PI, 0, 1, 0}
: A rotation 5 degrees around the Z-axis looks like: {5*180/PI, 0, 0, 1}
: In other words, a rotation W degrees around the axis described by the point {X,Y,Z} looks like: {W*180/PI, X, Y, Z}
:
: Note that rotation is in radians; degrees are converted to radians by going degrees*180/PI.
:
: You can use anything besides degrees; just multiply it by half of whatever a full rotation is and multiply by PI (or a full rotation and 2*PI)
:
: From what I can tell, quaternions tell what axis to rotate around to get to a position, rather than specifying what axis to rotate TO (that method is inaccurate, since you may need to rotate around that axis; that's two rotations!}. I don't know WHY quaternions work, they just do; the only draw back of quaternions is that they are supposed to be impossible to visualize what rotation it represents, or how it even finds the correct axis and stuff.
:
: A quaternion is actually a complex number like a+bi, but it consists of
: a + bi + cj + dk. Here are some of its properties:
:
: i^2 = j^2 = k^2 = ijk = -1
: ij = -ji = k
: ik = -ki = j
: jk = -kj = i
:
: It's hard to understand.
: Quaternions can also be used in various other applications like physics
:

• [b][red]This message was edited by chick80 at 2004-5-2 11:37:7[/red][/b][hr]
Sorry, but I cannot understand...

The code you posted is not so different from what I had proposed you here:

In fact all the quaternions equations you wrote subtend trigonometry and I cannot believe you understood quaternions (that are
4-dimensional vectors) and you don't understand trigonometry.

And, even if you don't understand trig and don't know how sin and cos work, using the equation I gave you you can do the same things much faster.

Btw, take a look here: http://www.gamasutra.com/features/19980703/quaternions_01.htm

nICO

[hr]
[italic]How beautiful, if Sorrow had not made sorrow more beautiful than Beauty itself.[/italic]
JOHN KEATS