# -= OGL translate & rotate around an axis =-

This topic is 4311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I have a problem. I can't solve this: for example: translate an object to (1,2,0) then rotate around its horizontal center axis. what's the solution? please help me in this vc++ code: http://people.inf.elte.hu/suranyi/ogl/nehe/main_eltol.cpp THANKS

##### Share on other sites
We won't do your homework for you.

However, I will tell you that all rotations happen around the point (0,0,0), so you'll need to perform the rotation before you perform the translation.

##### Share on other sites
i see.

but my problem is:
the axis is : (x,0,0)
and i want the axis to be : (x,0.5,0)

???

##### Share on other sites
Quote:
 Original post by samprass123but my problem is:the axis is : (x,0,0)and i want the axis to be : (x,0.5,0)

Why is this a problem? Could you explain, please?

##### Share on other sites
Look up "arbitrary axis rotations" or a similar term in a decent linear algebra or geometry book (like this one).

Basically, what you'll want to do is construct a matrix C that transforms the object into a coordinate system where the desired axis of rotation is the Z axis of the coordinate system, applying a rotation of the desired amount around the Z axis (call this matrix R) and then undoing the coordinate transform.

Constructing C is a simple as computing an orthonormal basis using your rotation axis as the initial vector. R is just the canonical "rotation about the Z axis" matrix. The final transformation is:

v = C * R * inverse(C)

As others have said you must rotate before you translate.

##### Share on other sites
Hey samprass,

If you understand matrix math, this is a trivial problem. If you're in a graphics class, understanding the operations of matrices and how they map into 3D space is crucial to understanding computer graphpics; trust me, I tried learning graphics without knowing the math, and it bit me in the behind.

Look up how to do a rotation around an arbitrary axis with a matrix, or create a rotation matrix. For the translation, look up a translation matrix. There are plenty of computer graphics resources on the web that will explain this sort of thing.

Hope that helps,
Aviosity

EDIT: Whoops, looks like jpetrie got there before I did, with a better explanation.

##### Share on other sites
void point_3::rotate_x(const float &radians){	float t_y = y;	y = t_y*cos(radians) + z*sin(radians);	z = t_y*-sin(radians) + z*cos(radians);}

##### Share on other sites
Thanx for help.

i've solved the problem.

but there is an other!

if i rotate the object with glRotatef(90,1,0,0) around axis x, with angle 90, and then rotate with glRotatef(90,0,1,0) around axis y, with angle 90, the object rotated not around the y, but the z axis!!!

i think, that the coord.system is rotated too, and it is the problem...

help me pls to solve this!

THANK