Roblox Pursuit AI: autonomous & self-driving cop car chases

I never expected to be able to reach this state… but it succeeded: autonomous car chases! It was somewhat a journey to get there, and I will list the most important milestones during the creation. If you’re in a hurry, feel invited to take a look at the showcase on YouTube or join the game directly.


:scroll: #1 - Setting a car’s movement programmatically
Because I don’t see myself as car modeler, I took two of the Roblox endorsed vehicle models and experimented a bit with it.

After experimenting, I learned the following:

  • Setting the AngularVelocity of the four wheel motors move the car forward.
  • Setting the TargetPosition of the SteeringPrismatic makes the car steer either left or right, or any value within.
  • Setting the AngularVelocity of the rear wheel motors to 0 but keeping the front 2 at their original speed can make a car drift at higher speeds.

image


:oncoming_automobile: #2 - Steering a car to a specific position
This sounds like a large step from the one to another, but I had so much luck to find this resource:

I started figuring out how the math of his code works and managed to make the car drive towards predefined points:

The green line is based on the nodes I predefined, the black line is the car’s movement by driving the route.


:red_car: #3 - Getting to follow a player; a big challenge
This went completely wrong at first. I tried things like using the VehicleSeat position of the suspect as dynamic node, Roblox’ core pathfinding service and even trained neural networks as attempt to get anywhere near my goal. The first two failed due to poor performance. The third one was quite interesting.

ScriptOn made a similar police car chase system in 2018, using neural networks. I spent hours learning more about how neural networks work and made my own implementation. However, there were a few reasons I couldn’t continue with neural networks:

  • Neural networks are guessing their outputs, one time they act really well, the other time they make a stupid mistake, their performance isn’t constant.
  • Training time takes somewhat long.
  • Neural networks are a black box, more explained on Stack Exchange about that.
  • Because they are a black box, you can’t debug why they made a decision. There’s no real logic you can base a result on.

So, we had to look further. Roblox’ DevForum didn’t provide any helpful resources, neither did Google when including the term “Roblox”. The useful answers started to pop up when I looked for Unity & Unreal Engine. One post there had a link to an in-depth tutorial about steering behavoirs (2012), and that felt like a gift from heaven. Now, it’s not just about following the player, but actually pursuing the player and using predictions to catch them!


Between those two steps was a lot of fine-tuning, getting the car to drive back on crashes and suchlike. This is not really an interesting part and I didn’t remember everything too, :sweat_smile:


:round_pushpin: #4 - Implementing an A* algorithm
After playing around with my friends and some community members, we noticed that cars were hitting walls continuously. This didn’t make the chase that intense, as it was very easy to get a police car trapped behind a wall. I tried to learn more about collision avoidance, but just didn’t get my head wrapped around that. The internet didn’t had that much resources, except a few study materials and one resource on the steering behaviors tutorial.

I decided to park the object avoidance part and maybe I will get to learn it (or find a cool developer on this forum knowing how to nail it) in the future. I implemented an A* pathfinding algorithm with predefined nodes to calculate a path to the suspect. Once the suspect is either nearby or the Ray casted doesn’t have any obstacles (police vehicles and border barriers ignored), the original pursuing behavior would return.


(The green lines are connections between one node to another)


:date: And… the future?
The cars still have a few problems, such as:

  • Object avoidance isn’t present, so it doesn’t avoid obstacles a lot. This can lead into less challenging chases and crashing officers at maps with many obstacles.
  • The A* pathfinding algorithm isn’t dynamic, it’s based on predefined nodes.
  • Police cars flip quite quick when getting contact with the player. This can be caused by the chassis.
  • It feels like the suspect’s car has an invisible block behind, the police cars can’t get faster when behind the suspect. I believe this is due to the network ownership model and difficult to solve.

_If you are interested, I made all of those plans in the next evolution of pursuing - https://youtu.be/BM9v6SwiCVI._

I didn’t make this for a game, just for educational purposes. I planned to open-source as learning resource, but it has a few problems and isn’t a plug-and-play solution yet (requires A* nodes to be set around each obstacle). I hope to work further on this project though!

If you have any feedback, got a game idea, want to use it in a game of yours or would like to cooperate on improvements, feel free to let me know as reply!

:video_camera: Do you have a YouTube video to showcase its functionalities?
Here is a new showcase of competition against the cops which is even more upgraded: https://youtu.be/BM9v6SwiCVI

:video_game: Can I play the experiment (old)?

For now, take care, stay healthy and enjoy developing! :smiling_face_with_three_hearts:

153 Likes

Dammmmm. I tested out the game and this is insane. I did find that the cops crashed into each other a lot and were ramming me out of the box they made around me but in general this is insane.

4 Likes

Thanks a ton for your kind words! I agree, the AI is a little bit too agressive and I will attempt to let them take care of safe distance between each unit. I have no idea how to do that at the moment, but maybe an idea pops up in the future…

2 Likes

This is really good! I’ve tested the game and it worked well. Would you ever consider making an open source version? :upside_down_face:

4 Likes

That are indeed my plans, once the AI is less agressive and drives more safely :upside_down_face:

8 Likes

Wow, I really want to see AI starting to get into roblox games and big games also

2 Likes

I saw this being retweeted in twitter by Dev Relations, and man I was AMAZED! Love the hard work and great job! :grinning_face_with_smiling_eyes:

I would love to use this in my game! I’ve been looking for simple way to have AI in a game for a while.

Would you consider making a racing ai, which could follow a loop.

4 Likes

I didn’t create a tweet on Twitter, probably you meant the safe driving implementation of “TommyRBLX”. Would love to see how that would work though, as obstacle avoidance can improve pursuits a lot!

3 Likes

Interesting one! I am attempting to create that for Go-Karting Xtreme, a popular Go-Karting group, would be a really good addition for players :eyes:

2 Likes

This is so cool! I noticed that one of the police cars wouldn’t move but apart from that it is amazing! :smiley:

1 Like

Indeed, I made two variants and noticed one performs much better, so I disabled the other variant. I forgot to remove it though :see_no_evil:

3 Likes

Do you think using stuff like this can make it possible to have something like AI assisted driving?

He already made the AI drive the car so… :+1:

Well AI and AI assisted driving are pretty different, one gets all of the control, the other gets less control and has to guess or stay on a path that the driver picks.

1 Like

What I’m trying to say If you can make AI drive you can make it assist driving.

(Like Forza Horizon 4 assisted steering AI) :smiley:

1 Like

Ah, you meant like an automated Uber service? You pick a destination and (possibly) an A* algorithm finds a path which the car will drive?

1 Like

I think he meant that you can drive and If you get out of the lane the AI will take control and move you back

Tesla someone?

1 Like

Yeah that’s what im trying to ask.

Eh not rlly, its more of a Ai assisted like you have 75% control of the car and the ai has 25% so the AI helps drive.