# Torsional Springs

Linear Springs are usually covered in high school physics class. Linear springs move you back to a desired rest position. However, what if you have a desired rest rotation or direction. This is where Torsion Springs come in.

### Math Background

Torsional springs are very similar to Linear Springs. The equation for a second order linear spring is the following:

F = k * x + d * v

Where F is the force, k is the spring stiffness constant, x is the distance from the desired position, d is the spring damping constant, and v is the difference between the current velocity and the desired velocity. The mathematical expression for torsional springs have a one-to-one mapping to a linear spring, as follows:

T = q * r * a1 + p * w * a2

Where T is the torque, q is the spring stiffness constant, r is the distance from the desired rotation in degrees, p is the spring damping constant, and w is the difference between the current angular velocity and the desired angular velocity. Also, a1 is the axis of rotation and a2 is the axis of rotation for the angular velocity. Note that a1 and a2 are not necessarily the same axis of rotation vector. Furthermore, if there is no desired angular velocity, then w is equal to zero and the expression simplifies to T = q * r * a1.

As you can see, the torsional spring expression is extremely similar to the linear spring expression. You just need to measure the rotation in degrees and also choose the correct axes of rotation.

### Code Implementation

The following function computes the required torque to apply to the game object.

`Vector3 getTorsionalSpringTorque (                                Vector3 axisOfRotation,                                 Vector3 axisOfAngularVelocity) {    float r_ij = desRot — currRot;    float w_ij = desAngVel — currAngVel;    Vector3 springTorque =         Q * r_ij * axisOfRotation         + P * w_ij * axisOfAngularVelocity;    return springTorque;}`

If you want to apply this torque to the game object, there are a variety of ways to do so. You can either call an applyTorque() function, or you can calculate the angular acceleration via torque’s equivalent to F=ma :

Where T is the torque, I is the inertia tensor, and z is the angular acceleration. Since I is a tensor, you need to take its inverse to calculate the angular acceleration. Once you compute the angular acceleration, you can add the computed angular acceleration to the game object’s current angular velocity.

### Applications

See the next article for how to keep your game character standing.