Jump to content
• Advertisement

# OpenGL Rotating around a vector (or arbitrary axis)

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

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

##### Share on other sites
Advertisement
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

##### Share on other sites
I found this through a Google search. I'm not at my work computer so I can't compare it against the code I've written before, but it looks correct.

#### Share this 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

##### Share on other sites
Quote:
 Original post by TheAdmiralI 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10
• Advertisement

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×

## 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!