# Rotation, model and the center :(

## Recommended Posts

juglar    148
Hi guys!! i have a model in my 3d Space, and i want to rotate the object,around his center, in a coord diferent to 0,0,0 y use this ecuatios (the euler rotation) x' = x y' = (cos é * y) - (sin é * z) z' = (sin é * y) + (cos é * z) Rotation about the y axis: x' = (cos é * x) + (sin é * z) y' = y z' = -(sin é * x) + (cos é * z) Rotation about the z axis: x' = (cos é * x) - (sin é * y) y' = (sin é * x) + (cos é * y) z' = z but this not work it turn aroun the "world center" and not around the model center any advice on how to do that thanks in advance....

##### Share on other sites
Wolfdog    268
Could you do the rotations around the orign then translate back?

O = Object Position

x' = x
y' = (cos é * y-Oy) - (sin é * z-Oz) + Oy
z' = (sin é * y-Oy) + (cos é * z-Oz) + Oz

Edit: Just to be clear, x,y,z are the vertices that make up you object.

[Edited by - Wolfdog on October 13, 2007 10:19:47 PM]

##### Share on other sites
mmakrzem    1036
What you need to do is move your model "back" so that its center is located at 0,0,0. Then apply your rotation. Now move the model "back" to where it was originally.

If you create a scene graph structure all this "moving back" business is handled for you. Check out the videos on my website if you want to see it in action.

##### Share on other sites
juglar    148
mmm yes but, the problem is, my object is an object created in 3dsmax, so i have all the data olready stored, i put the object in the 0,0,0 of the world and i have the same effect... :(

##### Share on other sites
Wolfdog    268
Quote:
 Original post by juglarmmm yes but, the problem is, my object is an object created in 3dsmax, so i have all the data olready stored, i put the object in the 0,0,0 of the world and i have the same effect... :(
Can you explain what it is you are trying to do? I am very confused here.

Are you rendering this model in your own program with OpenGL/DirectX/Something Else? Or are you doing something different?

##### Share on other sites
juglar    148
Yes,i want to load a model, and rotate it in any part of the world around his own axis(local rotation ? ) an not in the word axis here is the code for made a rotation that i did it's extranger work fine por the rotation in X but no for the rest... any clue?
------ code -----
int i;
float new_z,new_y,new_x;

for(i=0;i<numPoints;i++){

if(anglex){
//new_x = points[i].x;

//pointsToCamera[i].x = new_x;
pointsToCamera[i].y = new_y;
pointsToCamera[i].z = new_z;
}
//transform y
if(angley){
//new_y = points[i].y;

pointsToCamera[i].x = new_x;
//pointsToCamera[i].y = new_y;
pointsToCamera[i].z = new_z;
}
//transform z
if(anglez){
//new_z = points[i].z;

pointsToCamera[i].x = new_x;
pointsToCamera[i].y = new_y;
//pointsToCamera[i].z = new_z;
}

}//for

------ /code -------------

##### Share on other sites
Wolfdog    268
Have you thought about using matrices? They could be used to clean all this up and have many great benefits.

Now there one obvious problem with this code. If you have more than two angles set you overwrite the other. For example if you set anglex and angley then you end up replacing all the changes of X because you don't take it into account.

I'll fix this real quick, but I think you might want to look around for basic examples of these things, nehe.gamedev.net is a good place to start and perhaps mmakrzem's own video tutorials might be helpful.
int i; float new_z,new_y,new_x;for(i=0;i<numPoints;i++){    new_x = points[i].x;   new_y = points[i].y;   new_z = points[i].z;    if(anglex){      new_y = cos(DEG_TO_RAD(anglex))*new_y - sin(DEG_TO_RAD(anglex))*new_z;      new_z = sin(DEG_TO_RAD(anglex))*new_y + cos(DEG_TO_RAD(anglex))*new_z;   }   //transform y   if(angley){      new_x = cos(DEG_TO_RAD(angley))*new_x + sin(DEG_TO_RAD(angley))*new_z;      new_z = -sin(DEG_TO_RAD(angley))*new_x + cos(DEG_TO_RAD(angley))*new_z   }   //transform z   if(anglez){      new_x = cos(DEG_TO_RAD(anglez))*new_x - sin(DEG_TO_RAD(anglez))*new_y;      new_y = sin(DEG_TO_RAD(anglez))*new_x + cos(DEG_TO_RAD(anglez))*new_y;   }    pointsToCamera[i].x = new_x;   pointsToCamera[i].y = new_y;   pointsToCamera[i].z = new_z; }//for

##### Share on other sites
juglar    148
:( it's was a very newby bug.... this happend when i program so much hours at night , thanks people for the help!! :), i will do it with matrix