Obstacle Avoidance steering behavior: how can an entity avoid an obstacle while other forces are acting on the entity?

I'm trying to implement the Obstacle Avoidance steering behavior in my 2D game.

Currently my approach is to apply a force on the entity, in the direction of the normal of the heading, scaled by a number that gets bigger the closer we are to the obstacle. This is supposed to push the entity to the side and avoid the obstacle that blocks it's way.

However, in the same time that my entity tries to avoid an obstacle, it Seeks to a point more or less behind the obstacle (which is the reason it needs to avoid the obstacle in the first place).

The Seek algorithm constantly applies a force on the entity that pushes it (more or less) in the direction of the obstacle, while the Obstacle Avoidance algorithm constantly applies a force that pushes the entity away (more accurately, to the side) of the obstacle.

The result is that sometimes the entity succesfully avoids the obstacle, and sometimes it collides with it, depending on the strength of the avoidance force I'm applying.

How can I make sure that a force will succeed in steering the entity in some direction, while other forces are currently acting on the entity? (And while still looking natural).

I can't allow entities to collide with obstacles when realistically they should be able to easily avoid them, doesn't matter what they're currently doing.

Also, the Obstacle Avoidance algorithm is made exactly for the case where another force is acting on the entity. Otherwise it wouldn't be moving and there would be no need to avoid anything. So maybe I'm missing something. Thanks

You are just summing the forces, which means that the only way to reduce the seek behavior is to overwhelm it with a large avoid force. A simple alternative is to give each behavior a "desire" level in addition to a force. Then take the weighted average of the vectors to determine the steering. So seek might be always 1.0, and whenever you are far from an obstacle you give 'avoid' a very low desire. When you get near something the desire could go up to something like 10.0. Then roughly 90% of the steering comes from avoiding the obstacle, but the vector can still be the same size (say, the maximum steering force). For extra goodness, don't base the desire on how close you are to the obstacle, but on the closest you will get to any obstacle over the next second or two. Prediction is the key to keep things from crashing. From there it is basically just tweaking the numbers until your agents don't crash as much.

In complex environments with many obstacles, these kinds of simple force based steering behaviors are really no substitute for a proper pathfinding algorithm, but in sparse environments they work fine.

If you want it to be a realistic representation of an intelligent agent avoiding an obstacle, then I would suggest a different (more representative and general) kind of model.

When the agent first enters the situation, have it plot a course to the target, and check whether the course will crash into anything or not. If not, then it can follow that plan until the situation changes. When the situation changes, and/or every so often in complex situations, evaluate whether there is a collision or other problem with what the agent is doing.

If and when the agent detects that its current plan will result in a crash, have it plot a course that won't crash. In this case, needs to go to the side, and possible accelerate towards the target more slowly, until it clears the obstacle.

For more complex situations, you can add more tests for states and other coping strategies. For example, it might want to limit its speed at some point, rather than forever accelerating towards its target.

This kind of state-based heuristic algorithm is much more like what a decision-making agent would do realistically, and can lead not just to realistic behavior in this one case, but the kind of system that can behave in realistic and interesting ways in more complex and dynamic cases, too.

Add a repulsive force from the obstacle if the obstacle is within a certain radius. This force could be scaled to overcome any voluntary force (such as the Seek force) if too close - that way the entity will do its best to avoid crashing.

Obstacle Avoidance as described here includes such a braking force.

• Thanks for answering. What is the best way to calculate the breaking force, to acheive best avoidance? What do I need to take into account in the calculation? Jul 6, 2014 at 15:34
• @Prog I suggest you find what force fits your game best by trial and error, just make sure it is enough to repel an entity heading straight for the obstacle at relatively high speed and it should cover most cases. A common model force is proportional to 1/R^n for entity-obstacle distance R and some exponent n (try 2-3). n=2 for many physical forces, which might make it look "natural" depending on the setting. You may want the force to depend on the entity's speed, so that the entity does not rebound with the same speed in the other direction. Jul 6, 2014 at 17:05