0
104 May 11, 2010 at 01:23

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?

#### 12 Replies

0
102 May 11, 2010 at 05:24

General axis rotation is very simple with quaternions.

0
101 May 11, 2010 at 09:45

Yes, just use quaternions, and it simply works.

0
104 May 11, 2010 at 11:39

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

0
102 May 11, 2010 at 11:48

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

0
104 May 11, 2010 at 12:05

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?

0
102 May 11, 2010 at 12:17

Yeah, I told you it’s simple (:

0
101 May 11, 2010 at 15:15

@rouncer

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

http://topmost.se/personal/programming/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.

0
101 May 11, 2010 at 15:50

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

0
104 May 11, 2010 at 16:17

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.

0
101 May 11, 2010 at 22:09

@rouncer

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 B). And when you learn how things work, you’ll learn to use those functions better and more efficiently.

0
104 May 11, 2010 at 22:35

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.

0
101 May 26, 2010 at 10:34