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.
#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.
#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.
#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,
#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)
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!
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
Can I play the experiment (old)?
For now, take care, stay healthy and enjoy developing!
Edit: September 2024
Thanks for the continued support! Still see new likes and references every so often, considering to write a new story on what we’re working on, and how we got pursuits to be competitive, reliable and high performing. Stay tuned for that!