How do you write collision detection for multiple objects of the same name?

I have a function that randomly spawns objects (squares) zipping across the screen from left to right, at random screen heights. I then have a draggable object (a circle) that I want to change color if struck by one of the randomly spawning squares. I don’t want there to be physics applied to these objects. Does anyone have any ideas on how to write some sort of code that detects when my circle has collided with one of the spawning squares? My square spawning code can be boiled down to something like this:

local function squareSpawn (event) timer.performWithDelay(math.random(20,35),squareSpawn) local square = display.newRect(50, 50, 100) square.x = -100 square.y = math.random(0,1024) transition.to(square, {time = 1500, delay = 0, x = square.x + 968, onComplete=function() square :removeSelf() end}) endtimer.performWithDelay(0,squareSpawn,1)[/code]And my drag function can be boiled down to something like this:local circle = display.newCircle(50, 50, 100)circle:setFillColor(255,255,255)function moveCircle( event ) circle.x = event.x circle.y = event.yendRuntime:addEventListener("touch", moveCircle)[/code]Does anyone have any knowledge on how to write a simple collision code similar to this:local sqrt = math.sqrt local dx = square.x - circle.x;local dy = square.y - circle.y; local distance = sqrt(dx*dx + dy*dy); if distance < 32 then --Collidedend[/code]but for multiple objects? Any help or suggestions would be much appreciated.Thank youSteven [import]uid: 79394 topic_id: 14186 reply_id: 314186[/import]

When you create the squares with this code:

[lua]local square = display.newRect(50, 50, 100)[/lua]

…you’re setting things like the X and Y coords right after that:

[lua]square.x = -100
square.y = math.random(0,1024)[/lua]

There’s nothing to stop you from adding a name to that object:

[lua]square.myname = “Fred”[/lua]

Of course, every square would have the same name so you might want to increment a counter so your square names would start at 1, then 2, 3, etc.

Then when a square collides you can check the value of square.myname to see which object it was.

Of course, I could have completely misunderstood what you meant, but I hope that helps. :slight_smile:

Jay
[import]uid: 9440 topic_id: 14186 reply_id: 52259[/import]

it will be a huge overhead if we wan’t to manually code the collisions. using physics will be the best option here.
try the below code to get the result you want to achieve.
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)

local function squareSpawn (event)
local square = display.newRect(50, 50, 25,25)
square.x = -100
square.y = math.random(0,1024)
physics.addBody(square,“dynamic”,{isSensor = true})
transition.to(square, {time = 1500, delay = 0, x = square.x + 968, onComplete=function() square :removeSelf() end})
end
timer.performWithDelay(math.random(20,35),squareSpawn,0)

local circle = display.newCircle(50, 50, 20)
circle:setFillColor(255,255,255)
physics.addBody(circle,“kinematic”)
function moveCircle( event )
circle.x = event.x
circle.y = event.y
end
Runtime:addEventListener(“touch”, moveCircle)

local function onCircleCollision()
print(“collided with square”)
end
circle:addEventListener(“collision”,onCircleCollision)[/lua] [import]uid: 71210 topic_id: 14186 reply_id: 52261[/import]

As Renjith said, the path of least resistance here would be physics.

What you want to do will be fairly simple and clean this way, rather than messy and needlessly complex. [import]uid: 52491 topic_id: 14186 reply_id: 52270[/import]

Hear hear, the kiss principle is in full effect on this one:)

Thanks so much Peach and Renjith, and J.A.Whye for your suggestion as well.

Steven [import]uid: 79394 topic_id: 14186 reply_id: 52273[/import]

Thanks for that snippet, I have been using animation rather than physics and having to handle my own hit detection as a result. You gave me a vital clue!

cheers [import]uid: 84102 topic_id: 14186 reply_id: 52274[/import]

you could use physics. but you stated that you don’t want to.
there’s a simple solution for that: use Arrays (resp. tables)!

Every time you spawn an object, throw that into a table:

local bullets = {}  
local bCount = 0  
  
function fireBullet(theX, theY)  
  
 local bullet = {} -- create a local bullet table  
 -- create new bullet object  
 bullet.object = display.newImage("bullet.png",theX,theY)  
  
 bCount = bCount + 1 -- bullet counter goes up  
 if bCount \> 300 then   
 bCount = 0 -- ...until it reaches a limit  
 end  
  
 if bullets[bCount] ~= nil then  
 -- if there's a bullet at that index,   
 -- delete it from the bullets table  
 bullets[bCount] = nil   
 end  
 -- then add the new bullet object to the table  
 bullets[bCount] = bullet   
  
end  

now you just have to iterate through that table and check for collisions in an enterFrame method:

  
function hitTestObjects(obj1, obj2)  
 local dx = obj1.x - obj2.x  
 local dy = obj1.y - obj2.y  
 local distance = math.sqrt(dx\*dx + dy\*dy)  
 if distance \< 40 then -- 40px radius  
 return true  
 else  
 return false  
 end  
end  
function myEnterFrameMethod ()  
  
 for i,valBul in pairs(bullets) do  
  
 if hitTestObjects(valBul.object, myCircle) then  
 -- bullet object hits myCircle  
 -- remove object from screen and table  
 valBul.object:removeSelf()  
 bullets[i] = nil  
 end  
  
 end  
  
end  
  

this works very nice and fast and completely without physics.
I use this method in my iOS-game Parallax on dozens of bullet and enemy objects, in case you want to take a look how good the performance is… :wink:

cheers,
-finefin [import]uid: 70635 topic_id: 14186 reply_id: 52280[/import]

Hi Renjith,

Your code works as advertised. Thank you! Now I am faced with a new obstacle that I thought I’d run by you. Rather than objects, I’m actually using sprites, and I need the “hit-box” of these sprites to be considerably smaller than the sprites themselves. To do this, I am utilizing a Spriteloq API that allows you to choose your own physics shape for your sprites that can be different than the shape of the sprites themselves. So even though my sprites are squares, I can make their physical “bodies” smaller rectangles within these squares. The problem is, to get my code to recognize these “bodies”, I had to add some line of code like this:

square:addPhysics(physics,'dynamic',{})[/code]and now the squares are bounding off each other when their "bodies" overlap or collide. Is there any way you know of to remove the physics effects from these objects?Thanks,Steven [import]uid: 79394 topic_id: 14186 reply_id: 53033[/import]