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.
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) and (2) are easy enough to implement at a given time interval. Something like
if(speed < maxSpeed) { accel(); } else if(speed > maxSpeed) { decel(); }
where adouble 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.