# general axis rotation

12 replies to this topic

### #1rouncer

Senior Member

• Members
• 2725 posts

Posted 11 May 2010 - 01:23 AM

I read on the internet that this can be accomplished by a few x y and z rotations and a couple of translations.

An algorithm (see Figure 1):

(1) translate space so that the rotation axis passes through the origin

(2) rotate space about the $z$ -axis so that the rotation axis lies in the $xz$ -plane

(3) rotate space about the $y$ -axis so that the rotation axis lies along the $z$ -axis

(4) perform the desired rotation by $\theta$ about the $z$ -axis

(5) apply the inverse of step (3)

(6) apply the inverse of step (2)

(7) apply the inverse of step (1)

So I could just go and type that in and just see if it works, because I cant really understand it... But I was wondering, does anyone have some C code that does this I can convert into my program- Just so I can be sure its going to work?
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #2JarkkoL

Senior Member

• Members
• 475 posts

Posted 11 May 2010 - 05:24 AM

General axis rotation is very simple with quaternions.

### #3geon

Senior Member

• Members
• 939 posts

Posted 11 May 2010 - 09:45 AM

Yes, just use quaternions, and it simply works.

### #4rouncer

Senior Member

• Members
• 2725 posts

Posted 11 May 2010 - 11:39 AM

Can someone give me the simple implementation then? :) please?
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #5JarkkoL

Senior Member

• Members
• 475 posts

Posted 11 May 2010 - 11:48 AM

You first create quaternion for the rotation and then convert it to 3x3 matrix (just google for quaternion to matrix conversion). If v=the axis you want to rotate about, and a=the angle, then the quaternion for the rotation q=[v*sin(a/2), cos(a/2)]

### #6rouncer

Senior Member

• Members
• 2725 posts

Posted 11 May 2010 - 12:05 PM

So all I gotta do is convert it to a matrix and presto I can use it to transform with? any vector?
To make myself clearer, you just a) create the quaternion b) declare the rotation c) convert it to a matrix and Ive got it?
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #7JarkkoL

Senior Member

• Members
• 475 posts

Posted 11 May 2010 - 12:17 PM

Yeah, I told you it's simple (:

### #8geon

Senior Member

• Members
• 939 posts

Posted 11 May 2010 - 03:15 PM

rouncer said:

Can someone give me the simple implementation then? :) please?

http://topmost.se/pe...ming/3dmath.zip

There you go.

It's pretty much a vanilla C++ implementation of vector, matrix and quaternion mathematics. What's special about this implementation is the matrix class, which is OpenGL compatible, and that vectors are multiplied by matrices, not the other way around.

### #9.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 11 May 2010 - 03:50 PM

You *could* devise it yourself if you know how to multiply two quaternions and the fact that a point p rotated around quaternion q equals q * [p, 0] * q-1. Simply write that out on a piece of paper and you know what to put in each element of the matrix
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

### #10rouncer

Senior Member

• Members
• 2725 posts

Posted 11 May 2010 - 04:17 PM

Thanks everyone. Maybe if I was a genius like you .oisyn...

 void set_rot_axis(float a, vector axis)
{
axis.normalize();

float c=(float)Math.cos(a);
float s=(float)Math.sin(a);
float t=1.0f-(float)Math.cos(a);

this.m[0][0]=(t*axis.x*axis.x)+c;this.m[1][0]=t*axis.x*axis.y+s*axis.z;this.m[2][0]=t*axis.x*axis.z-s*axis.y;this.m[3][0]=0;
this.m[0][1]=t*axis.x*axis.y-s*axis.z;this.m[1][1]=t*axis.y*axis.y+c;this.m[2][1]=t*axis.y*axis.z+s*axis.x;this.m[3][1]=0;
this.m[0][2]=t*axis.x*axis.y+s*axis.y;this.m[1][2]=t*axis.y*axis.z-s*axis.x;this.m[2][2]=t*axis.z*axis.z+c;this.m[3][2]=0;
this.m[0][3]=0;this.m[1][3]=0;this.m[2][3]=0;this.m[3][3]=1;
}


Thank you drive through. lol

When it comes to rotating things decently, you have to have the general axis rotate, you cant do anything with skeletons without it.

I downloaded that math library anyway, probably proove useful. thanks.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #11.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 11 May 2010 - 10:09 PM

rouncer said:

Maybe if I was a genius like you .oisyn...
Well I'm certainly no genius, but I also wasn't being a smug either if perhaps that's what you think

No, I was serious. Everyone with a *bit* of math skills, which I assume you have at least given the projects that you undertake, should be able to write down what actually happens to all the 4 components of the quaternions in the equation. In the end, you want to have them as

r = q * p * q-1
r.x = p.x * ... + p.y * ... + p.z * ... + p.w * ...
r.y = ...
r.z = ...
r.w = ...

And hey, that looks exactly like a vector-matrix multiplication, with p being the vector input, the coefficients of the components of p being the matrix you actually want to know, and r being the vector result.

Naturally, if you have written this down you still want to take an existing implementation because an error would be hard to track down , but I find that these kinds of things really help you understand the mathematical constructs you actually use but have always been abstracted away because you were using prefab textbook functions . And when you learn how things work, you'll learn to use those functions better and more efficiently.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

### #12rouncer

Senior Member

• Members
• 2725 posts

Posted 11 May 2010 - 10:35 PM

Im actually serious when I call you a genius.

It doesnt matter how full on a guy gets into technical details, hes always too modest to say hes quite good at what he does, in fact, better than a lot of others.

Everythings easy in the end, its just a matter of how much you managed to pack inside your head to date, which makes you a genius or not... and you sir are a damn genius!

That truly is amazing. thankyou.
you used to be able to fit a game on a disk, then you used to be able to fit a game on a cd, then you used to be able to fit a game on a dvd, now you can barely fit one on your harddrive.

### #13.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 26 May 2010 - 10:34 AM

Yes, that was already mentioned. See http://www.devmaster...75755#post75755. Please read the topic before posting.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users