woensdag 15 juni 2011

Car physics

Dear readers,

It's been a while since I wrote. In the meantime I've been working on some racing games about which I cannot say much. What I can tell you about is how our cars are (roughly) implemented. First let's briefly explore an idea, evaluate it and then improve upon it.

We start with creating a rigid body: a physics object with properties such as mass, friction, linear velocity, etc. This rigid body collides with other physic objects such as our track. So let's create a rigid body (without the wheels for simplicity of explaining) with the shape of a car as shown below (sliding on ground):
Sliding and hovering
To move the car forward we can impulse the car in the direction it is facing. And to turn left or right we can apply an angular impulse. Tada! Done! Well no, not quite. One issue for example is that the car would regularly spin around wildly as it is moving over the track. This is because the car is constantly colliding with the track and the physic engine reacts upon the collisions with unwanted results.

To avoid the collisions we make the car hover over the track instead of sliding on it. One way to do this is by shooting a ray from the center of the car straight down (red arrow) and when the track is within the hover distance we impulse the car back up (countering gravity). In this way collisions are avoided (and we get a nice bounce effect as well) but when driving over the track we'd still have collisions when driving up a slope. We can avoid this by shooting more rays: from the four places of the wheels (blue arrows). We could even take this idea further and shoot multiple rays per wheel as shown below, but shooting rays is not for free and requires CPU cycles.
Shooting multiple rays per wheel
This is a rough and simplified explanation of how our cars work and it works quite well for our needs.

-- Stijn

Geen opmerkingen:

Een reactie posten