Components Overlapping/Collision-detection

I’m working on a JUCE program at the moment in which the user can create an indeterminate number of movable/draggable objects of a particular component, within a fixed area of the window.
Programmatically, this is done with an ownedArray of the component’s class and that all works fine for the most part.

I want to make it so that these dynamically created components cannot overlap, intersect or otherwise block each other on the screen, but I can’t seem to find the best way to do this.

I had been using a RectangleList to store information about what space on the screen was taken up by each component (RectangleList.add when component is created or placed, .subtract when the opponent is moved or deleted) and then do an IntersectsRectangle check on that RectangleList to determine if a component’s position already contained space reserved for one of its siblings.
The problem with that code was that it was neither fast nor effective and resulted in a very fragmented RectangleList over time (which consolidation didn’t seem to fix completely).

I had considered using a combination of toBack() and getVisibleArea() on the components to put them behind any of their siblings and then check if they were being blocked but the only way I could envision doing that is by having each component call a parent component function to loop through the ownedArray and check the component’s getVisibleArea() against each of its siblings getBounds() in turn, which could be horribly slow, especially considering these components could number in the hundreds or thousands depending on the usage of the program.

Perhaps there’s a way to use ComponentBoundsConstrainers to prevent siblings from overlapping each other or otherwise occupying each other’s space?

Any suggestions to get around this would be very welcome!
Thanks in advance :slight_smile:

I did something like that a while back and did all the overlap/collision checking in ComponentBoundsConstrainer::applyBoundsToComponent.

It’s a tricky (but quite interesting) geometry problem. If none of these things are overlapping, then RectangleLists would probably be no better than just an array of their positions. ComponentBoundsConstrainer won’t do the job for you, but it will provide a good place to put your collision-detection logic.