Sign in to follow this  
Journey

OpenGL Rotating around a vector (or arbitrary axis)

Recommended Posts

I've been trying to rotate a vector about a certain axis but the functions I use implicitly rotate it about the vectors 1,0,0 (x) 0,1,0 (y) and 0,0,1 (z), the default ones. I know the openGL function glRotate() performs the rotation about a given vector. Anyone know how one would go about implementing one like that? These are the functions I'm using now

void C3DVector::RotateZ(double dDegrees_)
{

	double dRads = DEG_TO_RAD(dDegrees_);

	double dX = this->m_dX, dY = this->m_dY;
	
	this->m_dX = dX*cos(dRads) - dY*sin(dRads);
	this->m_dY = dX*sin(dRads) + dY*cos(dRads);
	
}// end void C3DVector::RotateZ(double dDegrees_)



void C3DVector::RotateY(double dDegrees_)
{

	double dRads = DEG_TO_RAD(dDegrees_);

	double dX = this->m_dX, dZ = this->m_dZ;
	
	this->m_dX = dX*cos(dRads) - dZ*sin(dRads);
	this->m_dZ = dX*sin(dRads) + dZ*cos(dRads);
	
}// end void C3DVector::RotateY(double dDegrees_)


void C3DVector::RotateX(double dDegrees_)
{

	double dRads = DEG_TO_RAD(dDegrees_);

	double dY = this->m_dY, dZ = this->m_dZ;
	
	this->m_dY = dY*cos(dRads) - dZ*sin(dRads);
	this->m_dZ = dY*sin(dRads) + dZ*cos(dRads);
	
}// end void C3DVector::RotateX(double dDegrees_)



Share this post


Link to post
Share on other sites
It's just one big conjugate transformation:

Rotate & translate everything so that the axis of rotation is mapped to one of the coordinate axes, rotate about this axis, then inverse-transform back to the original coordinate space.
You could do this in the three steps as described, or you could get out some paper, write out the overall transformation, simplify, and use this in your implementation, for a more efficient result.

At least, that's the way I'd do it, being a mathematician. I suppose the computer scientist's solution would be to Google for an existing solution and copy it [rolleyes].

Regards
Admiral

Share this post


Link to post
Share on other sites
Check the Matrix and Quaternion FAQ on this very site.

Or just generate the appropriate quaternion from axis/angle formulation, which is very easy (calculate angle / 2, take sin/cosine, multiply axis by that much and use the consine for the angle).

Share this post


Link to post
Share on other sites
Quote:
Original post by TheAdmiral
I suppose the computer scientist's solution would be to Google for an existing solution and copy it [rolleyes].
Ouch - that's pretty cynical :-)

@The OP: The formula to rotate a vector about another (unit-length) vector is fairly straightforward. It looks like Zipster's link covers it, although the article does have a glaring error in that it confuses coordinate system handedness with vector orientation :-/

The vector rotation formula can also be expressed as a matrix-vector multiplication. This is what the OpenGL function glRotate() does - it constructs a matrix representing this operation and concatenates it with the matrix at the top of the currently active OpenGL matrix stack.

Share this post


Link to post
Share on other sites
Here is some code from my math library that returns the rotation matrix around an arbitrary axis.


Matrix4 Matrix4::MakeRotateArbitrary( Vector3& axis, float radian ) {
float fCos = Math::Cos( radian );
float fSin = Math::Sin( radian );
float fSum = 1.0f - fCos;

axis.Normalize();
Matrix4 m;

m[0][0] = ( axis.x * axis.x ) * fSum + fCos;
m[0][1] = ( axis.x * axis.y ) * fSum - ( axis.z * fSin );
m[0][2] = ( axis.x * axis.z ) * fSum + ( axis.y * fSin );
m[0][3] = 0.0f;

m[1][0] = ( axis.y * axis.x ) * fSum + ( axis.z * fSin );
m[1][1] = ( axis.y * axis.y ) * fSum + fCos;
m[1][2] = ( axis.y * axis.z ) * fSum + ( axis.x * fSin );
m[1][3] = 0.0f;

m[2][0] = ( axis.z * axis.x ) * fSum + ( axis.y * fSin );
m[2][1] = ( axis.z * axis.y ) * fSum + ( axis.x * fSin );
m[2][2] = ( axis.z * axis.z ) * fSum + fCos;
m[2][3] = 0.0f;

m[3][0] = 0.0f;
m[3][1] = 0.0f;
m[3][2] = 0.0f;
m[3][3] = 1.0f;

return m;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this