Better way of getting instances in a circle?

How exactly would you detect things in a circle without iterating through an entire group?
Kinda wondering how you do this.

Currently you can use Magnitude but it’d still require you to go through an entire table to check if the instance is in the range of like 50 or so. That can be resource intensive like every 0.05 seconds.

This is my 2nd post hopefully I’m not breaking any rules.

One solution you could do is to divide the group into sub divisions based on positions, perhaps a 3d array in which there are cells that hold objects. When an object moves you can change the grid its located in. Then using this you can determine which grids you need to iterate through based off the position and the radius, so if there are a lot of objects spread out in space, this can cut down on a lot of iteration.

1 Like

I’ll try that idea, it may work.

1 Like

I’d look into quad-trees for 2D or octrees for 3D. I’ve never implemented it myself. How many items on average are you expecting to look through? Things are surprisingly fast these days so you may not even need to optimize if you don’t actually have that many items to check. My safeside would be less than 250 items.

Instead of looping through every single item and making sure’s it’s within range every 0.05 seconds, what you could do is use Region3s (i suggest EgoMoose’s Rotated Region3 Module, as it supports cylinders and balls, which make radius checking easier) every few seconds (4-5 seconds would be fine) and determine what items are in an acceptable range, cache these items in a separate table.

Then, on your 0.05 second loop, you can use the cached items that are verified to be close from the Region3 cache. This will speed things up quite a bit, since you no longer need to constantly check every single item, rather just the nearby ones.

i don’t know enough about oct-trees to suggest anything there.

1 Like

Put in a part that occupies that circle, and use :GetTouchingParts()

In order to handle hit detection of my zombie NPCs, around each player, I cast 6 radial rays in an ellipse around them to see if there are any zombies in attack range (plus some extra math to account for latency). Since each ray has a very small magnitude (the range of the attack), the overhead is negligible, and this can be done very frequently. This is particularly beneficial to my use case because I need to know if the object (zombie) is obstructed.

This method would not be applicable for a larger radius, due to higher overhead and reduced accuracy from the same number of rays, but hopefully food for thought