🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Instead of glRotatef?

Started by
7 comments, last by Bruno 23 years, 11 months ago
HI Does anyone has a formula that alows me to rotate a point in 3d? I know that if i want to rotate in the x axis, i only move at the Y and Z axis, but how? To rotate in the Z axis, i use this: rotated_x = x * cos(angle) - y * sin(angle); rotated_y = y * cos(angle) + x * sin(angle); How about rotation in x? thanks Bruno
Advertisement
I was looking around for the same type of thing, i wanted to work out multiple transforms myself and call glMultMatrix() once. i used altavista (i think) and searched for matrix & quaternions and came up with a really good faq located at:
http://www.flipcode.com/documents/matrfaq.html

here are the matrices for rotations from it.

Q28. How do I generate a rotation matrix in the X-axis?
-------------------------------------------------------

Use the 4x4 matrix:

| 1 0 0 0 |
M = | 0 cos(A) -sin(A) 0 |
| 0 sin(A) cos(A) 0 |
| 0 0 0 1 |


Q29. How do I generate a rotation matrix in the Y-axis?
-------------------------------------------------------

Use the 4x4 matrix:

| cos(A) 0 sin(A) 0 |
M = | 0 1 0 0 |
| -sin(A) 0 cos(A) 0 |
| 0 0 0 1 |


--------------------------------------------------
Q30. How do I generate a rotation matrix in the Z-axis?

Use the 4x4 matrix:

| cos(A) -sin(A) 0 0 |
M = | sin(A) cos(A) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |


hope that helps a bit (by the way the actual y rotation in the faq is wrong (i think))

alistair

(btw, how does the formatting on this forum work?)

Edited by - alistair b on August 4, 2000 7:35:57 PM
Thanks for the links..
I'' ve extracted this formula from there, but it doens''t seems to work.. Can you show me how do you do it?
x1 = x;
y1 = (y*cos(90))+(z*sin(90));
z1 = (y*cos(90))-(x*sin(90));
When you use cos or sin, You should change degree to radian.

For degrree a, radian ra is a*PI/180.f (PI = 3.14159265).
Now you can use cos(ra) or sin(ra).

Ok., i''ve converted the angle to radians, but it''s still not working.., any ideias???

Thanks
Bruno
ok, first when you're doing any of this make sure you do matrix multiplication so that:
x' = m00*x + m01*y + m02*z + m03*w
y' = m10*x + m11*y + m12*z + m13*w
z' = m20*x + m21*y + m22*z + m23*w
w' = m30*x + m31*y + m32*z + m33*w

then you should get:
---------------------------------------------
X-rotation:
x' = x
y' = y*cos(A) - z*sin(A)
z' = y*sin(A) + z*cos(A)

Y-rotation:
x' = x*cos(A) + z*sin(A)
y' = y
z' =-x*sin(A) + z*cos(A)

Z-rotation:
x' = x*cos(A) - y*sin(A)
y' = x*sin(A) + y*cos(A)
z' = z
----------------------------------------------

and remember if you had glRotatef(A, ....)
now A=A*pi/180.0 (ie work out radians)

hope that helps (the problem is normally getting the signs right)


alistair

i just checked these, and they work in my code

Edited by - alistair b on August 5, 2000 7:49:34 AM
Ok alistair, do you mind looking at this code and see what the hell i''m doing wrong?? Because it''s still not working!
Some objects indeed look like they were rotated correctly, but others are a real mess.
I know that the problem it''s not the world, because if a take the lines tempy,tempz, and make just apont->y=mapi.y and apont->z=mapi.z, and the just call glRotatef(-90,1.0,0.0,0.0) in the drawing routine everything appears correctly.

float Rad(float angle)
{
float PI = 3.14159265;
angle = (angle*PI)/180;
return angle;
}

do{
fread(&mapi, sizeof(bb3d), 1,ftp);
apont->x = mapi.x;
tempy = mapi.y * cos(Rad(-90)) - mapi.z * sin(Rad(-90));
tempz = mapi.y * sin(Rad(-90)) + mapi.z * cos(Rad(-90));
apont->y = tempy;
apont->z = tempz;
apont=apont->prox;
}while!(feof(ftp));

thanks
Bruno
well if the object loading code works without the sin/cos rotations, i think the thing to do is load the object, then apply the rotation separately because i''ve taken the bit out of your code & it works...i tried it using nehe''s lesson2:

float A=0;
float x, y, z;
float tempx, tempy, tempz;

GLvoid doRotation(GLfloat x, GLfloat y, GLfloat z, GLfloat A)
{
tempx = x;
tempy = y * cos(Rad(A)) - z * sin(Rad(A));
tempz = y * sin(Rad(A)) + z * cos(Rad(A));
}

int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f);

A++;
glPushMatrix();
glBegin(GL_TRIANGLES); x=0.0f, y=1.0f, z=0.0f;
doRotation(x, y, z, A);
glVertex3f( tempx, tempy, tempz);
x=-1.0f, y=-1.0f, z=0.0f;
doRotation(x, y, z, A);
glVertex3f( tempx, tempy, tempz);
x=1.0f, y=-1.0f, z=0.0f;
doRotation(x, y, z, A);
glVertex3f( tempx, tempy, tempz); glEnd(); glPopMatrix();
}

it is a bit messy, but it demonstrates that there''s no problem with the rotation.

sorry i can''t help more than that, i''m not much good with pointers and ''->'' stuff yet...

alistair
hi.

just thought I''d pop my head in and recommend Computer Graphics: Principles and Practice, 2nd ed by James D. Foley (ISBN:0201848406). you can get it at Amazon.

btw; a good shelf-neighbour to this book would be Calculus and Analytic Geometry by Edwards and Penney (ISBN:0137363311) which is also found at <a href="http://www.amazon.com/exec/obidos/ASIN/0137363311/qid=965724406/sr=1-21/102-2839595-9804942>Amazon

This topic is closed to new replies.

Advertisement