# Complex behaviour from a few simple rules

Often its easy to assume a computer controlled agent is working under some highly complex and “intelligent” rules. However as boids shows us that need not be the case. In the video despite a twisty complex path to follow and more cars than their should be in a small space, all the cars don’t end in a big pile pushing against each other…

There are just two rules in play here, the first is to drive towards a waypoint, when the car gets within 6 metres of the waypoint the next waypoint in the path is selected. Using just this rule with a few spread out cars is fine, but something more is needed.

Each car checks the distance of every other car, all the cars in a radius contribute a steering force away from themselves which is divided by half the distance between the cars. Despite the fact the cars have slightly different acceleration profiles, this allows cars to run side by side while still following the path – you can see this at about 2:24 in the video. When encountering a temporary jam of cars, a car will often circle around for another go, or even continue on the path just past the jam.

Its worth considering the steering algorithm I implemented, rather than the car heading and atan2 of the relative positions, I decided to use vectors, one for the car heading and the other just a simple look at vector, the dot product of these two vectors can be directly used as the steering angle (the physics engine handles maximum steering lock) Although this algorithm sounds more complex it’s actually easier to implement as you don’t get issues with angles wrapping etc…

Given an additional set of profiles that describe for example how a particular car modifies its speed for corners, and you could probably get away with just that for a believable racing game…

Complexity is an interesting thing and often emerges from the simplest set of rules, worth remembering!

Enjoy!