C# Monogame, trying to create a 2D racing AI

511 views Asked by At

So I have been tasked in making a 2D top-down racing game over the summer for college, and I have been dreading doing the AI but it is finally time. I have googled many different ways of the same thing just to try and find a person asking the same question but it seems everyone uses Unity over Monogame.

So I have an "enemy" car which can accelerate (as in slowly speeds up to top speed), decelerate and steer left and right. I have got the actual car the player drives working fine but the game is boring when the player isn't racing against anyone. All I need is a very basic AI which will follow a path around the course and will readjust if it gets knocked or something happens to it. I don't even know where to start, Please help!!! Please let me know if you need any more details.

2

There are 2 answers

1
Jacob K On

What you need to implement would be dependent on how complex, of course, your AI needs to be. If all it needs to do is readjust its steering and monitor its speed, a basic AI car could at a given time step...

  1. Accelerate if not at top speed
  2. Decelerate if cooling down from a boost
  3. Steer away from the track boundaries
  4. Decide whether or not to boost

(1) and (2) are easy enough to implement at a given time interval. Something like if(speed < maxSpeed) { accel(); } else if(speed > maxSpeed) { decel(); } where a double maxBoostSpeed exists to limit speed during a boost.

(3) and (4) could be achieved by drawing a trajectory in front of the car with something like [ x + speed*Math.cos(angle), y + speed * Math.sin(angle) ]. Then (3) could be achieved by steering towards the center of the track, and (4) could be from projecting the trajectory into a line and finding the distance before the next track boundary a.k.a. the next turn. If that distance to the trajectory intersection is large, it may be time to boost.

6
Michael On

I may be misunderstanding your question, but it does not seem like you are looking for AI capabilities in your enemy car.... "All I need is a very basic AI which will follow a path around the course and will readjust if it gets knocked or something happens to it.". AI typically implies learning, but no where does it seem that you need your car to learn from past mistakes/"experiences". It sounds like you can use a path-finding algorithm to solve your problem since you have no requirement of the car actually learning from previous interactions with other cars, fields, etc. A super popular algorithm you can look into is A*. You can set up your game to be a graph with edges that have the "boosts" be lower weighted then the common "road". The obstacles or path-finding equivalent term - walls can be represented as high weight edges which would cause your car to avoid them automatically, by nature of A* finding the fastest path to a point.

AStar explanation with pseudo code: https://en.wikipedia.org/wiki/A*_search_algorithm

Great visualizer tool: https://qiao.github.io/PathFinding.js/visual/

Accelerating/Decelerating

As for accelerating/decelerating that can be separate logic like randoms deciding whether to speed up or not.

If it gets knocked or something happens to it

You can re-calculate the A* when the car is hit to ensure that your car gets the new fastest path to get back on course. The actual collision logic is up to you (not part of the A* algo).

Note that if you are planning to have more than just a straight path in which the cars can steer (meaning there is no crazy bends or turns) the A* should not have too much variation from the natural algorithm. If you are planning to support that kind of track you may need to look into slightly different algorithms, because you will need to keep track of the rotated angle of your car.