How to prevent a spawned object from overlapping x,y coordinates with a previous iteration of the same object

Here is some code to help with confusion based on the questions phrasing.

local sheetData = { width = 64, height = 64, numFrames = 8, sheetContentWidth = 256, sheetContentHeight = 128 } local mySheet = graphics.newImageSheet("Sleepers.png",sheetData) local w,h = display.contentWidth, display.contentHeight local playerAnimation = { name = "sleeper", frames={1,2,3}, time = 2000 } local enemyAnimation = { name = "demon", frames={5,6,7}, time = 500 } local bedAnimation = {name = "exit", frames = {4}, time = 0} local player = display.newSprite( mySheet, playerAnimation ) local enemy = display.newSprite( mySheet, enemyAnimation ) local bed = display.newSprite( mySheet, bedAnimation) local physics = require("physics") physics.start() physics.setGravity(0,0) player.x = w/2 player.y = h/2 physics.addBody(player, {density = 0, friction = 0, bounce = 0}) player.myName = "Player" enemy.x = w+64 enemy.y = h+64 physics.addBody(enemy, "static", {isSensor = true}) bed.x = math.random(64, w-100) bed.y = math.random(64, h-100) physics.addBody(bed,"static", {isSensor = true}) bed.myName = "Bed" --local function spinImage (event) -- transition.to( bed, { rotation = bed.rotation+360, time=3500, onComplete=spinImage} ) --end --spinImage() local function spawnBed() bed.x = math.random(math.random(64,w/4), w-100) bed.y = math.random(math.random(64,h/4), h-100) end local function collision(self, event) if event.phase == "began" then if self.myName == "Player" and event.other.myName == "Bed" then timer.performWithDelay(100, spawnBed, 1) end end end player.collision = collision player:addEventListener("collision") bed.collision = collision bed:addEventListener("collision") function left() enemy.x = -64 enemy.y = math.random(0,h) transition.to(enemy,{time=2000, x=w+64}) enemy:play() end function player:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus( self, event.id ) self.isFocus = true self.markX = self.x self.markY = self.y elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x,y end return true end player:addEventListener( "touch", player) player:play() timer.performWithDelay(2000,left,0)

**UPDATE**  This use to be available in the ‘July 2015 Answer Pack’ in the ‘ASKED’ archive… however:

The AskEd archive is getting an overhaul to bring it up to spec with the current Corona SDK features and in cases where it relied on SSK I am updating it to use SSK2.

See code posted below for now…

**UPDATE**  This use to be available in the ‘July 2015 Answer Pack’ in the ‘ASKED’ archive… however:

The AskEd archive is getting an overhaul to bring it up to spec with the current Corona SDK features and in cases where it relied on SSK I am updating it to use SSK2.

See code posted below for now…

Hi Roaminggamer,

the link is down. Could you upload it again? :slight_smile:

Hi.  The AskEdarchive is getting an overhaul to bring it up to spec with the current Corona SDK and in cases where it relied on SSK I am updating it to use SSK2.

I’ll make an announcement when this is all done, but until then, the code for that particular example is in the

  • AskEd July 2015 folder ====> 'overlapTest/'

    – ============================================================= – Answers to interesting Corona Forums Questions – ============================================================= – by Roaming Gamer, LLC. 2009-2015 (http://roaminggamer.com/) – ============================================================= display.setStatusBar(display.HiddenStatusBar) – Notes for this example ( not part of example ) – local notes = { “The person who wrote this question wanted to learn how to check”, “if objects are overlapping.”, “”, “This example generates a number of objects while ensuring none overlaps.”, } for i = 1, #notes do local tmp = display.newText( notes[i], 50, 20 + (i-1) * 30, native.systemFont, 22 ) tmp.anchorX = 0 end – -- 1. Some variables for the example local currentObjects = {} local w = display.contentWidth local h = display.contentHeight local centerX = w/2 local centerY = h/2 local minX = 25 local maxX = w - minX local minY = 200 local maxY = h - minX local maxPieces = 150 local objRadius = 20 local offset = 4 local buffer = -6 local safeRadius = (objRadius + offset) * 2 + buffer local safeRadius2 = safeRadius * safeRadius local maxIter = 150 – -- 2. Some math functions we’ll need local mRand = math.random local mSqrt = math.sqrt local mRad = math.rad local mCos = math.cos local mSin = math.sin local function subVec( x0, y0, x1, y1 ) return { x = x1 - x0, y = y1 - y0 } end local function lenVec( vec ) return mSqrt(vec.x * vec.x + vec.y * vec.y) end local function len2Vec( vec ) return vec.x * vec.x + vec.y * vec.y end – -- 3. A circle generator local function safelyPlaceCircle() local tx = mRand( minX, maxX ) local ty = mRand( minY, maxY) local iter = 0 local isSafe = false while( isSafe == false ) do iter = iter + 1 if( iter > maxIter ) then print("Failed Iter Test and Gave Up @ " … system.getTimer()) return end isSafe = true for i = 1, #currentObjects do local obj = currentObjects[i] local vec = subVec( tx, ty, obj.x, obj.y ) local len2 = len2Vec( vec ) if( len2 < safeRadius2 ) then isSafe = false tx = mRand( minX, maxX ) ty = mRand( minY, maxY ) end end end local tmp = display.newCircle( tx, ty, objRadius ) currentObjects[#currentObjects+1] = tmp print("Created in iterations ", iter) print("Total objects: ", #currentObjects ) return tmp end – -- 4. Generate circles after a short delay – Make one randomly placed circle at a time – for i = 1, maxPieces do timer.performWithDelay( i * 30, function() local tmp = safelyPlaceCircle() if(tmp) then tmp:setFillColor( mRand(0,100)/100, mRand(0,100)/100, mRand(0,100)/100 ) end end ) end

Hi Roaminggamer,

the link is down. Could you upload it again? :slight_smile:

Hi.  The AskEdarchive is getting an overhaul to bring it up to spec with the current Corona SDK and in cases where it relied on SSK I am updating it to use SSK2.

I’ll make an announcement when this is all done, but until then, the code for that particular example is in the

  • AskEd July 2015 folder ====> 'overlapTest/'

    – ============================================================= – Answers to interesting Corona Forums Questions – ============================================================= – by Roaming Gamer, LLC. 2009-2015 (http://roaminggamer.com/) – ============================================================= display.setStatusBar(display.HiddenStatusBar) – Notes for this example ( not part of example ) – local notes = { “The person who wrote this question wanted to learn how to check”, “if objects are overlapping.”, “”, “This example generates a number of objects while ensuring none overlaps.”, } for i = 1, #notes do local tmp = display.newText( notes[i], 50, 20 + (i-1) * 30, native.systemFont, 22 ) tmp.anchorX = 0 end – -- 1. Some variables for the example local currentObjects = {} local w = display.contentWidth local h = display.contentHeight local centerX = w/2 local centerY = h/2 local minX = 25 local maxX = w - minX local minY = 200 local maxY = h - minX local maxPieces = 150 local objRadius = 20 local offset = 4 local buffer = -6 local safeRadius = (objRadius + offset) * 2 + buffer local safeRadius2 = safeRadius * safeRadius local maxIter = 150 – -- 2. Some math functions we’ll need local mRand = math.random local mSqrt = math.sqrt local mRad = math.rad local mCos = math.cos local mSin = math.sin local function subVec( x0, y0, x1, y1 ) return { x = x1 - x0, y = y1 - y0 } end local function lenVec( vec ) return mSqrt(vec.x * vec.x + vec.y * vec.y) end local function len2Vec( vec ) return vec.x * vec.x + vec.y * vec.y end – -- 3. A circle generator local function safelyPlaceCircle() local tx = mRand( minX, maxX ) local ty = mRand( minY, maxY) local iter = 0 local isSafe = false while( isSafe == false ) do iter = iter + 1 if( iter > maxIter ) then print("Failed Iter Test and Gave Up @ " … system.getTimer()) return end isSafe = true for i = 1, #currentObjects do local obj = currentObjects[i] local vec = subVec( tx, ty, obj.x, obj.y ) local len2 = len2Vec( vec ) if( len2 < safeRadius2 ) then isSafe = false tx = mRand( minX, maxX ) ty = mRand( minY, maxY ) end end end local tmp = display.newCircle( tx, ty, objRadius ) currentObjects[#currentObjects+1] = tmp print("Created in iterations ", iter) print("Total objects: ", #currentObjects ) return tmp end – -- 4. Generate circles after a short delay – Make one randomly placed circle at a time – for i = 1, maxPieces do timer.performWithDelay( i * 30, function() local tmp = safelyPlaceCircle() if(tmp) then tmp:setFillColor( mRand(0,100)/100, mRand(0,100)/100, mRand(0,100)/100 ) end end ) end