Question on collision and physics

Hi,

I have recently learned more about the physics by watching the video tutorial “Zero to Angry Birds in 30 minutes” and it was great! Now I am trying to learn about the collision and am a little confused. I was going through the documentation and saw the code below. My question is how exactly do I use this to check if any object hit a character? For example, if I made an Angry Birds type game how can i tell if any of the blocks hit the character? Will the example below fire every time one object hit another? And if so, I assume I can tell if the character was hit if it is either the object1 or object2 name right?

I’m just trying to see if I understand this or if I’m way off. Thanks!!

Warren

local crate1 = display.newImage( "crate.png", 100, 200 )  
physics.addBody( crate1, { density = 1.0, friction = 0.3, bounce = 0.2 } )  
crate1.myName = "first crate"  
  
local crate2 = display.newImage( "crate.png", 100, 120 )  
physics.addBody( crate2, { density = 1.0, friction = 0.3, bounce = 0.2 } )  
crate2.myName = "second crate"  
  
local function onCollision( event )  
 if ( event.phase == "began" ) then  
 print( "began: " .. event.object1.myName .. " & " .. event.object2.myName )  
 elseif ( event.phase == "ended" ) then  
 print( "ended: " .. event.object1.myName .. " & " .. event.object2.myName )  
 end  
end  
  
Runtime:addEventListener( "collision", onCollision )  

[import]uid: 184193 topic_id: 34209 reply_id: 334209[/import]

Hey Warren, I tend to use “event.other” and the object “type” to determine the response of collisions.

  
local function checkBubble(self, event)  
  
 if event.other.type == "spikey" or event.other.type == "wall" then  
  
 audio.play(popSound)  
 self:removeSelf()  
 self = nil  
 end  
end  
local function newBubble()  
  
 local bubble = display.newImage("images/bubble.png")  
 physics.addBody(bubble, "dynamic", { density = 1.5/contentScale, friction = 0.5, bounce = 1, radius = (bubble.width/2), filter = filter.bubble} )  
 bubble.collision = checkBubble  
 foreground:insert(bubble)  
 bubble:addEventListener("collision", bubble)   
 return bubble  
end  
  
player.type = "spikey"  
boundary.type = "wall"  

This code would create a bubble and cause it to pop whenever it hit an object you designated as “spikey” or “wall”. You could also make yours “player” or whatever.

This is just what I do. I’m relatively new to all of this so other developers might have other approaches that they would argue are better. Hope this works for you or at least gets you going in the right direction.

[import]uid: 136211 topic_id: 34209 reply_id: 136043[/import]

Hey Warren, I tend to use “event.other” and the object “type” to determine the response of collisions.

  
local function checkBubble(self, event)  
  
 if event.other.type == "spikey" or event.other.type == "wall" then  
  
 audio.play(popSound)  
 self:removeSelf()  
 self = nil  
 end  
end  
local function newBubble()  
  
 local bubble = display.newImage("images/bubble.png")  
 physics.addBody(bubble, "dynamic", { density = 1.5/contentScale, friction = 0.5, bounce = 1, radius = (bubble.width/2), filter = filter.bubble} )  
 bubble.collision = checkBubble  
 foreground:insert(bubble)  
 bubble:addEventListener("collision", bubble)   
 return bubble  
end  
  
player.type = "spikey"  
boundary.type = "wall"  

This code would create a bubble and cause it to pop whenever it hit an object you designated as “spikey” or “wall”. You could also make yours “player” or whatever.

This is just what I do. I’m relatively new to all of this so other developers might have other approaches that they would argue are better. Hope this works for you or at least gets you going in the right direction.

[import]uid: 136211 topic_id: 34209 reply_id: 136043[/import]