max_jenkins

11-12-2002, 09:21 AM

Hi, I'm trying to write my own rotation code to handle rotation of my own vector structure. I got the mathematical transformations on the internet, and came up with these three functions:

void rot_x(vec_p a, GLfloat r)

{

a->y = a->y*cos(r) - a->z*sin(r);

a->z = a->y*sin(r) + a->z*cos(r);

a->x = a->x;

}

void rot_y(vec_p a, GLfloat r)

{

a->z = a->z*cos(r) - a->x*sin(r);

a->x = a->z*sin(r) + a->x*cos(r);

a->y = a->y;

}

void rot_z(vec_p a, GLfloat r)

{

a->x = a->x*cos(r) - a->y*sin(r);

a->y = a->x*sin(r) + a->y*cos(r);

a->z = a->z;

}

where vec is structure of {GLfloat x, y, z}.

These seem to work, the problem is, after many rotations the vectors seem to "lose their shape"...they become shorter and shorter until they no longer have any magnitude. It's almost as if they "lose" a bit of vector on every rotation, cumulatively adding up to a decrease in size. Does anyone know what this is caused by? I thought it might be innaccuracy caused by using GLfloat instead of doubles, or something in the sine/cosine functions. Any fix would be greatly appreciated.

void rot_x(vec_p a, GLfloat r)

{

a->y = a->y*cos(r) - a->z*sin(r);

a->z = a->y*sin(r) + a->z*cos(r);

a->x = a->x;

}

void rot_y(vec_p a, GLfloat r)

{

a->z = a->z*cos(r) - a->x*sin(r);

a->x = a->z*sin(r) + a->x*cos(r);

a->y = a->y;

}

void rot_z(vec_p a, GLfloat r)

{

a->x = a->x*cos(r) - a->y*sin(r);

a->y = a->x*sin(r) + a->y*cos(r);

a->z = a->z;

}

where vec is structure of {GLfloat x, y, z}.

These seem to work, the problem is, after many rotations the vectors seem to "lose their shape"...they become shorter and shorter until they no longer have any magnitude. It's almost as if they "lose" a bit of vector on every rotation, cumulatively adding up to a decrease in size. Does anyone know what this is caused by? I thought it might be innaccuracy caused by using GLfloat instead of doubles, or something in the sine/cosine functions. Any fix would be greatly appreciated.