Simple Car Controller
https://github.com/Sewmina7/SimpleCarController
This project was inspired by the lack of customization in unity's built-in wheel colliders. I wanted a fully functioning and flexible wheel physics. And of course I wanted to learn how real cars behave, So what's a better way to learn something other than implementing it on my own?
Enjoy this devlog of writing a Car Controller with my own wheel and suspension physics!
Suspension System
Theory
So I started by implementing the suspensions first. I approached this with how real car suspensions behave. Each wheel has a shock absorber spring anchored one side to the chassi of the car and the other to the wheel. So the chassi can stay afloat on the wheels via the springs.
In order to make the spring behave, I simply applied the Hooke's Law which describes how springs behave in real world.
So basically Springs length can be either shrunk or expanded by adding force. The change of the length is directly proportional to the added force. So the equation for Hooke's Law is like this
F= -kx F=Applied force, k=Spring Constant, x=change of Length
Implementation
To implement this in my car controller, I used a raycast from the wheel's center to the bottom of the wheel, So the raycast can detect if the wheel is touching the ground. If the wheel is not touching ground, the spring will expand to its max length until the wheel hits ground and if the wheel is touching ground then it will shrink to its smallest length.
So when the car is in the air, the spring is in its max length and when the car is on the ground, the spring is shrunk to its smallest length. Now we have the wheels connected to the chassi with a shock absorber but it doesn't apply any force yet so it can move freely.
Then I calculate the force to be generated by the shrinking of spring using its new length comparing with its natural length. Basically if the springs new length is less than its natural length, it will apply an upward force to the car's chassi on the position of spring's joint to the chassi which is proportional to the change of length. And I define a variable for the Spring constant. Higher the spring constant, higher the force will be applied to the chassi by the spring.
When the upward force of spring find its balance with gravitational pull from the chassi, the system becomes stable and the car starts floating on its wheels.
Problems
It didn't become stable after its balancing point. Reason? Spring reacts too fast for the system to settle down. Because its fast reaction, the system becomes a pogo stick. When at bottom, it gives highest upward force causing to reach the top, and wise-versa.
To overcome this issue, I added dampening to springs reaction. So instead of instantly pushing the spring in and out, Now it does it slowly, giving the system enough time to stabilize itself. This kind of dampening is found on real life suspensions as well. They put a viscous fluid inside the suspension tubes and make a narrow hole for it to flow when getting compressed, making a resistance which dampens the reaction.
Code Snip from Source.
When the Car is not moving, Combined friction forces from the wheels are equal to the forces applied on the car.

We start by implementing Kinetic friction.
We calculate Friction force for each wheel using the formula above, referencing current speed of the wheel, where the wheel is headed (for steering wheels) and how grippy the tyre is and the weight for each wheel. Weight for wheels are static for this project since this is an arcade car controller.
Code Snippet from Source:
With this in place, the car will slow down when pushed and will steer when the wheels are turned.
It's time for us to move and apply brakes.
Rolling Wheels
For this project, We don't need to simulate all of that. We are going to skip them and apply the force directly the wheel's contact points, to their facing direction.
Implementation
Then we need to make a power curve, X axis being Speed, and Y axis being Power. Since we don't have air resistance in our simulation, We workaround that with this power curve, As the car reaches its top speed (declared by a variable) the power it produces becomes lower, giving 0 power at top speed.
I added a Animation Curve for this power curve so I can adjust it easily in the editor.
Code Snippet from Source:
Applying Brakes
For our car controller, we only implement the braking force. Simplifying the simulation.
Implementation
Then we apply the brakes if the input is given, by applying a force which is opposite to the direction where the wheel is moving, multiplied by braking factor. So higher the wheel speed, higher the braking power.
When the wheel reaches 0 velocity, braking force will calculate 0 as well.
Code Snippet from Source:
Steering
Comments
Post a Comment