PDA

View Full Version : Local rotation



Sansus
02-20-2002, 12:45 PM
Do you know any method to do LOCAL rotations? I want to rotate my object around the 3 local axis. I know this was a topic before, but it couldn't help me out; because you spoke about the method of incremental angles to aproximate local rotations when using small angle increments. But I don't like that method. Do you know any one else???!! Those who have made a ship game o else, how did you do that???

Majik
02-20-2002, 01:15 PM
I'm not too sure exactly what you want. But I'll take a guess at what I think you might be asking.

You have an object who's point of rotation is NOT at <0, 0, 0>. Say the point of rotation is at <19, 56, -9> then translate the object by the inverse of the translation <-19, 56, 9>, perfrom the rotation and then translate the object back to it's original position.


Ofcourse, you would be wanting to rotate around a vector which is completely different.

Osku
02-20-2002, 01:31 PM
Hello, this might work:

glPushMatrix();
glLoadIdentity();
..
Rotate and draw your objects..
..
glPopMatrix();

Osku

Sansus
02-20-2002, 01:32 PM
Thanx, but that is not what I asked. I want to rotate my object around the three LOCAL AXIS not around an arbitrary point.
Sorry my english, maybe it confused you.

Mr.Freeze
02-20-2002, 02:10 PM
The following routine may help you: you just give it 2 points (for the rotation axis) and an angle: it will build a rotation matrix around the specified segment. If the specified segment is identical to one of your local axis, it will build the rotation matrix around that local matrix. Is it what you want??

void buildRotationMatrixAroundSegment(float m[4][4],float angle,
float x0,float y0,float z0,float x1,float y1,float z1)
{
buildIdentityMatrix(m);
x1=x1-x0;
y1=y1-y0;
z1=z1-z0;
if ((x1==0)&&(y1==0)&&(z1==0)) return;
if ((x1==0)&&(y1==0))
{
if (z1<0) angle=-angle;
buildZRotationMatrix(m,angle);
return;
}
if ((x1==0)&&(z1==0))
{
if (y1<0) angle=-angle;
buildYRotationMatrix(m,angle);
return;
}
if ((y1==0)&&(z1==0))
{
if (x1<0) angle=-angle;
buildXRotationMatrix(m,angle);
return;
}
float r=(float)sqrt(pow(x1,2)+pow(y1,2)+pow(z1,2));
x1=x1/r;
y1=y1/r;
z1=z1/r;
float theta=(float)atan2(y1,x1);
float psi=(float)acos(z1);
float m0[4][4];
float m1[4][4];
float m2[4][4];
buildTranslationMatrix(m2,-x0,-y0,-z0);
buildZRotationMatrix(m1,-theta);
multiply(m0,m1,m2);
buildYRotationMatrix(m2,-psi);
multiply(m1,m2,m0);
buildZRotationMatrix(m2,angle);
multiply(m0,m2,m1);
buildYRotationMatrix(m2,psi);
multiply(m1,m2,m0);
buildZRotationMatrix(m2,theta);
multiply(m0,m2,m1);
buildTranslationMatrix(m2,x0,y0,z0);
multiply(m,m2,m0);
}

(multiply(m0,m1,m2) performs : m0=m1*m2)

Sansus
02-20-2002, 02:39 PM
I am not sure if this is what I want. Your function only makes a rotation matrix around a segment (the same what does glRotatef, isnt it?). I want to:

RotateLocalXAxis(rotx);
RotateLocalYAxis(roty);
RotateLocalZAxis(rotz);

I want that RotateLocal*Axis() function or a method to rotate around the local axis.

Mr.Freeze
02-20-2002, 03:16 PM
With that function you can do any rotation around any axis. I think that your problem is just the ordering of the transformation matrices. I'm not sure to understand exactly, but let's take an example:

We have a serie of transformations:

Ttotal = T3 * T2 * T1 * T0

origin = Ttotal * (0,0,0)
xAxis = Ttotal * (1,0,0)
yAxis = Ttotal * (0,1,0)
zAxis = Ttotal * (0,0,1)

If you want to rotate around xAxis of angle alpha:

buildRotationMatrixAroundSegment(xAxisRot,alpha,or igin.x,origin.y,origin.z,
xAxis.x,xAxis.y,xAxis.z);

and then your new total transformation becomes: TtotalNew = xAxisRot * Ttotal

TtotalNew was rotated of angle alpha around the original xAxis.

What exlse would you want to do if not that??

Cheers

richardve
02-20-2002, 03:40 PM
cMat44 obj, view, m;

// Rotate around the objects LOCAL axis
obj.setAxisAngle(10.f, cVec3(0.f, 1.f, 0.f));

// For each object
m = view * obj;

glLoadMatrix(m);


Or do it the OpenGL way:




glLoadIdentity();

// Set view (camera)
glRotate(-..);

// For each object
glPushMatrix();
// Rotate around the objects LOCAL axis
glRotatef(..);
glPopMatrix();



(do I make sense? http://www.opengl.org/discussion_boards/ubb/smile.gif)