Collision Detection help

Hello! I am wondering if there is any way to use this… hole:addEventListener( “collision”, inhole ) … with 2 things. So basically collision between hole and ball, instead of just hole. I need to know because I have two balls on the screen and need to know which hits the hole.

Thanks!!
[import]uid: 86879 topic_id: 18934 reply_id: 318934[/import]

you can either attach listener to one object(and set it to respond for whatever objects you want) or create runtime function that will check collisions of two (or more) objects [import]uid: 16142 topic_id: 18934 reply_id: 72933[/import]

Hey! Thanks! Can you please elaborate on option one? [import]uid: 86879 topic_id: 18934 reply_id: 72940[/import]

you can set up it like that:
[lua]local obj = display.newRect(0,0,50,50)

local obj2 = display.newRect(O,0,50,50)
obj2.name = “obj2”

local obj3 = display.newCircle(0,0,50)
obj3.name = “obj3”

local function onCollision(self, event)
if event.other.name == “obj2” or event.other.name == “obj3” then
print(event.other.name)
end
end
obj.collision = onCollision
obj:addEventListener(“collision”, onCollision")
[lua]of course you’ll need to set up physics, i just didnt wrote it, you can do it yourself) [import]uid: 16142 topic_id: 18934 reply_id: 72943[/import]

I receive this error…

The file sandbox for this project is located at the following folder:
(/Users/Kurt/Library/Application Support/Corona Simulator/Crazy Putt-CBE76DEA36949BA4E154106AB1551BFB)
Runtime error
/Users/Kurt/Desktop/Crazy Putt/1p2.lua:328: attempt to index local ‘event’ (a nil value)
stack traceback:
[C]: ?
/Users/Kurt/Desktop/Crazy Putt/1p2.lua:328: in function
?: in function <?:215>
Runtime error
/Users/Kurt/Desktop/Crazy Putt/1p2.lua:328: attempt to index local ‘event’ (a nil value)
stack traceback:
[C]: ?
/Users/Kurt/Desktop/Crazy Putt/1p2.lua:328: in function
?: in function <?:215>

My code is set as…
local function onCollision(self, event)

if event.other.name == “ball” then
ballonein = true
remove1 = physics.removeBody( ball )
else if event.other.name == “ball2” then
balltwoin = true
remove2 = physics.removeBody( ball2 )

end

if ballonein == true then
if balltwoin == true then

end
end
end
end

hole:addEventListener( “collision”, onCollision ) [import]uid: 86879 topic_id: 18934 reply_id: 72946[/import]

you forgot the
[lua]obj.collision = onCollision[/lua]
line, otherwise it will not respond to self and will get you an error for event [import]uid: 16142 topic_id: 18934 reply_id: 72950[/import]

I am still getting that error =( [import]uid: 86879 topic_id: 18934 reply_id: 72953[/import]

try this, it should works perfectly

[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)

local obj1 = display.newRect(0,0,50,50)
physics.addBody(obj1)
obj1.x = 50
obj1:setFillColor(255,0,0)
obj1.name = “obj1”

local obj2 = display.newRect(0,0,30,30)
physics.addBody(obj2)
obj2.y = 80
obj2.name = “obj2”

local obj3 = display.newRect(0,0,60,40)
physics.addBody(obj3)
obj3.x = 150
obj3.name = “obj3”

local function drag(event)
if event.phase == “moved” then
obj1.x = event.x
obj1.y = event.y
end
end

obj1:addEventListener(“touch”, drag)

local function onCollision(self,event)
if event.phase == “began” then
if event.other.name == “obj2” or event.other.name == “obj3” then
print(self.name… " colliding with: "… event.other.name)
end
end
end

obj1.collision = onCollision
obj1:addEventListener(“collision”, obj1)[/lua] [import]uid: 16142 topic_id: 18934 reply_id: 72957[/import]

Here is the code… and it isn’t working =( No error, it just goes over the hole…Thanks so much for your help

local function onCollision(self, event)
if event.phase == “began” then
if event.other.name == “ball” then
ballonein = true
remove1 = physics.removeBody( ball )
ball.isVisible = false
audio.play(sinkputt)
ballincup.isVisible = true
transition.dissolve( ballincup, nil, 800 )

else if event.other.name == “ball2” then
balltwoin = true
remove2 = physics.removeBody( ball2 )
ball2.isVisible = false
audio.play(sinkputt)
ballincup2.isVisible = true
transition.dissolve( ballincup2, nil, 800 )
end

if ballonein == true then
if balltwoin == true then

end
end
end
end
end

hole.collision = onCollision
hole:addEventListener( “collision”, hole ) [import]uid: 86879 topic_id: 18934 reply_id: 72962[/import]

Hi,

I don’t think you need to specify the collision handler two times.
Using the following works fine on my end:
[lua]local function onColTest(event)
print(event.phase … ’ - ’ … event.other.type);
end
shipGroup:addEventListener(“collision”, onColTest);[/lua]

using:
[lua]shipGroup.collision = onColTest;[/lua]
is another way to define the collision handler.

About your code… You could use ELSEIF instead of ELSE in your case. Make sure that you set the name of the balls in your code:
[lua]ball.name = “ball”;
ball2.name = “ball2”;[/lua]

I would also make the “ballincup” and “ballonein” to be properties of the ball itself. That way you could handle the collisions as such:
[lua]local function onCollision(event)
if event.phase == “began” then
local obj = event.other;
if (obj.name == “ball” or obj.name == “ball2”) then
obj.ballin = true;
remove = physics.removeBody(obj);
obj.isVisible = false;
audio.play(sinkputt);
obj.ballincup.isVisible = true;
transition.dissolve( obj.ballincup, nil, 800 );
end
end
end

hole:addEventListener(“collision”, onCollision);[/lua]
and then you could check for each ball if they are in cup etc…

Make sure you use print(); to help you debug.

Kindly, [import]uid: 69375 topic_id: 18934 reply_id: 72994[/import]