How can I rotate a point around a given axis?

Started by
1 comment, last by Zorodius 21 years, 5 months ago
I'm having a tough time with this. I must have looked at over a hundred pages so far, and I have yet to reach a solution. All I want to do is rotate a point around a given axis (specified as a vector, so it's always going to pass through the origin), ie rotate P around (1, -1, -1). My knowledge of trigonometry is extremely minimal and exclusively self-taught, so that's not helping any Here's what I currently have. It only seems to work correctly for certain angles - seemingly mostly ones that lie on the axes, or at 45 degree angles to them. If you can spot something that screams out as being wrong with this, please mention it. Alternately, feel free to ignore the code entirely and just point me in the right direction to solve this problem, that'd be great too
    // Rotate around an arbitrary axis.

void Rotate(float theta, triple axis)
{
	float a, b;
	
		if(theta == 0.0f)
			return;
			
	Normalize(&axis);
	
	// Rotate around the X axis so that the given axis lies in the XZ plane.

	a = RadianAngle(triple(0.0f, axis.y, axis.z), triple(0.0f, 0.0f, 1.0f));	// Angle between the vector's projection

															// on the YZ plane and the Z axis.

		
	RotateX(a);
	RotateX(&axis, a);
	// Rotate around the Y axis to align the given rotated axis with the Z axis.

	b = RadianAngle(axis, triple(0.0f, 0.0f, 1.0f));
	RotateY(b);
	// Perform the theta rotation around the Z axis.

	RotateZ(theta);
	// Undo the previous two rotations.

	RotateY(-b);
	RotateX(-a);
}
    
triple is a structure, triple(a,b,c) is a constructor that creates a floating point triple with those values, RotateX/Y/Z is an overloaded function that will multiply the global transformation matrix by whichever axis rotation matrix if given a floating-point number (angle), or will rotate just a vector around whichever axis if given a pointer to a triple and a floating-point number (angle.) Again, feel free to ignore the above entirely and just suggest where I can find out how to do it the right way I sincerely appreciate any help you guys can give me with this problem [edited by - Zorodius on November 12, 2002 2:50:31 PM]
You don't need to vote for the "lesser of two evils"! Learn about Instant Runoff Voting, the simple cure for a broken democracy!
Advertisement
Well, Zorodius, you''ll be happy to know that I have found the solution to your problem, and will post it here for the benefit of people besides me, myself, and I who might come across this post while suffering through a similiar problem and searching for an answer, in the event that the search function ever returns.

There is a matrix for rotating around an arbitrary axis.
The matrix is presented below.

    |tx²+c   txy+sz  txz-sy  0|R = |txy-sz  ty²+c   tyz+sx  0|    |txz+sy  tyz-sx  tz²+c   0|    |0       0       0       1| 


Where...
(x,y,z) = unit vector to rotate around
theta = desired angle of rotation
c = cos(theta)
s = sin(theta)
t = 1 - cos(theta)

Yay!
You don't need to vote for the "lesser of two evils"! Learn about Instant Runoff Voting, the simple cure for a broken democracy!
Another Solution to your problem is Quatrations, You can learn them here under Articals & Resources. I find them Simple to use If you make a class for them and Just use them like that They have some other neat uses that may come in handy.
"I seek knowledge and to help those who also seek it"

This topic is closed to new replies.

Advertisement