Pedestrian pathfinding part 2: Collision detection

I spent much time during the last weeks in programming the pedestrian pathfinding. After investing much work into the buggy implementation of evasion pathfinding algorithm and many hours of debugging now it seems to work.

Instead of the vehicle implementation (using Dijkstra pathfinding) for the pedestrian I use a jumpoint-pathfinding and a grid map. 

To prevent pedestrians from colliding with aother pedestrian, they mark their map pos as blocked (drawn red in the screenshot).



To synchronize between the vehicles and the pedestrians the vehicles project their actual position onto the pedestrian map (drawn black in the the screenshot). So no pedestrian can enter a field, a vehicle is driving across. 

Only for exceptional cases (when the blocking of the map has not prevented a collision) there is a front collider attached to each pedestrian.



Pathfinding seems to work fine now. So I can now go on with a little more exciting part of game development including graphic stuff and game mechanics.

First AI approach for the traffic system

With more than one vehicle on the road a little bit of artificial intelligence is needed. 

I decided to attach three Unity3D collider (with activated “trigger” property and one rigidbody component) to each vehicle:

  • An active front collider to detect if the vehicle is about to crash into another car or pedestrian
  • A passive body collider to be detected by the front collider of another vehicle
  • A passive back collider – also to be detected by another front collider

 

First idea of the collider and the AI behavior:

If the front collider of the vehicle hits a body collider it stops and tries to drive back a little bit. 

When the back collider is hit, the other car is probably just driving on the same track but with less speed. In this case the vehicle with the triggered front collider adjusts to the speed of the car in front of it.

The result looks more like a cartoon character using trial and error  than a defensive driving style. I call this AI behavior “Italian  driving mode” ;-)

A first animated video of some vehicles using the AI:

 

BTW: One thing I learned about Unity3Dwhile working on this: Because Unity seems to serialize enums as numbers and not by name: Use a flag number for every enum value. Otherwise when you change the order inside the enum definition the meaning swaps.