• 14
• 15
• 10
• 10
• 9

# 3D Rotation Problem

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

## Recommended Posts

Im getting a werid problem with my code to rotate objects in 3D. 1. rotations about the Y axis always rotate the object about its local Y axis even if they follow several rotations about the z axis. 2. rotations about the Z axis rotate the object about its local z-axis if there have been no prior y axis rotations but they seem to veer off course if there have been prior y-axis rotations. Im doing it like this
void RotateY(float angle)
{
rotation.y += angle;

float x = faceing.x;
faceing.x = faceing.z*sin(angle) + x*cos(angle);
faceing.z = faceing.z*cos(angle) - x*sin(angle);

x = up.x;
up.x = up.z*sin(angle) + x*cos(angle);
up.z = up.z*cos(angle) - x*sin(angle);
}

void RotateZ(float angle)
{
// Increment object z rotation
rotation.z += angle;

// Update object orientation vectors
float x = faceing.x;
faceing.x = (faceing.y*sin(angle) + x*cos(angle));
faceing.y = faceing.y*cos(angle) - x*sin(angle);

x = up.x;
up.x = (up.y*sin(angle) + x*cos(angle));
up.y = up.y*cos(angle) - x*sin(angle);
}


up and faceing are unit vectors pointing along the objects local axis. The object model is rotated using glRotatef() calls which work fine, the problem is that the camera is aligned behind the object using the faceing vector, which is incorrect. Im guessin i need to change some of the signs of the cos and sin components in the RotateZ function but i cant figure out which (have tried with trial and error)

##### Share on other sites
any ideas anyone...?

##### Share on other sites
I not sure what exactly your math is doing there, but your y-rotations look weird to me. Normally I see clockwise rotations about the z-axis written like:

x2 = x1*cos(theta) + y1*sin(theta)
y2 = -x1*cos(theta) + y1*cos(theta)

Equilvalently for rotations about the y axis:

z2 = z1*cos(theta) + x1*sin(theta)
x2 = -z1*sin(theta) + x1*cos(theta)

However, the first part of your y-rotation code,

float x = faceing.x;
faceing.x = faceing.z*sin(angle) + x*cos(angle);
faceing.z = faceing.z*cos(angle) - x*sin(angle);

is equivalent to:

z2 = z1*cos(angle) - x1*sin(angle)
x2 = z1*sin(angle) + x1*cos(angle)

which has the signs of the sin terms reversed, effectively rotating in the opposite direction from what is presumably intended.

Maybe that's part of the problem?

##### Share on other sites
Hi thanks, but that wasnt the problem, think id left that in from when I was trying out differnt combinations lol have changed it back now. Have uploaded the source code and executable so maybe if anyone fancies a look they can see the problem in describing. Its in a zip file here: http://geocities.com/neilsdemos/MoutainFighter.zip

To see the problem, first use the up and down arrows to tilt the plane up/down, this works fine, next uses left/right arrows, it again rotates correctly. Now use up and down again and the camera drifts sideways as well as up :S (also x to accelerate, z to brake but arnt needed to see the problem)

Thanks to anyon who takes a look. PS. the rotation code is in the file Object.h