Collision Help

The part I am having trouble in is when a ball hits an object, I want that object to be eliminated and that game is over.

– Example
local physics = require “physics”
physics.start()
physics.setGravity(0, 2)

–Status Bar
display.setStatusBar(display.HiddenStatusBar)

local sky = display.newImage( “sky.png” )
local grass = display.newImage( “grass.png” )
grass.y = 480
physics.addBody( grass, “static”, { friction=0.3 } )

local orb = display.newImage( “orb.png” )
orb.x, orb.y = 200, 100
orb.rotation = 50
physics.addBody( orb, { bounce=0.3, radius=15 } )

local function spawnOrb()
local orb = display.newImage( “orb.png” )
orb.x = math.random( 320 )
orb.y = -100
orb.rotation = 10
physics.addBody( orb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end

timer.performWithDelay( 9000, spawnOrb, 20 )

local bomb = display.newImage( “bomb.png” )
bomb.x, bomb.y = 200, 100
bomb.rotation = 50
physics.addBody( bomb, {bounce=0.3, radius=15 } )

local function spawnBomb()
local bomb = display.newImage( “bomb.png” )
bomb.x = math.random( 320 )
bomb.y = -100
bomb.rotation = 10
physics.addBody( bomb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end

display.setStatusBar (display.HiddenStatusBar)
– Hides the status bar

–Penguin
local penguin = display.newImage(“penguin.png”)
penguin.x=240
penguin.y=225
physics.addBody(penguin,“static”,{radius=45})
timer.performWithDelay( 1500, spawnBomb, 5 )

–Define wall graphics (rectangles)
local leftWall = display.newRect( 0, 0, 1, display.contentHeight )
local rightWall = display.newRect( display.contentWidth, 0, 1, display.contentHeight )

–Turn wall graphics into physical bodies
physics.addBody(leftWall, “static”, { bounce = 0.1} )
physics.addBody(rightWall, “static”, { bounce = 0.1} )
[import]uid: 132369 topic_id: 23947 reply_id: 323947[/import]

Please post your code inside < lua > and < /lua > tags so it can be read easily.

Try this simple little tutorial, will show you all about collision detection: http://techority.com/2011/06/19/corona-for-newbies-part-4-physics/

Peach :slight_smile: [import]uid: 52491 topic_id: 23947 reply_id: 96508[/import]

Sorry Peach, I did not get any help from the link. Can you please give me some lines of codes for help? [import]uid: 132369 topic_id: 23947 reply_id: 96651[/import]

I want the penguin to be removed when touched by an object. I believe it has something to do with removeSelf(). But I don’t know the full lines of codes. Please Help! [import]uid: 132369 topic_id: 23947 reply_id: 96720[/import]

Try looking around http://www.learningcorona.com/ I’m sure you will find answers to most of the stuff you have questions for.

As for what you are trying to do try something like

[code]
penguin.myName = “penguin”
orb.myName = “orb”

local function onCollideObject(event)
if ( event.phase == “began” ) then
if event.object1.myName == “bomb” and event.object2.myName == “penguin” then

event.object2:removeSelf()
event.object2 = nil

end
if event.object1.myName == “orb” and event.object2.myName == “penguin” then

event.object2:removeSelf()
event.object2 = nil

end
end
end

Runtime:addEventListener( “collision”, onCollideObject)
[/code] [import]uid: 77199 topic_id: 23947 reply_id: 96771[/import]

I think you would want to use display.remove(object) vs object:removeSelf()

From what I understand, the display.remove(object) will check for nil before trying to remove it vs object:removeSelf() which will error out with something like “Attempt to index a nil object” if the object is already nil.
I’ve tested this out,a display.remove(object) seems to be more “safe” now and less errors on my end (because of the nil check).

Keep in mind, object means the name of your object so like display.remove(box) for example will remove the display object (like a local variable display object) called box.
That’s my understanding of things, I just recently got informed of that difference between the 2 methods of removing something. Hopefully that is helpful?

If not, then…well it’s not. haha!

-ng [import]uid: 61600 topic_id: 23947 reply_id: 96777[/import]

Thank you so much. I have one last concern. If I want to make it so that every spawning orb and bomb does the same thing , what can i do ? I already have it set to spawn orbs and bombs. [import]uid: 132369 topic_id: 23947 reply_id: 96960[/import]

local orb = display.newImage( “orb.png” )
orb.x, orb.y = math.random( 320 ), 100
orb.rotation = 50
physics.addBody( orb, { bounce=0.3, radius=15 } )

local function spawnOrb()
local orb = display.newImage( “orb.png” )
orb.x = math.random( 320 )
orb.y = -100
orb.rotation = 10
physics.addBody( orb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end

timer.performWithDelay( 9000, spawnOrb, 9000 )

local bomb = display.newImage( “bomb.png” )
bomb.x, bomb.y = math.random( 320 ), -100
bomb.rotation = 50
physics.addBody( bomb, {bounce=0.3, radius=15 } )

local function spawnBomb()
local bomb = display.newImage( “bomb.png” )
bomb.x = math.random( 320 )
bomb.y = -100
bomb.rotation = 10
physics.addBody( bomb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end

–This is my spawning codes [import]uid: 132369 topic_id: 23947 reply_id: 96962[/import]

Please help someone ^^^ [import]uid: 132369 topic_id: 23947 reply_id: 97765[/import]

How much did you really try to figure this out yourself? And the second thing is, you never specified what exactly you want your orbs/bombs to do, you just said you want them to do the same thing. [import]uid: 77199 topic_id: 23947 reply_id: 97767[/import]

I actually have been trying for a long time. I want it so when my orbs/bombs hit the penguin, the penguin disappears. I have used your codes you posted on the previous comments and it worked only for the first orb and the first bomb [import]uid: 132369 topic_id: 23947 reply_id: 97769[/import]

Give me a few minutes [import]uid: 77199 topic_id: 23947 reply_id: 97773[/import]

no problem. thank you [import]uid: 132369 topic_id: 23947 reply_id: 97776[/import]

my current code for spawning is
[lua]local orb = display.newImage( “orb.png” )
orb.x, orb.y = math.random( 320 ), -100
orb.rotation = 10
physics.addBody( orb, { bounce=0.3, radius=15 } )

local function spawnOrb()
local orb = display.newImage( “orb.png” )
orb.x = math.random( 320 )
orb.y = -100
orb.rotation = 10
physics.addBody( orb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end

timer.performWithDelay( 1500, spawnOrb, 5 )

local bomb = display.newImage( “bomb.png” )
bomb.x, bomb.y = math.random( 320 ), -100
bomb.rotation = 50
physics.addBody( bomb, {bounce=0.3, radius=15 } )

local function spawnBomb()
local bomb = display.newImage( “bomb.png” )
bomb.x = math.random( 320 )
bomb.y = -100
bomb.rotation = 10
physics.addBody( bomb, { density=2.0, friction=0.5, bounce=0.3, radius=15 } )
end
timer.performWithDelay( 1500, spawnBomb, 5 ) [import]uid: 132369 topic_id: 23947 reply_id: 97780[/import]

local spawnTrue = true;  
display.setStatusBar( display.HiddenStatusBar )  
local physics = require "physics"  
physics.start()  
--physics.setDrawMode ( "hybrid" )  
physics.setDrawMode ( "normal" )  
physics.setGravity( 0, 10 )  
  
local ground = display.newRect( 0, 0, 320, 50)  
ground:setReferencePoint( display.BottomLeftReferencePoint )  
ground.x, ground.y = 0, 320  
  
local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }  
physics.addBody( ground, "static", { friction=1.0, density=1.0, bounce=0 } )  
  
local block = display.newRect(200, 180, 50, 50)  
physics.addBody( block, "dynamic", { friction=50, density=50, bounce=0} )  
block.myName = "block"  
  
  
local function spawnShit()  
 if( spawnTrue == true )then  
 local randomPos = math.random ( 0, 320 )  
 character = display.newImage( "blueball.png" )  
 character.x = randomPos  
 character.y = 230  
 character.myName = "character"  
 physics.addBody(character , "static", {isSensor = true})  
 end  
  
end  
  
timer.performWithDelay( 1500, spawnShit, 500 )  
  
local function onCollideObject(event)  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "block" and event.object2.myName == "character" then  
   
 event.object2:removeSelf()  
 event.object2 = nil  
  
 end   
 end  
end  
   
Runtime:addEventListener( "collision", onCollideObject)  
  
   

Im new to this as well but it doesnt seem normal to me that youd have local orb bla bla spawn at the start and then a function that makes another local called orb exactly like the earlier one, might wanna name those seperately but dont take my word on it. Got it to work for you though, just added the local spawnTrue = true and if(spawnTrue = true)then statement and it should work. If you have any questions feel free to ask.

Edit: I hope my code doesnt confuse you, the ‘character’ is just the orb or the bomb and the ‘block’ would be the penguin in your case. [import]uid: 77199 topic_id: 23947 reply_id: 97786[/import]

Thank you so much. I really appreciate it [import]uid: 132369 topic_id: 23947 reply_id: 97787[/import]

Sorry to bother you again, but an issue popped up. my orbs/bombs are suppose to be falling. In the codes you gave my when they started spawning, the spawned in the middle of the screen and just stood there. [import]uid: 132369 topic_id: 23947 reply_id: 97792[/import]

It’s because the ‘character’ in my code is set to ‘static’, you want to set it to ‘dynamic’. you want your orbs and bombs to be dynamic and set gravity so that your objects fall in the direction you want. [import]uid: 77199 topic_id: 23947 reply_id: 97795[/import]

ahh i see… I fixed it but one more issue. Sorry about this. When the orbs touches the block (penguin), the orb is the one that disappears, not the penguin.
[import]uid: 132369 topic_id: 23947 reply_id: 97800[/import]

That must be from this code:

if event.object1.myName == "orb" and event.object2.myName == "penguin" then  
   
 event.object2:removeSelf()  
 event.object2 = nil  
  
end   

What this code means is. object1 is the first object and object2 is a second object, when the two collide it calls removeSelf on object2. So whatever object2 is set to, that is the thing that gets removed. So object1 should be the orb and object2 should be the penguin so that the penguin gets removed. [import]uid: 77199 topic_id: 23947 reply_id: 97805[/import]