I used this approach in my AI for 2D Games library, it is based on the book ‘Programming Game AI by Example’ written by Matt Buckland.

Consider a boid moving through a series of obstacles whose collision areas are represented by red circles.

The problem is that determining whether a rotated box (the detecting box) intersects with a circle (obstacle collision area) is not a trivial matter. It is even less trivial to determining the intersection coordinates, which are needed to calculate the lateral and breaking forces.

The trick here is to calculate the obstacle position in the boid’s coordinate space.

In this diagram the radius of the grey circles is the obstacle collision radius plus half the width of the detection box, we call this the extended collision radius.

So if we consider each obstacle in turn.

A we can ignore because it has a negative x position (i.e. behind the boid)

B we can ignore because it x position is greater than the detection box length plus the extended collision radius.(i…e. out of range)

C we can ignore because its absolute y position is greater than the extended collision radius.

D we must test this because its absolute y position is less than the extended collision radius.

The next step is to calculate the 2 intersection points between the extended collision radius and the x axis, x0 and x1. We iterate over possible obstacles and use the one with the closest intersection point i.e. smallest x0

The lateral force will be proportional to the obstacles collision radius minus its y position so that the force diminishes with the obstacle’s distance from the x axis. This force would also be inversely proportional to the obstacles x position so that it increases as we get closer to the obstacle.

Finally the breaking force will be inversely proportional to the distance x0 so it increases as the boid approaches the obstacle.