This site uses strictly necessary cookies. More Information

X- Home /

# 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;

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

**Answer** by Herman-Tulleken
·
Sep 06, 2010 at 09:09 AM

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;

rigidbody.AddTorque(rigidbody.transform.forward * totalTorque);

//now do the same for pitching (use X instead of Z)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Clamp Bike Rotation 0 Answers

Bike stay upright based on surface normal 1 Answer

Trying to rotate my catapult's launch arm. 1 Answer

Constrain Rigidbody Y Axis Rotation in World Space 2 Answers