Archived

This topic is now archived and is closed to further replies.

Translate/Rotation - Basic

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a set of position and rotation data of a model and I need to rotate it 90 around the x axis. I have an understanding of the concepts of what i need to do. I need to translate to the origin, perform the rotation, and translate back to my position. I have working translate and rotate functions from a math lib and they are defined as follows, they work in similar fashion to glrotate and gltranslatef void mat4_Translate(float x, float y, float z, mat4_t m) void mat4_Rotate(float angle, float x, float y, float z, mat4_t m) My main problem is im not sure the sequence and usage of these functions I copy all of my data into a matrix, like so. m[0] = t.rot[0][0]; m[1] = t.rot[0][1]; m[2] = t.rot[0][2]; m[3] = 0; m[4] = t.rot[1][0]; m[5] = t.rot[1][1]; m[6] = t.rot[1][2]; m[7] = 0; m[8] = t.rot[2][0]; m[9] = t.rot[2][1]; m[10] = t.rot[2][2]; I assume in then simply use the functions in the following fashion // Translate to Origin mat4_Translate(t.pos[0]*-1, t.pos[1]*-1, t.pos[2]*-1, m); // Perform rotation along X axis mat4_Rotate(90, 1, 0, 0, m); // Translate back to orginal position mat4_Translate(t.pos[0], t.pos[1], t.pos[2], m); The result i'm getting however, is that the objects seem to swing along a wide arc. Which make me think that they are not in fact being rotated about the origin. So my problem is here // Translate to Origin mat4_Translate(t.pos[0], t.pos[1], t.pos[2], m); What is the correct usage of this, in order to transle back to the origin Thanks [edited by - DMonaghan on November 26, 2003 3:37:00 PM]

Share this post


Link to post
Share on other sites
of course they are, if your math functions work like the transformations in opengl (side from which they are multiplied).

they work in local space, simply rotating will rotate around the point you currently are and the current "right" of your object. if you do the translations (btw. why x*-1 instead of -x?) you go to the world origin, rotate around your local "right" and then translate back along your NEW axes, so theres no way you could end up in the same position as before.

if just rotating screws your position store it and then replace that part of the matrix after the rotation yourself. if you want to rotate around the worlds "right" you rotate around the x vector of the transposed matrix.

what i use is this (you should be able to just replace opengls rotate with your math libs rotate.. also i removed some calls to loadmatrix etc.):


void CameraGL::move(float x, float y, float z, bool global, float distance) {
if (global) {
Transform[12] += x * distance;
Transform[13] += y * distance;
Transform[14] += z * distance;
}
else {
Transform[12] += distance * (x*Transform[0] + y*Transform[4] + z*Transform[8]);
Transform[13] += distance * (x*Transform[1] + y*Transform[5] + z*Transform[9]);
Transform[14] += distance * (x*Transform[2] + y*Transform[6] + z*Transform[10]);
}
}

void CameraGL::rotate(float deg, float x, float y, float z, bool global) {
if (global) glRotatef(deg,
x*Transform[0] + y*Transform[1] + z*Transform[2],
x*Transform[4] + y*Transform[5] + z*Transform[6],
x*Transform[8] + y*Transform[9] + z*Transform[10]);
else glRotatef(deg, x,y,z);
}

Share this post


Link to post
Share on other sites