# OpenGL openGL rotation help

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

## Recommended Posts

greeting, I'm currently a bit stuck on rotation's and its doing my head in as it should be relatively simply, but essentially i need an object i have to face a known point in space...its up orientation is not important just that it turns to face this point in space. I'm also using vectors, so i dont have any matrices holding and information on the objects current transformation. any help would be awesome cheers

##### Share on other sites
So you're looking for a rotation that transforms the look vector from its initial direction (i.e. where it points to when the object is not rotated) to its target direction (i.e. pointing towards the aimed location). If these 2 vectors are distinct, then you can use the cross-product to compute the normal of the plane of rotation and the dot-product to compute the angle between them. Then you have an axis/angle rotation representation which can be converted into any other rotation representation.

##### Share on other sites
okay i just attempted something in code and it didnt work out....okay...so. Your advice makes perfect sense

rotationAxis = CurrentLookVec X DesiredLookVec;

float angle =
CurrentLookVec X DesiredLookVec/ CurrentLookVec.mag * DesiredLookVec.mag;

this give me me the angle and the axis to rotate about...but i'm using glRotatef();

how do i get openGL to rotate about an axis i define?

EDIT: at the moment this is how im trying to calculate the rotation

glPushMatrix( );		Fvector look = Wanderer->get_pEntityVelocity( ) - Wanderer->get_pEntityPosition( );		float angleX = look.y/sqrtf(((look.y * look.y) + (look.z * look.z)));		angleX = acosf(angleX);		float angleY = look.x/sqrtf(((look.x * look.x) + (look.z * look.z)));		angleY = acosf(angleY);		glTranslatef( 0, 0, 0 );		glRotatef( RAD_TO_DEG( angleX ),1,0,0 );		glRotatef( RAD_TO_DEG( angleY ),0,1,0 );		glTranslatef( Wanderer->get_pEntityPosition( ).x, Wanderer->get_pEntityPosition( ).y, Wanderer->get_pEntityPosition( ).z );		glColor3f( 0,1,0 );		glutSolidCone( 1,2,20,20 );	glPopMatrix( );

[Edited by - homojedi on April 23, 2010 6:50:06 AM]

##### Share on other sites
glRotate(angle,axisX,axisY,axisZ);

That is how you get to rotate on an arbitrary axis.

##### Share on other sites
so....would i do this?

rotationAxis = CurrentLookVec X DesiredLookVec;

float angle =
CurrentLookVec . DesiredLookVec/ CurrentLookVec.mag * DesiredLookVec.mag;

glrotate(angle,rotationAxis.x,rotationAxis .y,rotationAxis.z);

##### Share on other sites
Quote:
 Original post by homojediso....would i do this?rotationAxis = CurrentLookVec X DesiredLookVec;float angle = CurrentLookVec . DesiredLookVec/ CurrentLookVec.mag * DesiredLookVec.mag;glrotate(angle,rotationAxis.x,rotationAxis .y,rotationAxis.z);
In principal: Yes. But there are 2 caveats. You may have overseen them or neglected beliberately, I don't know, so I tell you:

Let i be the initial look vector and d the desired one.

Then
a := i x d
gives the unnormalized axis of rotation. You can normalize it yourself
a / |a|
but when used as glRotate's x, y, z values, OpenGL will normalize the axis anyway.

Further, the angle between the both vectors is
β := acos( ( i . d ) / ( |i| * |d| )
which has to be converted to degrees, of course, before handed over to glRotate.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
17
5. 5

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

• Total Topics
631776
• Total Posts
3002299
×

## Important Information

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!