# Calculate the leaning angle

I have an object moving around, let's say it's a cycle. In cycle dynamics, when the object takes a turn, it often leans into the direction of the turn. I would like to simulate this.

I'm using the equation: atan(v^2 / r * g), where v is the current velocity, r is the radius of the turning circle and g is the gravitational force. I get r with v^2 / a, where a is the current acceleration.

However it doesn't seem to work right. In some cases it leans to the right direction, in others, it leans to the opposite, meanwhile the leaning should always be inwards.

I've determined the direction where the acceleration force pulls the object with sign(velocity.y * acceleration.x - acceleration.y - velocity.x) and multiplied the leaning with the result, but it has no effect.

What am I missing?

To calculate how an object leans when it turns, you need a lot of things, such as: the center of mass, the points of support (in the case of a vehicle these are the outer edges of the wheels), the suspension strength, the speed, the friction coefficient of the road and the wheels, etc. Due to this, you'll have to fake it to some extent, but even that doesn't make the mathematics simple, just simpler.

So, we can assume the vehicle is a box, the center of mass is in the exact middle. You need to define the friction coefficient (a value between 1 and 0, 0 is frictionless and the car can't lean, 1 makes the car stop immedialitely), you need to know the radius of the turning circle and the mass of the object.

Let's look at the actual formula:

Fcf = mrw²


Fcf is the centrifugal force, m is the mass of the object (in kilograms), r is the radius of the turn (in meters) and w (should be a lowercase omega) is the angle speed (in 1 / second). We can calculate the last by getting the angle the object rotates in a second in radians and divide it by 2π, or if it is in degrees, then divide it by 360°.

With this we can calculate the centrifugal force's strength. This force always points outwards from the center and applies to the center of mass.

We can then calculate the strength of the frictional force with the following formula:

Ff = μFcf


Where Ff is the frictional force, μ (mu) is the friction coefficient and Fcf is the centrifugal force.

This force doesn't apply to the center of the mass however, this always applies to the lowest point in the object under the center of the mass. This force difference is why the vehicles lean. The center of the mass is trying to go to somewhere, but the base is going in the other direction. What happens is basically the same as when you hit someone in the stomach from the left and you swipe his leg to the right in the same time (it's literally the same), then he will fall over to the left.

We need to calculate the rotational force the frictional force makes. If you use an engine with a good physics engine in it, then I highly recommend just applying these two forces to the object at the right places, but if you don't, here's how you calculate the rotation:

First let's calculate the force, which is marked by τ (tau). Because the force vector is always in 90 degrees to the vector between the center of the mass and the base of the object, so the formula is a bit simpler than usual. The length of τ is

τ = rF


Where r is the distance from the center of mass and the base and F is the force, or in this case the frictional force.

Then we can calculate the angular velocity, τ = Iα where I is the moment of inertia (I'm going to get to this in a minute) and α (alpha) is the angular velocity.

We need to first calculate I, which is

I = mr²


Where m is the mass of the object and r is the distance between the center of the mass and the base.

With this you can calculate α. Now you need to take this and every frame add it to the angular velocity of the vehicle, then you add that to the angle of the vehicle, and you should get yourself a leaning vehicle.

But you also need it to go back to normal, when you stop. This can be done by calculating the same thing with the gravity instead of the friction, making the center the edge on which the vehicle "stands" instead of the center of the mass and applying the force in the cebter of the mass instead.

That should also get you an angular velocity in the other way. You should divide this from the velocity of the vehicle, if it's currently leaning.