Best method for collision detection

Can anyone suggest a good method for collision detection for a top down, non-physics based game?
I’ve had a look at isSensor but can’t seem to get my head around how it works or if it would even be suitable.
At the moment I’m just using circles as place holder graphics and calculating the distance between them to see if there’s any overlap which then triggers the collision function, but I’m nearly at the point where I need to put in proper graphics which obviously aren’t all going to be circular and I’m pretty stumped on how to detect collisions.

[import]uid: 7841 topic_id: 14708 reply_id: 314708[/import]

Do what I do; use physics for the collision filters, but turn gravity to zero.

Here’s an example:

http://www.youtube.com/watch?v=ZiZCHlJDPdc

Here’s the table/chart you need:

http://developer.anscamobile.com/forum/2010/10/25/collision-filters-helper-chart

And here’s my code if you wish to see an example:

https://github.com/JesterXL/PlaneShooter [import]uid: 23693 topic_id: 14708 reply_id: 54428[/import]

I use the collision function below if I don’t need physics in my project.

Example of collision detection without requiring physics.

-- Ball object  
local ball = display.newImageRect("ballSmall.png", 12, 12)   
ball.x = 240; ball.y = 160  
ballSizeX = 12; ballSizeY = 12  
  
-- Paddle object  
local leftPaddle = display.newImageRect("paddleSmall.png", 12, 60)   
leftPaddle.x = 35; leftPaddle.y = 160  
paddleSizeX = 12; paddleSizeY = 60  
-- Collision detection  
local function collision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)  
 if (box1x \> (box2x + box2w)) then return false   
 elseif ((box1x + box1w) \< box2x) then return false   
 elseif (box1y \> (box2y + box2h)) then return false   
 elseif ((box1y + box1h) \< box2y) then return false   
 else return true   
 end  
end  
  
-- Check if ball hit paddle  
if collision(ball.x, ball.y, ballSizeX, ballSizeY, leftPaddle.x, leftPaddle.y, paddleSizeX, paddleSizeY) then  
 -- if ball hit paddle then do something   
end   

[import]uid: 53445 topic_id: 14708 reply_id: 54441[/import]

@jesterxl
Ahh, that’s sweet man! That looks perfect.
As soon as I get some time I’ll have a go at implementing it.
Of course finding the time isn’t easy at the moment as we’re getting ready to move house in a few weeks time.

Now I just need to get my head around sprite sheets :frowning: [import]uid: 7841 topic_id: 14708 reply_id: 54445[/import]

@jesterxl
Hi mate, I’ve had a proper read through of that now and it really does look simple. One thing though, how do you actually detect the collisions between the objects? I’ve got a main character (critter) and a table of enemies (enemies) so the filters are easy to set up, but how do you use them? I’ve set physics up with zero gravity but no idea where to go from here.

Cheers
[import]uid: 7841 topic_id: 14708 reply_id: 54568[/import]

It’s not the best method, but it’ll get you started.

  1. give each object you create a name so you can later uniquely identify it (Yes, there are better ways of doing this).
  2. look for that name in the collision event and do stuff

Example, when a Player’s bullet hit’s a bad guy. This code is inside the bad guy (here is the full class).

[lua]function onHit(self, event)
– TODO/FIXME: string names? Really? That’s great man…
if(event.other.name == “Bullet”) then
– TODO: create enemy death
–createEnemyDeath(self.x, self.y)
–print("ship is dead, ID: ", self.ID)
event.other:destroy()
elseif(event.other.name == “Player”) then
event.other:onBulletHit()
end
local smallShipDeathSoundChannel = audio.play(EnemySmallShip.smallShipDeathSound)
audio.setVolume(.4, {channel = smallShipDeathSoundChannel})
self:destroy()
end

img.collision = onHit
img:addEventListener(“collision”, img)[/lua]

img is the image of the bad guy. He’s been added to the physics via addBody. Notice the onHit collision code. It basically says “If the thing I hit is called ‘Bullet’, then I’m dead bro! Otherwise, if I hit the ‘Player’ then call his onBulletHit method. Either way, I hit something, explode and die and play the sound.”

You dig? [import]uid: 23693 topic_id: 14708 reply_id: 54796[/import]