How do I Stop Overlap of Cubes in code? Hard Challenge

Hello Guys,

So over the last 4 days ive struggled in preventing randomized cubes from overlapping on the same coords.

So far I have this code:

function spawnCubes() xposit1 = math.random(1,4) yposit1 = math.random(1,5) print(xposit1) print(yposit1) flag2 = true while flag2 == true do xposit2 = math.random(1,4) yposit2 = math.random(1,5) if (xposit2 ~= xposit1) and (yposit2 ~= yposit1) then flag2 = false end end print(xposit2) print(yposit2) flag3 = true while flag3 == true do xposit3 = math.random(1,4) yposit3 = math.random(1,5) if (xposit3 ~= xposit1) and (yposit3 ~= yposit1) and (xposit3 ~= xposit2) and (yposit3 ~= yposit2) then flag3 = false end end print(xposit3) print(yposit3) flag4 = true while flag4 == true do xposit4 = math.random(1,4) yposit4 = math.random(1,5) if (xposit4 ~= xposit1) and (yposit4 ~= yposit1) and (xposit4 ~= xposit2) and (yposit4 ~= yposit2) and (xposit4 ~= xposit3) and (yposit4 ~= yposit3) then flag4 = false end end print(xposit4) print(yposit4) flag5 = true while flag5 == true do xposit5 = math.random(1,4) yposit5 = math.random(1,5) if (xposit5 ~= xposit1) and (yposit5 ~= yposit1) and (xposit5 ~= xposit2) and (yposit5 ~= yposit2) and (xposit5 ~= xposit3) and (yposit5 ~= yposit3) and (xposit5 ~= xposit4) and (yposit5 ~= yposit4) then flag5 = false end end print(xposit5) print(yposit5) flag6 = true while flag6 == true do xposit6 = math.random(1,4) yposit6 = math.random(1,5) if (xposit6 ~= xposit1) and (yposit6 ~= yposit1) and (xposit6 ~= xposit2) and (yposit6 ~= yposit2) and (xposit6 ~= xposit3) and (yposit6 ~= yposit3) and (xposit6 ~= xposit4) and (yposit6 ~= yposit4) and (xposit6 ~= xposit5) and (yposit6 ~= yposit5) then flag6 = false end end print(xposit6) print(yposit6) flag7 = true while flag7 == true do xposit7 = math.random(1,4) yposit7 = math.random(1,5) if (xposit7 ~= xposit1) and (yposit7 ~= yposit1) and (xposit7 ~= xposit2) and (yposit7 ~= yposit2) and (xposit7 ~= xposit3) and (yposit7 ~= yposit3) and (xposit7 ~= xposit4) and (yposit7 ~= yposit4) and (xposit7 ~= xposit5) and (yposit7 ~= yposit5) and (xposit7 ~= xposit6) and (yposit7 ~= yposit6) then flag7 = false end end print(xposit7) print(yposit7) flag8 = true while flag8 == true do xposit8 = math.random(1,4) yposit8 = math.random(1,5) if (xposit8 ~= xposit1) and (yposit8 ~= yposit1) and (xposit8 ~= xposit2) and (yposit8 ~= yposit2) and (xposit8 ~= xposit3) and (yposit8 ~= yposit3) and (xposit8 ~= xposit4) and (yposit8 ~= yposit4) and (xposit8 ~= xposit5) and (yposit8 ~= yposit5) and (xposit8 ~= xposit6) and (yposit8 ~= yposit6) and (xposit8 ~= xposit7) and (yposit8 ~= yposit7) then flag8 = false end end print(xposit8) print(yposit8) flag9 = true while flag9 == true do xposit9 = math.random(1,4) yposit9 = math.random(1,5) if (xposit9 ~= xposit1) and (yposit9 ~= yposit1) and (xposit9 ~= xposit2) and (yposit9 ~= yposit2) and (xposit9 ~= xposit3) and (yposit9 ~= yposit3) and (xposit9 ~= xposit4) and (yposit9 ~= yposit4) and (xposit9 ~= xposit5) and (yposit9 ~= yposit5) and (xposit9 ~= xposit6) and (yposit9 ~= yposit6) and (xposit9 ~= xposit7) and (yposit9 ~= yposit7) and (xposit9 ~= xposit8) and (yposit9 ~= yposit8) then flag9 = false end end print(xposit9) print(yposit9) flag10 = true while flag10 == true do xposit10 = math.random(1,4) yposit10 = math.random(1,5) if (xposit10 ~= xposit1) and (yposit10 ~= yposit1) and (xposit10 ~= xposit2) and (yposit10 ~= yposit2) and (xposit10 ~= xposit3) and (yposit10 ~= yposit3) and (xposit10 ~= xposit4) and (yposit10 ~= yposit4) and (xposit10 ~= xposit5) and (yposit10 ~= yposit5) and (xposit10 ~= xposit6) and (yposit10 ~= yposit6) and (xposit10 ~= xposit7) and (yposit10 ~= yposit7) and (xposit10 ~= xposit8) and (yposit10 ~= yposit8) and (xposit10 ~= xposit9) and (yposit10 ~= yposit9) then flag10 = false end end print(xposit10) print(yposit10) flag11 = true while flag11 == true do xposit11 = math.random(1,4) yposit11 = math.random(1,5) if (xposit11 ~= xposit1) and (yposit11 ~= yposit1) and (xposit11 ~= xposit2) and (yposit11 ~= yposit2) and (xposit11 ~= xposit3) and (yposit11 ~= yposit3) and (xposit11 ~= xposit4) and (yposit11 ~= yposit4) and (xposit11 ~= xposit5) and (yposit11 ~= yposit5) and (xposit11 ~= xposit6) and (yposit11 ~= yposit6) and (xposit11 ~= xposit7) and (yposit11 ~= yposit7) and (xposit11 ~= xposit8) and (yposit11 ~= yposit8) and (xposit11 ~= xposit9) and (yposit11 ~= yposit9) and (xposit11 ~= xposit10) and (yposit11 ~= yposit10) then flag11 = false end end print(xposit11) print(yposit11) flag12 = true while flag12 == true do xposit12 = math.random(1,4) yposit12 = math.random(1,5) if (xposit12 ~= xposit1) and (yposit12 ~= yposit1) and (xposit12 ~= xposit2) and (yposit12 ~= yposit2) and (xposit12 ~= xposit3) and (yposit12 ~= yposit3) and (xposit12 ~= xposit4) and (yposit12 ~= yposit4) and (xposit12 ~= xposit5) and (yposit12 ~= yposit5) and (xposit12 ~= xposit6) and (yposit12 ~= yposit6) and (xposit12 ~= xposit7) and (yposit12 ~= yposit7) and (xposit12 ~= xposit8) and (yposit12 ~= yposit8) and (xposit12 ~= xposit9) and (yposit12 ~= yposit9) and (xposit12 ~= xposit10) and (yposit12 ~= yposit10) and (xposit12 ~= xposit11) and (yposit12 ~= yposit11) then flag12 = false end end print(xposit12) print(yposit12) flag13 = true while flag13 == true do xposit13 = math.random(1,4) yposit13 = math.random(1,5) if (xposit13 ~= xposit1) and (yposit13 ~= yposit1) and (xposit13 ~= xposit2) and (yposit13 ~= yposit2) and (xposit13 ~= xposit3) and (yposit13 ~= yposit3) and (xposit13 ~= xposit4) and (yposit13 ~= yposit4) and (xposit13 ~= xposit5) and (yposit13 ~= yposit5) and (xposit13 ~= xposit6) and (yposit13 ~= yposit6) and (xposit13 ~= xposit7) and (yposit13 ~= yposit7) and (xposit13 ~= xposit8) and (yposit13 ~= yposit8) and (xposit13 ~= xposit9) and (yposit13 ~= yposit9) and (xposit13 ~= xposit10) and (yposit13 ~= yposit10) and (xposit13 ~= xposit11) and (yposit13 ~= yposit11) and (xposit13 ~= xposit12) and (yposit13 ~= yposit12) then flag13 = false end end print(xposit13) print(yposit13) flag14 = true while flag14 == true do xposit14 = math.random(1,4) yposit14 = math.random(1,5) if (xposit14 ~= xposit1) and (yposit14 ~= yposit1) and (xposit14 ~= xposit2) and (yposit14 ~= yposit2) and (xposit14 ~= xposit3) and (yposit14 ~= yposit3) and (xposit14 ~= xposit4) and (yposit14 ~= yposit4) and (xposit14 ~= xposit5) and (yposit14 ~= yposit5) and (xposit14 ~= xposit6) and (yposit14 ~= yposit6) and (xposit14 ~= xposit7) and (yposit14 ~= yposit7) and (xposit14 ~= xposit8) and (yposit14 ~= yposit8) and (xposit14 ~= xposit9) and (yposit14 ~= yposit9) and (xposit14 ~= xposit10) and (yposit14 ~= yposit10) and (xposit14 ~= xposit11) and (yposit14 ~= yposit11) and (xposit14 ~= xposit12) and (yposit14 ~= yposit12) and (xposit14 ~= xposit13) and (yposit14 ~= yposit13) then flag14 = false end end print(xposit14) print(yposit14) flag15 = true while flag15 == true do xposit15 = math.random(1,4) yposit15 = math.random(1,5) if (xposit15 ~= xposit1) and (yposit15 ~= yposit1) and (xposit15 ~= xposit2) and (yposit15 ~= yposit2) and (xposit15 ~= xposit3) and (yposit15 ~= yposit3) and (xposit15 ~= xposit4) and (yposit15 ~= yposit4) and (xposit15 ~= xposit5) and (yposit15 ~= yposit5) and (xposit15 ~= xposit6) and (yposit15 ~= yposit6) and (xposit15 ~= xposit7) and (yposit15 ~= yposit7) and (xposit15 ~= xposit8) and (yposit15 ~= yposit8) and (xposit15 ~= xposit9) and (yposit15 ~= yposit9) and (xposit15 ~= xposit10) and (yposit15 ~= yposit10) and (xposit15 ~= xposit11) and (yposit15 ~= yposit11) and (xposit15 ~= xposit12) and (yposit15 ~= yposit12) and (xposit15 ~= xposit13) and (yposit15 ~= yposit13) and (xposit15 ~= xposit14) and (yposit15 ~= yposit14) then flag15 = false end end print(xposit15) print(yposit15) flag16 = true while flag16 == true do xposit16 = math.random(1,4) yposit16 = math.random(1,5) if (xposit16 ~= xposit1) and (yposit16 ~= yposit1) and (xposit16 ~= xposit2) and (yposit16 ~= yposit2) and (xposit16 ~= xposit3) and (yposit16 ~= yposit3) and (xposit16 ~= xposit4) and (yposit16 ~= yposit4) and (xposit16 ~= xposit5) and (yposit16 ~= yposit5) and (xposit16 ~= xposit6) and (yposit16 ~= yposit6) and (xposit16 ~= xposit7) and (yposit16 ~= yposit7) and (xposit16 ~= xposit8) and (yposit16 ~= yposit8) and (xposit16 ~= xposit9) and (yposit16 ~= yposit9) and (xposit16 ~= xposit10) and (yposit16 ~= yposit10) and (xposit16 ~= xposit11) and (yposit16 ~= yposit11) and (xposit16 ~= xposit12) and (yposit16 ~= yposit12) and (xposit16 ~= xposit13) and (yposit16 ~= yposit13) and (xposit16 ~= xposit14) and (yposit16 ~= yposit14) and (xposit16 ~= xposit15) and (yposit16 ~= yposit15) then flag16 = false end end print(xposit16) print(yposit16) flag17 = true while flag17 == true do xposit17 = math.random(1,4) yposit17 = math.random(1,5) if (xposit17 ~= xposit1) and (yposit17 ~= yposit1) and (xposit17 ~= xposit2) and (yposit17 ~= yposit2) and (xposit17 ~= xposit3) and (yposit17 ~= yposit3) and (xposit17 ~= xposit4) and (yposit17 ~= yposit4) and (xposit17 ~= xposit5) and (yposit17 ~= yposit5) and (xposit17 ~= xposit6) and (yposit17 ~= yposit6) and (xposit17 ~= xposit7) and (yposit17 ~= yposit7) and (xposit17 ~= xposit8) and (yposit17 ~= yposit8) and (xposit17 ~= xposit9) and (yposit17 ~= yposit9) and (xposit17 ~= xposit10) and (yposit17 ~= yposit10) and (xposit17 ~= xposit11) and (yposit17 ~= yposit11) and (xposit17 ~= xposit12) and (yposit17 ~= yposit12) and (xposit17 ~= xposit13) and (yposit17 ~= yposit13) and (xposit17 ~= xposit14) and (yposit17 ~= yposit14) and (xposit17 ~= xposit15) and (yposit17 ~= yposit15) and (xposit17 ~= xposit16) and (yposit17 ~= yposit16) then flag17 = false end end print(xposit17) print(yposit17) flag18 = true while flag18 == true do xposit18 = math.random(1,4) yposit18 = math.random(1,5) if (xposit18 ~= xposit1) and (yposit18 ~= yposit1) and (xposit18 ~= xposit2) and (yposit18 ~= yposit2) and (xposit18 ~= xposit3) and (yposit18 ~= yposit3) and (xposit18 ~= xposit4) and (yposit18 ~= yposit4) and (xposit18 ~= xposit5) and (yposit18 ~= yposit5) and (xposit18 ~= xposit6) and (yposit18 ~= yposit6) and (xposit18 ~= xposit7) and (yposit18 ~= yposit7) and (xposit18 ~= xposit8) and (yposit18 ~= yposit8) and (xposit18 ~= xposit9) and (yposit18 ~= yposit9) and (xposit18 ~= xposit10) and (yposit18 ~= yposit10) and (xposit18 ~= xposit11) and (yposit18 ~= yposit11) and (xposit18 ~= xposit12) and (yposit18 ~= yposit12) and (xposit18 ~= xposit13) and (yposit18 ~= yposit13) and (xposit18 ~= xposit14) and (yposit18 ~= yposit14) and (xposit18 ~= xposit15) and (yposit18 ~= yposit15) and (xposit18 ~= xposit16) and (yposit18 ~= yposit16) and (xposit18 ~= xposit17) and (yposit18 ~= yposit17) then flag18 = false end end print(xposit18) print(yposit18) flag19 = true while flag19 == true do xposit19 = math.random(1,4) yposit19 = math.random(1,5) if (xposit19 ~= xposit1) and (yposit19 ~= yposit1) and (xposit19 ~= xposit2) and (yposit19 ~= yposit2) and (xposit19 ~= xposit3) and (yposit19 ~= yposit3) and (xposit19 ~= xposit4) and (yposit19 ~= yposit4) and (xposit19 ~= xposit5) and (yposit19 ~= yposit5) and (xposit19 ~= xposit6) and (yposit19 ~= yposit6) and (xposit19 ~= xposit7) and (yposit19 ~= yposit7) and (xposit19 ~= xposit8) and (yposit19 ~= yposit8) and (xposit19 ~= xposit9) and (yposit19 ~= yposit9) and (xposit19 ~= xposit10) and (yposit19 ~= yposit10) and (xposit19 ~= xposit11) and (yposit19 ~= yposit11) and (xposit19 ~= xposit12) and (yposit19 ~= yposit12) and (xposit19 ~= xposit13) and (yposit19 ~= yposit13) and (xposit19 ~= xposit14) and (yposit19 ~= yposit14) and (xposit19 ~= xposit15) and (yposit19 ~= yposit15) and (xposit19 ~= xposit16) and (yposit19 ~= yposit16) and (xposit19 ~= xposit17) and (yposit19 ~= yposit17) and (xposit19 ~= xposit18) and (yposit19 ~= yposit18) then flag19 = false end end print(xposit19) print(yposit19) flag20 = true while flag20 == true do xposit20 = math.random(1,4) yposit20 = math.random(1,5) if (xposit20 ~= xposit1) and (yposit20 ~= yposit1) and (xposit20 ~= xposit2) and (yposit20 ~= yposit2) and (xposit20 ~= xposit3) and (yposit20 ~= yposit3) and (xposit20 ~= xposit4) and (yposit20 ~= yposit4) and (xposit20 ~= xposit5) and (yposit20 ~= yposit5) and (xposit20 ~= xposit6) and (yposit20 ~= yposit6) and (xposit20 ~= xposit7) and (yposit20 ~= yposit7) and (xposit20 ~= xposit8) and (yposit20 ~= yposit8) and (xposit20 ~= xposit9) and (yposit20 ~= yposit9) and (xposit20 ~= xposit10) and (yposit20 ~= yposit10) and (xposit20 ~= xposit11) and (yposit20 ~= yposit11) and (xposit20 ~= xposit12) and (yposit20 ~= yposit12) and (xposit20 ~= xposit13) and (yposit20 ~= yposit13) and (xposit20 ~= xposit14) and (yposit20 ~= yposit14) and (xposit20 ~= xposit15) and (yposit20 ~= yposit15) and (xposit20 ~= xposit16) and (yposit20 ~= yposit16) and (xposit20 ~= xposit17) and (yposit20 ~= yposit17) and (xposit20 ~= xposit18) and (yposit20 ~= yposit18) and (xposit20 ~= xposit19) and (yposit20 ~= yposit19) then flag20 = false end end print(xposit20) print(yposit20) end

However after xposit4 and yposit4 it will not print further results and the simulator freezes.

Can anyone help make my function better so this does not occur?

James x

Look at this tutorial:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

You can use the function there to check to see if your new object is “colliding” with the existing object.

Rob

It would appear that you have a 4x5 grid of cells, and you’re attempting to randomly order those 20 cells (so that for instance cell coordinate 2,3 might be the first one, cell coordinate 4,1 might be the second, and so on to the twentieth, using each cell exactly once) - is that an essentially correct restatement of the problem? if so, post back, there is a very simple solution

I answered a similar question here: http://forums.coronalabs.com/topic/47560-create-random-position-that-are-not-colliding-each-other/#entry245588

You can take the code I provided as an example and modify it to work for cubes.  

The simplest way to do this is to change the radius to be equivalent to to this value of your cubes/rectangles:

local aCube = display.newRect( 0, 0, 100, 200 ) local halfWidth = aCube.contentWidth/2 local halfHeight = aCube.contentHeight/2 local boundingSphereRadius = math.sqrt( (halfWidth ^ 2) + (halfHeight ^ 2) )

-Ed

Look at this tutorial:

http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

You can use the function there to check to see if your new object is “colliding” with the existing object.

Rob

It would appear that you have a 4x5 grid of cells, and you’re attempting to randomly order those 20 cells (so that for instance cell coordinate 2,3 might be the first one, cell coordinate 4,1 might be the second, and so on to the twentieth, using each cell exactly once) - is that an essentially correct restatement of the problem? if so, post back, there is a very simple solution

I answered a similar question here: http://forums.coronalabs.com/topic/47560-create-random-position-that-are-not-colliding-each-other/#entry245588

You can take the code I provided as an example and modify it to work for cubes.  

The simplest way to do this is to change the radius to be equivalent to to this value of your cubes/rectangles:

local aCube = display.newRect( 0, 0, 100, 200 ) local halfWidth = aCube.contentWidth/2 local halfHeight = aCube.contentHeight/2 local boundingSphereRadius = math.sqrt( (halfWidth ^ 2) + (halfHeight ^ 2) )

-Ed