We perform a ray cast in front of each Agent, the length based on the velocity the agent is travelling at (The faster we are travelling, the further ahead we should look to avoid)
If we find an obstacle, we ask it if it is turning to avoid. If it is, we turn the same way (two agents travelling in opposite direction towards each other but both turning left will avoid each other).
If it is not turning, we work out whether we should turn left or right to avoid them and do so.
The strength of the steering force varies depending how close the other agent is compared to our velocity (The sooner we will collide, the harder we should turn).
The code ends up being quite big, so lets jump in to it.
We then combine the resulting force with our other steering behaviours.
But when the individual units are coming in on a big angle
Or the groups are large. Then things don’t work quite as well.
The green lines shown in these examples are the avoidance steering forces.
So, how can we improve this? Using Reciprocal Velocity Obstacles would be one way to improve, it may have a high CPU cost however. video demonstration.
I think we could do some fudging of the physics which would allow our agents to slide past each other better. Currently when two agents collide head on, they are both pushed backwards (in the opposite direction than they want to go), we could instead push them sideways (the right angle), which would let them slide past each other much easier! This is probably a cheaper way for a game to go, even if the physics aren’t quite real :)