How do I know which object is event.object1 in Global Collision?

In a global collision there’s event.object1 and event.object2 – is there any way to tell ahead of time whether the alien will always be object1 and the planet object2?

Or do I need to do something like this:

[lua]

if event.object1.name == “alien” and event.object2.name == “planet” or event.object1.name == “planet” and event.object2.name == “alien” then

[/lua]

Know what I mean?

In a demo I’m working on now, the alien is always object1 – even when I switched things and created the planet object first. How does Corona decide which object is 1 and which one is 2 when there’s a collision?

 Jay

It’s box2d part how it is handled. Your method with names is good and enough

I am not aware of anything that would work except for something like this in global collision events. 

And I do not think that there is a native Box2D call available… I did not run into a situation like this before where you *need* global collision events, though. 

Order matters, watch the craziness. I’ve dealt with this and I’ve been all over the map with it. 

First, let’s try this

#1 run this

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

#2 Then try this. 

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

Notice which one prints to console. The order declared, is the order the collision happens - so it appears. Also notice that I even put 2 different collision pieces in one for alien then planet, and then planet and alien. 

Collision function:

local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

THEN I got crazy, and lets put your code snippet in :

**Note I changed  .name to .type to keep the same as my code above

local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision)

That removes the "alien. But wait, lets switch around the alien and planet!

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision) --[[local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision) --]]

Now the dang planet got removed, even using your logic.

I know I know, insane. I need a drink

I’m using this code:

[lua]

local function onCollision( event )

    if event.phase == “began” then

        if event.object1.type == “bumper” then

            local function wrap()

                bigRock.x = bumper[event.object1.opposite].x + event.object1.fudgex

                bigRock.y = bumper[event.object1.opposite].y + event.object1.fudgey

            end

            timer.performWithDelay ( 1, wrap )

        end

    end

end

[/lua]

…and no matter how I rearrange things, “bumper” is ALWAYS event.object1 – which is fine, but I don’t want to count on that unless I know it’s a certainty. 

I think I’ll probably switch to using a table listener because then I always know event.other is the other thing. :slight_smile:

 Jay

It’s box2d part how it is handled. Your method with names is good and enough

I am not aware of anything that would work except for something like this in global collision events. 

And I do not think that there is a native Box2D call available… I did not run into a situation like this before where you *need* global collision events, though. 

Order matters, watch the craziness. I’ve dealt with this and I’ve been all over the map with it. 

First, let’s try this

#1 run this

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

#2 Then try this. 

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

Notice which one prints to console. The order declared, is the order the collision happens - so it appears. Also notice that I even put 2 different collision pieces in one for alien then planet, and then planet and alien. 

Collision function:

local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision)

THEN I got crazy, and lets put your code snippet in :

**Note I changed  .name to .type to keep the same as my code above

local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision)

That removes the "alien. But wait, lets switch around the alien and planet!

-- Require in our storyboard, scene, and physics local physics = require("physics") physics.start(true) physics.setDrawMode("normal") --physics.setContinuous( true ) --Not sure if this will be needed, after a thorough testing on devices might take out. physics.setGravity(0, 10) physics.setPositionIterations(8) local group = display.newGroup() --====================================================================================== -- Set up display objects --====================================================================================== local alien = display.newCircle(100,100,20) -- bs values, this will change and be proper later. alien:setFillColor(255,0,0) physics.addBody( alien, "dynamic", {radius = 20} ) group:insert(alien) alien.type = "alien" local planet = display.newCircle(100,400,100) -- bs values, this will change and be proper later. physics.addBody( planet, "static",{radius = 20} ) group:insert(planet) planet.type = "planet" --====================================================================================== -- COLLISION DETECTION --======================================================================================- local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if event.object1.type == "alien" and event.object2.type == "planet" --print("alien and planet collide") or event.object1.type == "planet" and event.object2.type == "alien" ---print ("planet and alien collide") then display.remove(hit) hit = nil end end end Runtime:addEventListener("collision", onCollision) --[[local function onCollision(event) if event.phase == "began" then local agro = event.object1 local hit = event.object2 --print("agro.type, hit.type", agro.type, hit.type) if agro.type == "alien" and hit.type == "planet" then display.remove(hit) hit = nil print ("alien hitting planet") elseif agro.type == "planet" and hit.type == "alien" then display.remove(hit) hit = nil print ("planet hitting alien") end end end Runtime:addEventListener("collision", onCollision) --]]

Now the dang planet got removed, even using your logic.

I know I know, insane. I need a drink

I’m using this code:

[lua]

local function onCollision( event )

    if event.phase == “began” then

        if event.object1.type == “bumper” then

            local function wrap()

                bigRock.x = bumper[event.object1.opposite].x + event.object1.fudgex

                bigRock.y = bumper[event.object1.opposite].y + event.object1.fudgey

            end

            timer.performWithDelay ( 1, wrap )

        end

    end

end

[/lua]

…and no matter how I rearrange things, “bumper” is ALWAYS event.object1 – which is fine, but I don’t want to count on that unless I know it’s a certainty. 

I think I’ll probably switch to using a table listener because then I always know event.other is the other thing. :slight_smile:

 Jay