# How do I calculate a corrective torque to remove rotation from my object (except for yaw)?

I am trying to calculate the torque to apply to an object to correct all rotation, except for the yaw (which is the direction the object is steering in). Here is what I have so far:

`````` //rotation angle
float halfAngle = Mathf.Acos(rigidbody.rotation.w);
float sinHalfAngle = Mathf.Sin(halfAngle);

//rotation axis
float x = rigidbody.rotation.x / sinHalfAngle;
float y = rigidbody.rotation.y / sinHalfAngle;
float z = rigidbody.rotation.z / sinHalfAngle;

Vector3 rotationAxisWithoutYaw = new Vector(x, 0, z);
float rotationAboutAxisWithoutYaw = ? //this is the critical step

//unimportant detail below...included so that you can see what I am trying to do
float desiredRotation = 0;
float displacementFromDesired = desiredRotation - rotationAboutAxisWithoutYaw;

float springTorque = -springConstant * displacementFromDesired;
float inertia = SomeFunctionOf(rigidbody.inertia, rotationAxisWithoutYaw);

float viscousDampingCoefficient = 2 * Mathf.Sqrt(inertia * springConstant);
float dampingTorque = -viscousDampingCoefficient * rigidbody.angularVelocity.z;
float totalTorque = springTorque + dampingTorque;

``````

As you can see, I can calculate the axis of rotation to use by ignoring the y-component of the actual rotation vector. But how do I calculate the right angle (without the yaw?) I am unable to visualise it properly, and am totally stuck.

If anyone has any ideas of how to calculate that angle, I would appreciate it!

ht

I ended up using calculations based on Euler angles (I normally dread using Euler angles; my experience with such techniques is bad. In this case though, it worked perfectly).

``````// For rolling
float angularDisplacementZ = rigidbody.rotation.eulerAngles.z;

if(angularDisplacementZ > 180)
{
angularDisplacementZ -= 360;
}

float springTorqueZ = -springConstant * angularDisplacementZ ;
float inertiaZ = SomeFunctionOf(rigidbody.inertia, rotationAxisWithoutYaw);

float viscousDampingCoefficient = 2 * Mathf.Sqrt(inertia * springConstant);
float dampingTorqueZ = -viscousDampingCoefficient * rigidbody.angularVelocity.z;
float totalTorqueZ = springTorque + dampingTorque;