Sign in to follow this  
reaperrar

Quaternion LookAt function

Recommended Posts

Trying to rotate my character to face a certain point with a quaternion though he jitters as if overshooting the required rotation and having to rotate backward and forth.

Here's my code:

[CODE]
//Get Direction to target
Vec3 oNewDirection = a_oTargetPosition - GetWorldTranslate();
oNewDirection.Normalize();

//Get axis perpendiculr to forward dir and target dir to rotate around
Vec3 oRotationAxis = m_oDirection_Forward.Cross(oNewDirection);

//Get angle
float fRotationAngle = acos(Clamp(m_oDirection_Forward.Dot(oNewDirection), -1.0f, 1.0f));

//Invert angle if neccessary
if (m_oDirection_Right.Dot(oNewDirection) < 0.0f)
{
fRotationAngle *= -1.0f;
}

//Create new quaternion to rotate this object
Quaternion oTempQ = Quaternion(fRotationAngle, oRotationAxis);

//Rotate this objects quaternion by the new quaternion to face target
m_oRotation = m_oRotation * oTempQ;

//Normalize
m_oRotation.Normalize();

//Set Rotation
SetRotate(m_oRotation);
[/CODE]

Is my logic correct?

Share this post


Link to post
Share on other sites
I don't think you have to invert the angle if it's larger than pi/2. Other than that, at first inspection I don't see anything wrong with your code.

Perhaps you can capture the numbers from a situation where the code seems to be misbehaving, and then debug your code with that input. Edited by alvaro

Share this post


Link to post
Share on other sites
I think the lookat function isn't working,,

Example...
The original dot product between the forward and new direction will equal:
0.99895191
The dot product between the new forward (after rotation is applied) and new direction then equals:
0.99904567

So it rotate towards but doesn't end up snapping to the new direction, the dot product should equal 1 after...

EDIT: Solved, wasn't normalizing the rotation axis result of the cross product. Edited by reaperrar

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