Jump to content
  • Advertisement
Sign in to follow this  
neiluk

3D Rotation Problem

This topic is 4465 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

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;

		// Convert angle to radians
		angle = angle/RAD2DEG;

		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;

		// Convert angle to radians
		angle = angle/RAD2DEG;

		// 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!