Avoid overlapping of objects

Hello everyone,

I am creating objects randomly at any positions, what I want to do is while they are getting randomly placed they shouldn’t overlap is there any way to achieve this functionality. I tried many things but I didn’t able to achieve it.

Regards,

Swanand Thakur

What kinds of shapes?

  • circles?
  • squares?
  • rectangles?
  • polygons?

The easiest way to achieve this is to treat all objects as circles with a fully encompassing radius, then do some very basic checks to see if an object that is about to be placed is going to overlap another existing circle.

This has been asked and answered before:

https://forums.coronalabs.com/topic/47560-create-random-position-that-are-not-colliding-each-other/#entry245588

This answer shows how to avoid circle overlaps:

https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015/07/overlapTest

https://www.youtube.com/watch?v=K3ixlmzfgNk

Q: Did you search my repositories before asking this question?  If not,  you should add them to your link list and also download RG_Freestuff so you can search it in the future.

Sorry for raising the same concern again. I have seen the code, to understand it in a much better way would you please tell me what’s the basic idea which has to be implemented. 

Thanks and Regards,

Swanand Thakur.

I’m confused.  I did that above.

The easiest way to achieve this is to treat all objects as circles with a fully encompassing radius, then do some very basic checks to see if an object that is about to be placed is going to overlap another existing circle.

I’ll elaborate a little, but the concept is pretty simple.

If you are placing a circle A  on the screen and all you have on the screen are circles, you simply randomly choose a position and a radius for your ‘to be placed’ circle, then check all other circles to see if they will be less that Circle A radius + Circle B radius apart.  If you find a case where there is overlap, choose new coordinates.  This IS NOT EFFICIENT, but it is the base case from which all other test-then-place solutions can be derived.

What about rectangles?  Easy,  Treat all rectangles like circles, where the radius of the circle is half the length of the longest side of the rectangle.  i.e If a rectangle is wider than it is tall, then the radius of the circle representing its overlap space is width/2.

What about polygons?  These are the most difficult, but still quite simple.  Just plug the vertices into some math and calculate:

  • centroid of shape (x,y position of center)
  • maximum distance on shape from centroid (this is your radius).

Are there other ways?

There are tons of ways:

  • Instead of treating all shapes as circles, if your shapes are all rectangles you can apply rectangle versus rectangle overlap tests (search for the math on this.  I’m sure Lua based solutions exist.)
  • Alternately, you can check the points of shapes’ vertices to see if lie inside other shapes.

If you get really good at this, and if you have a restricted set of shape types, you can actually spend some time refining the math to get the most optimal checks and save computing time.

Are there even more ways?

  • Avoid the whole problem and come up with clever placement and tracking rules of your own.

The point is, there are dozens and dozens of ways to do this, so I find it unlikely you’ll get a ‘one-size fits all’ solution that is perfectly optimal for all cases here.  However, you can start off simple, dig into the math, and come out the other side with some fine knowledge and some cool solutions.

PS - You’ve got my code for the circle test and it’s pretty simple so I suggest that as a starting point.  Pay close attention to the fact that I quit looking if I can’t find a free spot within a reasonable number of tries.

Hey I forgot to ask, but what are using this for?  It might help me and other to answer if we had more background on what you’re making and for what.

To add to the points above, this is actually an active area of research and generally falls under names like Poisson disc distribution. For instance, here’s one of many articles that turned up on a search for “Poisson disc blue noise”, which goes over a few of the common methods.

How I did get rig of problem is just by a simple method of declaring every object a physics body and dynamic in nature and making the gravity to 0,now due to being dynamic in nature if their collision boundaries overlap they on their own push the objects beside and in this way they don’t overlap it served my purpose.

Thanks to everyone who has took part in the above discussion.

Regards,

Swanand Thakur.

What kinds of shapes?

  • circles?
  • squares?
  • rectangles?
  • polygons?

The easiest way to achieve this is to treat all objects as circles with a fully encompassing radius, then do some very basic checks to see if an object that is about to be placed is going to overlap another existing circle.

This has been asked and answered before:

https://forums.coronalabs.com/topic/47560-create-random-position-that-are-not-colliding-each-other/#entry245588

This answer shows how to avoid circle overlaps:

https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015/07/overlapTest

https://www.youtube.com/watch?v=K3ixlmzfgNk

Q: Did you search my repositories before asking this question?  If not,  you should add them to your link list and also download RG_Freestuff so you can search it in the future.

Sorry for raising the same concern again. I have seen the code, to understand it in a much better way would you please tell me what’s the basic idea which has to be implemented. 

Thanks and Regards,

Swanand Thakur.

I’m confused.  I did that above.

The easiest way to achieve this is to treat all objects as circles with a fully encompassing radius, then do some very basic checks to see if an object that is about to be placed is going to overlap another existing circle.

I’ll elaborate a little, but the concept is pretty simple.

If you are placing a circle A  on the screen and all you have on the screen are circles, you simply randomly choose a position and a radius for your ‘to be placed’ circle, then check all other circles to see if they will be less that Circle A radius + Circle B radius apart.  If you find a case where there is overlap, choose new coordinates.  This IS NOT EFFICIENT, but it is the base case from which all other test-then-place solutions can be derived.

What about rectangles?  Easy,  Treat all rectangles like circles, where the radius of the circle is half the length of the longest side of the rectangle.  i.e If a rectangle is wider than it is tall, then the radius of the circle representing its overlap space is width/2.

What about polygons?  These are the most difficult, but still quite simple.  Just plug the vertices into some math and calculate:

  • centroid of shape (x,y position of center)
  • maximum distance on shape from centroid (this is your radius).

Are there other ways?

There are tons of ways:

  • Instead of treating all shapes as circles, if your shapes are all rectangles you can apply rectangle versus rectangle overlap tests (search for the math on this.  I’m sure Lua based solutions exist.)
  • Alternately, you can check the points of shapes’ vertices to see if lie inside other shapes.

If you get really good at this, and if you have a restricted set of shape types, you can actually spend some time refining the math to get the most optimal checks and save computing time.

Are there even more ways?

  • Avoid the whole problem and come up with clever placement and tracking rules of your own.

The point is, there are dozens and dozens of ways to do this, so I find it unlikely you’ll get a ‘one-size fits all’ solution that is perfectly optimal for all cases here.  However, you can start off simple, dig into the math, and come out the other side with some fine knowledge and some cool solutions.

PS - You’ve got my code for the circle test and it’s pretty simple so I suggest that as a starting point.  Pay close attention to the fact that I quit looking if I can’t find a free spot within a reasonable number of tries.

Hey I forgot to ask, but what are using this for?  It might help me and other to answer if we had more background on what you’re making and for what.

To add to the points above, this is actually an active area of research and generally falls under names like Poisson disc distribution. For instance, here’s one of many articles that turned up on a search for “Poisson disc blue noise”, which goes over a few of the common methods.

How I did get rig of problem is just by a simple method of declaring every object a physics body and dynamic in nature and making the gravity to 0,now due to being dynamic in nature if their collision boundaries overlap they on their own push the objects beside and in this way they don’t overlap it served my purpose.

Thanks to everyone who has took part in the above discussion.

Regards,

Swanand Thakur.