I'm trying to match an agents orientation to the targets orientation but I seem to missing something. As the agent turns (vectors/seeks towards the target) the orientation doesn't match. The agent is doing something but not what I want. It's as if I need to include the current vector of the agent in the equation but I'm not sure how I would do that in this case.
I don't think subtracting quaternions has the meaning you are looking for.
If you have the target orientation, can't you set that directly on the agent? Make the agent have the same orientation as the target. If the target has some orientation offset initially, you can account for that too.
fassihi wrote:I don't think subtracting quaternions has the meaning you are looking for.
If you have the target orientation, can't you set that directly on the agent? Make the agent have the same orientation as the target. If the target has some orientation offset initially, you can account for that too.
According to the Ogre Wiki Quaternion and Rotation Primer adding and subtracting quaternions is exactly like adding and subtracting vectors. Now, the primer could be wrong - I'm not an expert in 3D math.
Quaternion and Rotation Primer wrote:
Vector3::getRotationTo(Vector3) returns a quaternion describing a relative rotation from one vector to the next. Remember that quaternions can be thought of as vectors with an angle attached? Remember that Vector A minus Vector B gives Vector C, a relative vector from B to A? Same thing with quaternions. Think of orientations as absolute, relative to the identity. Think of rotations as relative, usually relative to the current orientation.
Just setting the orientation is no good. We are using a physics engine. Just setting the orientation will cause the agent to "snap" to the orientation and by pass the physics engine.
If you check the same page on OGRE wiki about Quaternions, there is a table which shows the equivalent quaternions for different rotations. From the table you can see very well that adding quaternion values or subtracting the values does not work. The quaternions work with Cosine values and can not be numerically added if we are looking for concatenating rotations.
If you want to add two rotations using quaternions, you would need to multiply them together.
In your case, one thing you can do is have the front vector of the two objects and then call : Quaternion orientation_difference = VecA.getRotationTo(VecB);
This would give you the orientation which would rotate VecA towards VecB.
The Slerp function of quaternions can be used too, that would give you a part of the difference in their orientations, could be used if you are looking for a smooth turn.
//agent.x --> x position of the agent...
Ogre::Vector3 directionXY = Ogre::Vector3(agent.x - target.x, agent.y - target.y, 0.0);
directionXY.normalise();
//origin direction
Ogre::Vector3 vec = Ogre::Vector3::UNIT_Y;
//rotate from the origin direction to the target
Ogre::Quaternion quat = vec.getRotationTo(directionXY);
Ogre::Radian mRotationScalar = quat.getRoll();
Thank you all for your assistance. I finally got it to work. Here is the code for those who visit this thread later.
Realize that we are using a physics engine (newton) and are trying to utilize the same input controls that humans use. We use a scalar, -1.0 to +1.0 for torque and 0.0 to +1.0 for thrust, to determine how much of the "engine controls" to apply. Therefore it doesn't matter if the the input is keyboard, mouse, joystick or computed (i.e. AI) the input to our physics system is always a scalar that represents the amount power/torque to apply. For example: