openGL rotation help

Started by
4 comments, last by haegarr 13 years, 11 months ago
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
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.
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]
glRotate(angle,axisX,axisY,axisZ);

That is how you get to rotate on an arbitrary axis.
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);
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.

This topic is closed to new replies.

Advertisement