Jump to content
  • Advertisement
Sign in to follow this  
Macmenace

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

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


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


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
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!