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:

1
2
3
4
5
6
7
8
9
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})
    end
 
timer.performWithDelay(0,squareSpawn,1)

When you create the squares with this code:

local square = display.newRect(50, 50, 100)

...you're setting things like the X and Y coords right after that:

1
2
square.x = -100
square.y = math.random(0,1024)

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)

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.

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

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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

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:

1
square:addPhysics(physics,'dynamic',{})
views:1290 update:2011/9/26 15:43:22
corona forums © 2003-2011