# How would I go about implementing a collision avoidance check, avoiding moving objects from multiple directions?

So I'm making a testbed for an AI actor(ship). It's a simple asteroids game using an AI controlled actor. I'm doing this using VS, monogame.

I have implemented a simple steering behaviour using a tutorial as a guideline, found here. https://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-collision-avoidance--gamedev-7777 It works somewhat.

The problem occurs when the objects(asteroids) are all moving around(at different speeds), using the guidelines from above, it only checks for collisions for whats infront, at specific points. Which would work if all other objects were static. My initial thought was to add a check for back,left,right. Then loop it to check all these positions in a 50-150 units radius. But the more I think about it, the more realize there must be a better way. As understand it, that wouldn't really solve it either, and would be performance heavy.

How would I go about implementing a collision avoidance check that 1; takes into account the approaching objects may come from any direction. 2; the differnt velocities of those objects.

Or might it be better to apply the existing check to the objects and passing the data to the actor?

Any references, tips, guide, code snippets or good old wisdom or simple pointer in the right direction, would be enormously appreciated!

The first thing to do with any large collision sets is elimination. Range check in itself is a good start but another check is simply a direction check. Is the direction the asteroid in this case in the potential set of threats. Are they moving in a direction generally away or toward.

Similar to the principals of sql. What checks and their cost will determine the order of elimination. What set is left is what you apply as the threat set. There are some heavier intercept algorithms around that you can feed direction speed in of 2 objects to determine intercept point. I would then calculate when and where this intercept points occur and using that to know where to avoid. Heat maps and all sort of path finding routines could be used.

This is key part. You could use a very simple local grid system to add a weighting. The lowest weighting would be the safest. But you could also add some simple path finding to evaluate safest path also

• Forgot to add, it occurred to me also, that your distance check really shouldn't be a distance check. It should be a speed check. That is, for any object facing your general direction, under its current speed, how long would it take to arrive at the location of your ship. This is more a temporal test, rather than a distance check. Distance check will eliminate objects in theory that could be travelling fast but fail the distance check and give you no chance to avoid. Feb 8, 2018 at 20:45
• This also occured to me, the tutorial I used as a guideline is as far as I understand it supposed to check non-moving objects. I read up on intercept algorithms following your answer and through those it became apparent that the current steering behaviour will just not work for what I'm trying to do. To be fair, I'm inclined to use a mix of both answers @Aethermolt method of checking for threats and a path algorithm to path through it. Again, thanks to the both of you. Feb 9, 2018 at 15:35

One method I have used in the past is the creation of invisible objects (typically lines, so that you won't have to deal with objects being stuck in the middle) that are at a safe distance from the object (in this case, the ship), placed in the shape of a circle, that will use collision detection to sense objects hitting them. Of course, nothing will bounce off of them.

When it detects a collision on the side away from your object, you can make your AI turn away from it. When the invisible object detects a collision on the side closer to the object, it will then be able to return to a regular state, thus acting as if it has a more sophisticated AI system.

Other choices would include intercepts or your idea. There is also a possibility that you can create a circle that scales itself up and finds in what direction the nearest object to collide with would be, and then tells your AI to avoid it.

• Interesting, how would you actually do a collision detection against lines in a circlular pattern? Provided we're talking about simple Rectangles or evaluating distances as you would circles. Thanks for the advice. Feb 8, 2018 at 19:44
• @Simon I'm using the idea of we have the circle expands until it touches something (standard collision detection)- the circle expanding is the moving object. Whatever hits first is what the closest object is and what we avoid. There are no circular vectors traced out.
– user112281
Feb 8, 2018 at 21:12