Jump to content
• Advertisement

# Orienting an object along a camera's view vector? (Quaternions)

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

Hello, I have a problem where I would like to orient an object along the camera's view vector. It appears two work when the camera and the object are moving to the right. (This is a third person camera) But, the object spins vigorously when moving to the left. The culprit function is one which takes the camera's view vector and sets the object's orientation using the axis angle theorem for quaternions. Here is the code, do you see anything obviously wrong?
  bool PhysicsWorld::OnPhysicsMoveWithCamera( IEvent* mEvent )
{
EPhysicsMoveWithCamera *eventCast = Utility::Polymorph< EPhysicsMoveWithCamera >( mEvent );
RigidBody *rb = GenePool::QueryComponent< RigidBody >( eventCast->objHandle );

if(rb)
{
Vector3 mAxis = eventCast->moveAxis;
mAxis.Normalize();

//Move the object linearly
rb->AddVelocity( mAxis * eventCast->velocity );

//Orient the object along the camera's view
Quaternion rot;
//Axis to rotate around
Vector3 y_Axis(0.0f, 1.0f, 0.0f);
//The objects front vector
Vector3 Front = rb->GetFrontVector();
Front.Normalize();

//Dot prod between obj's front and camera's view
float dot_product = mAxis.DotProduct( Front );
//Check dot pord is in bounds
if(dot_product > 1.0f || dot_product < -1.0f)
return false;

//Get angle between obj's front and camera's view
float theta = acos( dot_product );

//Rotate quaternion by y axis and angle(theta)
float sin_theta = sin( theta / 2.0f );
rot.i = y_Axis.x * sin_theta;
rot.j = y_Axis.y * sin_theta;
rot.k = y_Axis.z * sin_theta;
rot.r = cos( theta / 2.0f );
//Set the object's new orientation
rb->SetOrientation( rot * rb->GetOrientation() );
}

return false;
}


#### Share this post

##### Share on other sites
Advertisement
You only determine the rotation angle, but not the rotation direction (clock-wise or counter-clock-wise)
I mean this part:
//Get angle between obj's front and camera's view
float theta = acos( dot_product );

To determine your rotation direction use the right(X) vector of your object or camera. Something like this

//Get angle between obj's front and camera's view
float theta = acos( dot_product );
//The objects front vector
Vector3 Right = rb->GetRightVector();
Right.Normalize();
if(dot(Right,mAxis)>0) {
//rotate your object counter-clock-wise
theta = -theta)
} else {
//rotate your object clock wise, nothing to do
}

--
Ashaman

#### Share this post

##### Share on other sites
Quote:
 Original post by Ashaman73You only determine the rotation angle, but not the rotation direction (clock-wise or counter-clock-wise)I mean this part: //Get angle between obj's front and camera's view float theta = acos( dot_product );To determine your rotation direction use the right(X) vector of your object or camera. Something like this//Get angle between obj's front and camera's viewfloat theta = acos( dot_product );//The objects front vectorVector3 Right = rb->GetRightVector();Right.Normalize();if(dot(Right,mAxis)>0) {//rotate your object counter-clock-wisetheta = -theta)} else {//rotate your object clock wise, nothing to do}--Ashaman

Thank you so much. That worked perfectly!

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11
• Advertisement

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633677
• Total Posts
3013282
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!