Driver AI in Solar2D

In my previous post I implemented a barebones map loader in order to load up tracks from the Tiled level editing tool.

This allowed us to drive our little car around a ‘track’ with the camera following us.

This is all well and good but doesn’t really make for a race, what we need are other cars driving around. And to do that we need AI, or at least my feeble attempt at a fake AI that is in no way meant to rival [ insert whatever current racing game there is as I’m way out of the loop in regards to modern games ].

Full disclosure, I do not know how to make AI, racing or otherwise, and to date my only proper published games are first-person adventure games and as thus have no AI ( or driving physics or cameras for that matter so this is all completely new to me ) so please understand that this is all just one way to do it and by no means the only way to do it, and definitely not the correct way to do it, it’s just my way.

With that out of the way, here goes.

The first step was to simplify the track from the previous post to a single circle ( ish anyway ) and set up some waypoints at each of the corners.

All the driver does is get the position of the first waypoint, get the angle to it and then determine if it should turn left, right, or straighten out. With the pedal all the way to the metal it then just drives towards it, once it hits it’ll then update the waypoint index to the next and repeat.

Very simple, very dumb. But it works.

One issue, of many, with this is that the driver will constantly overshoot the corners and hit the grass before being able to correct itself and return to the track.

To begin to address this I have added a ‘speed’ variable to each waypoint. Now when the driver is driving to the next waypoint it’ll also check the target speed against its own speed and then hit the brakes or gas until it matches.

This is by no means perfect but it will now take corners a little better, but it makes for a boring race.

To try to alleviate some of the cornering issues in this next version I have added a ‘distance’ variable to waypoints as well, so now the driver doesn’t have to hit bang on a waypoint but instead they’ll be considered hit as long as the driver is within whatever distance the waypoint has set.

We’re begin to get somewhere but it’s still a long way from anything resembling “good”, so the next step is to massage the race line a touch by adding some extra waypoints to smooth out the corners. In this next version I also turned off the debug display as having all the info about the waypoints was getting in the way of the actual track for demo purposes.

Beginning t look a little better now, maybe it’s time to throw in some extra drivers.

First issue here is they are all clones of each other hitting the exact same line on every corner. Predictability might be good for driver safety, but not for racing.

To alleviate this, I’ve added in a small margin so when getting the position of the next waypoint there is a small random buffer added to it. Just a slight difference but it means no two cars are targeting the exact same position.

Finally, let’s throw a human driver into the mix by swapping out the AI driver in the first car with a human one. You.

As you’ll see it’s quite easy to beat them because the waypoint speed variables are way too low, but I think this is enough for this post as I have to go do school pickup and I think any more HTML5 demos will probably completely kill the page.

In a future post I’ll tackle obstacle avoidance, driver stats, race position checks, among other things.

The full demo is available here and the DriverAI module is here.

Leave a Reply

Your email address will not be published. Required fields are marked *