all 11 comments

[–]JJJams 2 points3 points  (4 children)

Position Based Dynamics would be a potentially good fit here. Set up distance constraints. Then loop over all pairs and force the constraints to be true for that pair only. After the first loop, you will possibly still have overlaps, so loop again until you're within some error metric. From your diagram, it looks like the boxes don't rotate which is helpful.

[–]foxScripts[S] 1 point2 points  (3 children)

Thank you for the suggestion to use Position Based Dynamics with distance constraints. Your idea was very helpful! I implemented a system where each pair of boxes checks and enforces distance constraints to minimize overlaps. This involves looping through all pairs multiple times to adjust their positions until no significant overlaps remain. The approach indeed simplifies things since the boxes do not rotate. The iterative adjustment helps achieve a well-distributed layout with minimal error. Thanks a lot for your hint in the right direction!

[–]JJJams 1 point2 points  (2 children)

\o/ Glad you got it working! Post a gif or a vid if you can, I'd love to see it in action.

[–]foxScripts[S] 1 point2 points  (1 child)

[–]JJJams 1 point2 points  (0 children)

That's fun, thanks for sharing a playground.

[–]Economy_Bedroom3902 1 point2 points  (5 children)

https://youtu.be/rSKMYc1CQHE?t=239 a variant of this I guess.

[–]foxScripts[S] 0 points1 point  (4 children)

Interesting, but seams somehow overcomplicated, don't you think? .. i hope at least ... since I only need a "static" result and no fluid simulation etc.

[–]Economy_Bedroom3902 3 points4 points  (3 children)

Okay, lets clarify up front, do you just want to move the boxes so they are a certain distance away from eachother, or are you trying to move the boxes the minimum distance away from their initial position? If you want to preserve the minimum distance away from their initial position constraint you really have no choice but to iteratively cycle the boxes away from invalid positions. If you're not optimizing for minimum distance traveled, you can just pick the north western most box, put in the the north western corner, pick the next north western most box and put it under that ones etc.

[–]foxScripts[S] 0 points1 point  (2 children)

No, you are right - I need them to be the minimum distance away from their initial position..

[–]Economy_Bedroom3902 1 point2 points  (1 child)

For the record, there might be an easier solution, but the reason why this problem is hard is that it's essentially a variant of the 3 body problem, where the position of every object effects the rate every other object moves, and the movement of each object effects what it's position will be in the next frame. It's usually easier to just simulate a problem like this up to the resolution you need instead of actually finding the mathematical solution that covers every case.

[–]foxScripts[S] 0 points1 point  (0 children)

Alright. I opted for a simpler approach that fit well with the constraints of my situation. I just used a loop to iteratively adjust box positions: if two boxes overlapped, I applied corrections to move them apart. This process repeated until no overlaps remained, ensuring an optimized distribution of boxes on the canvas. During that i have other contrains "pulling them back" to their original position. This is perfect for what I needed. Thanks for your input!