Download

Sphere overlap vs vector distance?

Hi, I am getting some basic AI distance to player detection going for my game and all guides I find use spheres to see if it overlaps with the player.

But shouldn’t just checking the distance between the AI location and player location do the exact same thing in practice and be much simpler to work with? Or are sphere collisions just that cheap?

If you have direct access to the objects, then a squared distance check of their position is indeed the fastest way. Also an important point is that the sphere query returns any object whose collision intersects with that sphere, not any object whose position is within the sphere - so stricly speaking the two things are not the same.

If you’re searching for objects, a sphere query is likely going to be faster or more convenient than say, iterating over all actors in the world.

My AI actors do have the player permanently stored, so that’s not an issue.

Do collisions check for overlap every tick, or does it *just *trigger once on overlap? Because if I do a plain distance check, I’ll have to be checking all the time to see if the player entered the range.
If that’s the case I can see how collisions might be more efficient, especially when you start getting more NPCs continiously checking for the player at all times.

Overlaps are still generated continously as objects move, it’s just that the engine tracks what is overlapping at any given time and broadcasts events when it changes. Overlap ***Events ***are extremely slow, and tbh I’ve gotten into the habit of disabling it globally now to avoid using them. It’s actually much faster to perform an overlap query by ticking the actor and doing it yourself.

If you already have a reference to the actor, and it’s already ticking, then a squared distance check every frame is going to be immeasurable. If you’re checking many actors, then you might want to use collision queries, or some other spatial acceleration system. Something like this.

Queries are fast, checking distances is fast (assuming you can access the actors quickly), overlap events are slow.

Just wondering, what’s the best way to disable overlap events on certain actors? We need them for some but not most of the actors in our levels.

You can only disable them by checking/setting the flags in components. I use source builds on most projects so I’ve just disabled it by default in UPrimitiveComponent.