Sign in to follow this  
Macmenace

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

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


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


Link to post
Share on other sites
Quote:
Original post by Ashaman73
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



Thank you so much. That worked perfectly!

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