Can anyone drive a car and a tank?

If you’ve never driven a sports car around a track, then add it to your bucket list, it’s fun! A tank would be a cool experience but bring the ear defenders and give it a miss if you are claustrophobic. With this posting, I give “AI” the chance to try them out.

What we’ll cover in this post is:

  • Explore creating a brain that learns to control both a car & tank.
  • Explore the test environment things can be configured (neural network, track, sensors etc).
  • Discussing how you can replace the AI with a formula.

What we won’t cover:

  • All of the code base because it is just a little too big (5k lines of executable code, out of 16k) to paste in a post and snippets won’t really help you quite as much as the explanations should.

If you don’t want to wait until the end of the post, the source code is on GitHub.

I’ve split this post into logical pages. Some of the animated GIFs are larger than I’d like, sorry about that.

Background

Although this is not my first post, it was the first of my just for “fun” apps back in February 2022, and it all started after my Google feed had an article on someone who’d used AI to drive a car.

Curiosity got the better of me and I started with a 2D polygon-shaped track with some blobs in place of cars. A few hours later, I had trained blobs.

Fancy attempting to build one yourself? Go ahead, try it now before I ruin the fun on pages 2 & 3!

It took a lot more effort to create the environment than the AI part. You might wonder why I chose a polygon – primarily because the track needs to be a loop but also because it’s easy ideal for working out which car is best.

I hope you’re thinking that “blobs” are not up to my usual graphics standard because that means you’ve read my other posts. Maybe you’d prefer go-karts or tanks? And how about some realistic physics? What about a track editor?

Will the below effort do, with telemetry showing the racing line the AI chooses, and red arrows indicating the loss of traction/g-force, sliding their back ends? Just say it – “that’s more what we expect, Dave!“.

Yes, it has real physics and to save me pointless effort I found this, from which I used some of the “physics” maths, so a shout out and credit to @spacejack. I combined it with the maths explanation on this page.

It’s not my flawed programming that the cars appear to drive on top of each other – they are, believe it or not, all racing on their “own” track and learning. If I drew each on a separate track, it’s not exactly going to animate quicker.

The basic AI/ML (whatever you want to call it) can be quite flexible and versatile. I was genuinely surprised how it was able to go around a track with such a tiny neural network.

It left me with so many questions.

Like, how many neurons does it take to steer a car?

Write down your answer. I will answer it shortly.

To help answer them, I built the feature-rich app now shared on GitHub. For example, you can observe in real-time (below) what the car sees (in the black-and-white world it lives in) and LIDAR beam(s) to give you an idea of what the car is doing. Due to the configured length of the LIDAR beam, and unbeknown to the car it is going too fast, it doesn’t have sufficient time to adjust the course.

But versatility doesn’t end there, it can drive tanks too! Sorry, authenticity doesn’t include Javelins or jack-in-the-box turrets. Nor does it include trees that jump in front of the tank, or people sitting on the side of it.

I am not kidding, instead of steering, it learns to use the two throttles (you can see in the tank visualisation, that it uses full left throttle, and cuts right throttle to turn).

I personally like the Tank that spins on the spot at the start line!

Tanks speeding around a track

There’s a lot going on above.

I have also provided a track constructor (nice neat edges) and track drawing tool (for inflicting horrible angles to push it to the limit like below):

Being feature-rich, a number of aspects might be of interest, but to avoid polluting the core AI/ML topic, I have moved them to other pages.

For example, If you like the speedo above, I’ve covered the drawing/animation of it in this post.

Links to other bits will be provided as we go.

As this is meant to be an AI/ML blog, page 2 will dive straight into that aspect.

Related Posts

Leave a Reply

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