Answer the question
In order to leave comments, you need to log in
How to quickly find a group of objects in an array that fall into the selected zone by coordinates Left-Top-Right-Bottom?
There is an array of classes, each of which is an object on the plane.
All objects are rectangles. In addition to XY coordinates and WH height-width, classes can return LTRB side coordinates. An array can contain from 3000 to 40000 or more objects in total.
Task: organize a quick search for a group of elements that fall into the zone according to the Left-Top-Right-Bottom coordinates, which is necessary for collision detection and selection of elements for rendering.
Answer the question
In order to leave comments, you need to log in
Finding collisions quickly is not a trivial task. But, fortunately, it has already been studied.
gamedevelopment.tutsplus.com/tutorials/quick-tip-u...
en.wikipedia.org/wiki/Quadtree
en.wikipedia.org/wiki/Collision_detection
www.java-gaming.org/index.php?topic=29244.0
staticvoidgames .com/tutorials/intermediateConcepts...
Depending on the behavior of your objects, it's possible to come up with something more efficient (more narrow-minded). Usually this is either tracking in the area of a single point, or caching the results.
I usually use the grid to optimize the search. Simple and effective.
That is, build a two-dimensional array, each element of which contains a list of all objects that fall into this cell.
When an object is moved, its position in the grid must also be updated.
Accordingly, for the search, it is enough to check all the cells in which the object falls, collect a list of potential candidates for the intersection and check each of them.
If the space is strongly non-homogeneous (that is, there are many heaps of objects broken far away), then the regular grid becomes very inefficient in terms of memory consumption, then it is better to build a sort tree (kd, Quad, Binary).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question