2
\$\begingroup\$

I'm very curious about AI drivers in a racing game, so I tried to build one :). I've built a 3D closed racing course and a car with Unreal engine and all the physics, and I try to develop an algorithm that drives the car.

I saw several posts in the past, like this one Driver AI in racing game but I still don't understand how to control the throttle, steering, and breaks to actually do it.

What I have tried so far is to create some waypoints in the course and use a PID controller to control the throttle and steering that goes to the next waypoint every time. The results were not so good... Depends on the car speed and curves, the steering PID controller has overshoots and/or slow response. After some testing, I think that the PID controller cannot fit for driving a car...

I want the car to drive like an AI driver in a good racing game. Without collision with the walls (closed course) and drive as well as possible.

What is the best method for optimizing the path and what is the best way to control the car (throttle, breaks, steering) to follow that path?

\$\endgroup\$
2
  • \$\begingroup\$ Does the AI collide with other race cars. For instance does it collide with the human's race car as well? \$\endgroup\$
    – AturSams
    Aug 19 at 11:32
  • \$\begingroup\$ @AturSams, no, I have one car that drives alone in a closed racecourse. It can collide only with the road border \$\endgroup\$
    – nrofis
    Aug 19 at 12:38
2
\$\begingroup\$

To get more optimized results, you may want to train a Reinforcement Learning model on your racing game mechanics.

Alternatively, to optimize the route for straight lines, you could use a navmesh. This would make the speed and turn rate computation more difficult.

To start with

  1. Generate a spline (or a 3d line path) of the track's center.
  2. Based on the spline: compute the rate at which the car needs to turn at each point on the track. The turning angle can be compute the dot products of two consecutive directional vectors on the path spline.
  3. Gather information about friction, road-tilt and game world gravity to compute the top speed at which this specific vehicle can turn at that rate. You can do this by a binary search on the physics engine with with the specifics of the track and the car, or alternatively if the physics engine is a black box, you can do this with experimentation, storing the results programmaticaly.
  4. Now work your way backwards, taking into account the time it takes to decelerate to determine the top speed in previous portions of the road.
  5. If the car has to move at a slower rate than the optimal speed, the turning rate should be calibrated accordingly.
  6. To get better results: understand how much leeway the car has to take the turn at increased speeds.
  7. Adjust the optimal speed a bit according to the leeway.

Yes, one method that could work he is to record the expected driving behavior and then play it back according to the progress on the track.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for the answer! I have few questions: 1. I can control the throttle and brakes. How can I manage the optimal speed at each point? 2. How can I compute the best rate and speed at steps 2 and 3? 3. I believe that staying at the central line of the track is but not optimal. For example, in a tiny curve, I think the car should drive straight without following the curve. Can the car drive optimally in the center only? 4. How it should work at runtime (after all the steps completes)? Should I need to hold in the memory the throttle, brakes, and steering for each point? It can be big dataset \$\endgroup\$
    – nrofis
    Aug 19 at 12:52
  • \$\begingroup\$ Did my best to answer some of your questions by updating. You should provide some track examples and a runnable code sample if you want to get into specific details. Your intuition is good. \$\endgroup\$
    – AturSams
    Aug 29 at 13:39
  • \$\begingroup\$ Thank you so much! \$\endgroup\$
    – nrofis
    Aug 29 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.