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;
rigidbody.AddTorque(rigidbody.transform.forward * totalTorque);
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