Jump to content
  • Advertisement
Sign in to follow this  
homojedi

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.

If you intended to correct an error in the post then please contact us.

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


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


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


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


Link to post
Share on other sites
Quote:
Original post by homojedi
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);
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.

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!