maandag 21 mei 2012

Computer Controlled Cars

Dear readers,

Now that Katie Commando is finished and live, I've been on a new racing game project. This racing game will have computer controlled cars. The question is how do we get these cars to be controlled by the computer? In this blog post I'll tell you how we do it.

Getting the car to drive on any road is quite difficult, therefore we ask the track creator to create waypoints that follow the road. Below you see an image of the road with waypoints (shown in green).
We now program the car to follow these way points. We keep track at which waypoint the car is. Then we pick a point on the next way point (shown as a blue dot in the image below). If this point is to the left of the car we make the car steer left. And if it's on the right, we make it steer right. There are a lot of subtleties here, but this is what is basically comes down to.
As for the speed, a car cannot drive at full speed the whole track: in a corner it must slow down or it will go off track. So we compute an advised speed for every waypoint (which is based on the curvedness of the road). If the car is above this speed it should let go off the gas or brake depending on how much the speed difference is. If the car is below the advised speed it should accelerate.

We should also have a fall-back in case the car gets stuck somewhere. Therefore if the car has not passed the next way point within 2 or 3 seconds we assume something is wrong and we reset the car back on track. Of course this shouldn't happen too often (if it does then the car steering should be adjusted).

-- Stijn

Geen opmerkingen:

Een reactie posten