Rotation is driving me INSANE!!! HELP!!!

Started by
4 comments, last by _coDer_ 23 years, 7 months ago
Well, thought that subject would make you look here... Now to the problem: I''m trying to write a 3D editor, such as QOOLE for quake, etc... I made a CFace3 class (which is a triangle), and a CFace4 (which is a quad, derived from CFace3). Each CFace can move, stretch itself, and find its centre point. I use a matrix to stretch/move them There''s also a CPolygon class which stores arrays of CFace''s, and is sort of a message router, when u move something on the X axis, it goes through its own CFace3 array and calls CFace3::MoveX(), then through its CFace4 array and calls CFace4::MoveX() for each CFace4. Now I''m up to making them rotate (well have been up to this for about a week and a half!). I (for some weird reason) didnt make a rotate function for the CFace''s, but only in CPolygon, which creates a rotation matrix depending on the axis you are rotating on, and multiplies each vertex of each CFace3/4 by this matrix. Now, problem is, if the CPolygon isnt centred at (0,0,0), when you rotate the polygon, it sorta orbits this origin (Its obvious to me why, that''s not my question ). To make it rotate each CPolygon properly, I made it find the centre of the CPolygon, move it by - of that value (which effectively moves it to (0,0,0)), and then rotated it, and then moved it back to where it came from... This sorta works, except that it doesnt ... Somehow a rounding error or something creeps in somewhere, (I''m suspecting the GetCentrePoint() function of the CPolygon) and after a few rotates, it doesnt centre itself on (0,0,0) any more and it starts the orbiting thing again. Does anyone know of any other way to rotate the polygons so they dont orbit but rotate about their own centre point??? Its been driving me mad for over a week now, no sleep, eyes red, etc... I just cant think of any other way to rotate them other than moving to the origin then back. Here''s my code for making a rotation matrix: void Mat4::CreateRotation(float angle, bool xRot, bool yRot, bool zRot) { float ca = cos(angle*PI_OVER_180); float sa = sin(angle*PI_OVER_180); float x = 0;//pt.getX(); float y = 0;//pt.getY(); float z = 0;//pt.getZ(); if(xRot==true) { m[0][0]*=1; m[0][1]*=0; m[0][2]*=0; m[0][3]*=0; m[1][0]*=0; m[1][1]*=ca; m[1][2]*=-sa; m[1][3]*=0; m[2][0]*=0; m[2][1]*=sa; m[2][2]*=ca; m[2][3]*=0; m[3][0]*=0; m[3][1]*=0; m[3][2]*=0; m[3][3]*=1; } if(yRot==true) { m[0][0]*=ca; m[0][1]*=0; m[0][2]*=-sa; m[0][3]*=0; m[1][0]*=0; m[1][1]*=1; m[1][2]*=0; m[1][3]*=0; m[2][0]*=sa; m[2][1]*=0; m[2][2]*=ca; m[2][3]*=0; m[3][0]*=0; m[3][1]*=0; m[3][2]*=0; m[3][3]*=1; } if(zRot==true) { m[0][0]*=ca; m[0][1]*=-sa; m[0][2]*=0; m[0][3]*=x; m[1][0]*=sa; m[1][1]*=ca; m[1][2]*=0; m[1][3]*=y; m[2][0]*=0; m[2][1]*=0; m[2][2]*=1; m[2][3]*=z; m[3][0]*=0; m[3][1]*=0; m[3][2]*=0; m[3][3]*=1; } } Maybe the rotate matricies are wrong??? Any ideas would be greatly appreciated
Advertisement
I don''t know if I totally understood your description _coDer_ but I think it has something to do with the point you are using as the CPolygon origin in world space.

"To make it rotate each CPolygon properly, I made it find the centre of the CPolygon, move it by - of that value (which effectively moves it to (0,0,0)), and then rotated it, and then moved it back to where it came from..."

If you want to rotate something on the spot (an object in the game world say) you definitely have to move it to the world origin, rotate it, then move it back to where it was, but make sure that the object origin that is specified in world space coordinates is always the same. Unless you are trying some fancy new effect specify this point once instead of calculating the centre based on the polygons position (I am just guessing at what might be happening).

If you are doing everything else correctly it sounds like your object origin is moving in the world, which is causing the object to "orbit".

Post again if I''m barking up the wrong tree.
Paulcoz.
Hey, a fellow Aussie answered!

Well yeah, that''s what I''m trying to do, move the object to the origin, rotate it and move it back, (origin being (0,0,0) in worldspace coords). But the origin isnt found properly or something (CPolygon::GetCentrePoint() isnt returning the EXACT origin, cause of some sort of rounding error <== I''m guessing that), so after a few rotates on the x,y or z axis, the GetCentrePoint() function returns something that''s prolly 0.02f off, so after some more rotates, the object begins to orbit and move around instead of turning on the spot, sorry, I''m not so good with describing things, but I think its clearer now
Well, this whole proccess depends on the fact that you know where the center of the face is. What you could do, is hold two values for each vertex (excluding the center, of course). One is in world coords (relative to origin), and one is in local coords (relative to center of face). When you want to rotate, use their local coords to make the rotation, and then add them to the world coords. Just make sure the local and world coords are kept current. If you think its a rounding error, just increase the precision of the variables holding the data. like float to double, etc.

=======================================
A man with no head is still a man.
A head with no man is plain freaky.
Just to clarify what has been said, you either have to:

(a) obtain the centre-point of the polygon, and move this to the world origin (which is just converting the vertices to local coordinates). Rotate the object, then move it back to where it was OR

(b) store the polygons vertices in local coordinates to begin with. Rotate these, then tranform them to the world (by specifying the location of the local origin in world coordinates).

I prefer method (b) myself, because you specify the local origin in world space once, and always use that fixed value (instead of re-calculating it).

Paulcoz.

PS. Where in Oz are you?

Edited by - paulcoz on September 4, 2000 4:49:45 AM
Thanks guys, couldnt get on the net for a few days, meanwhile I worked out the same thing you ppl said about storing local coords... I did it storing local coords for the poly, and a centre point, then when I draw I just add the centre point to each vertex and have the world coord. Oh and PaulCoz - I''m in Melbourne.

This topic is closed to new replies.

Advertisement