A traffic simulation model developed in Unreal Engine 5.
Inspired by modern open-world video games, this project was developed to learn the basics of a large-scale real-time traffic simulation.
Please note, that TrafficAI does not use UE5's Mass Entity Framework. The goal was to learn about AI used in games and employ strategies that help simulate on a large scale. Hence, a custom Data-Oriented-Design approach was favored against an existing framework.
birds_eye_view.mp4
Behind the scenes, the simulation is managed by two core systems - TrRepresentationSystem
and TrSimulationSystem
.
TrSimulationSystem
- It is the brain of the simulation and is responsible for driving all autonomous vehicles.
For each vehicle,
TrSimulationSystem
uses the Kinematic Bicycle Model to steer and move the vehicle, Intelligent Driver Model (IDM) to determine a safe acceleration value to prevent collision against the leading vehicle and Craig Reynold's path following algorithm to keep the vehicle on track. - Additionally, for each vehicle, it queries vehicles in the vicinity and filters those on a direct course of collision.
- Although no collision avoidance maneuvers are applied, the braking effect caused by the IDM is enough to prevent head-on collisions.
- The system also employs another system called
TrIntersectionManager
that simulates traffic signals by periodically blocking certain nodes at intersections while allowing the passage of traffic from the rest of the nodes. - Last but not least,
TrSimulationSystem
is based on a DOD solution that treats vehicles as numerical entities. It incorporates multiple arrays of floating point values that define the state of each entity. This plays a major role in making the simulation run on the CPU at respectable framerates. The following values are used to define the state of a vehicle/entity: Position, Velocity, Acceleration, Heading, Goal (the location the vehicle is supposed to go to), and some metadata values such as the index of the vehicle directly in front of the current vehicle, and information about its current path.
- It is the brain of the simulation and is responsible for driving all autonomous vehicles.
For each vehicle,
TrRepresentationSystem
- No matter how realistic an AI system becomes, in most games it becomes useless if it cannot be interacted with.
- This system allows players to interact with the vehicles without taxing the CPU too much.
- Each vehicle has two LODs. One with the least amount of detail is represented with an Instanced Static Mesh while the other with the highest amount of detail is represented by an actor.
- Actors can be controlled by players and can physically interact with the world and other vehicles.
TrRepresentationSystem
seamlessly swaps ISMCs with Actors and vice-versa as they come in and out of range of the player.- While ISMCs are moved by directly overriding their position & orientation received from
TrSimulationSystem
, actors are moved by a more sophisticated system. - Vehicle Actors are types of
AWheeledVehiclePawn
derived from UE's Chaos Vehicle System. A PID Controller is used, to determine the magnitude and direction of the force required to anchor the vehicle to the desired position and steer the vehicle's actor to match its orientation, to the values received from the simulation system.
intersection.mp4
In TrafficAI, vehicles don't actively try to avoid obstacles or other vehicles by using specialized maneuvers. Instead, they detect vehicles that are directly in front of them and choose the one that poses the greatest risk of collision. They then apply brakes to prevent an imminent collision.
Detecting objects in the surroundings can be a challenging task, especially if we want to avoid doing too many comparisons. To solve this problem, a spatial acceleration structure called Implicit Grid was used, which allows us to query nearby vehicles very quickly.
After the query is complete, a mathematical operation using the position of each queried vehicle is performed to determine which vehicles overlap against the sensing region of the current vehicle. Here's how it has been implemented in TrafficAI -
TrafficAI/Source/TrafficAI/Simulation/TrSimulationSystem.cpp
Lines 385 to 407 in 99e33e7
This, by far is the most time-consuming operation in the entire simulation update. Any efforts to further optimize the system must be focussed on this region in UTrSimulationSystem::UpdateCollisionData
TrafficAI uses a slightly modified version of Craig Reynolds' Path Following algorithm. It uses the distance between the vehicle's current position and projected position to decide if it's on track or off.
TrafficAI/Source/TrafficAI/Simulation/TrSimulationSystem.cpp
Lines 174 to 187 in 4838ef8
- Kinematic Bicycle Model
- Intelligent Driver Model (IDM)
- Craig Reynold's
- 5.7 Path Following (Steering) - Nature of Code
- Proportional–integral–derivative controller
- CAR STEERING
- Simple 2D car steering physics in games
Unreal Engine 5.2+
The paid assets used in the demonstrations are not included in the repository but can be found at Polygon - City Pack.