Collision Listener for matching objetcts

i tried to add a collision listener to my spawned objects but i think i may be missing a step somewhere. 

i don’t receive any errors but instead the objects just phase through each other and nothing happens. 

i am also attempting to get the objects to only work together if they match. 

for example.  when object1.png spawns another object with a different image also named object1.png in a  different folder spawns  only these two objects should be able to perform a collision, but i cant play with that until i get the collision listener working in general. 

here is my code

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) math.randomseed(os.time()) local lives = 3 local score = 0 local died = false local ObjectsTable = {} local newObject local newObject2 local gameLoopTimer local livesText local ScoreText local delayRate = 3000 local fallRate = 100 local backGroup = display.newGroup() local mainGroup = display.newGroup() local uiGroup = display.newGroup() local backGround = display.newImageRect(backGroup, "Images/BackGround1.png", display.contentWidth, display.contentHeight) backGround.x = display.contentCenterX backGround.y = display.contentCenterY if system.getInfo( "androidApiLevel" ) and system.getInfo( "androidApiLevel" ) \< 19 then native.setProperty( "androidSystemUiVisibility", "lowProfile" ) else native.setProperty( "androidSystemUiVisibility", "immersiveSticky" ) end livesText = display.newText( uiGroup, "Lives: ".. lives, display.contentCenterX - 120,15, native.systemFont, 20 ) ScoreText = display.newText(uiGroup, "Score: ".. score,display.contentCenterX + 120, 15, native.systemFont, 20) local function updateText() livesText.text = "Lives: " .. lives ScoreText.text = "Score: " .. score end local function leftRight() local side = {140 , -140} return side[math.random(1, #side)] end -------------------------------------------------------------------------------- local function ObjectMatch() newObject2 = display.newImageRect(mainGroup,"Objects2/Object" .. randomNum ..".png", 69.2 , 60.8) newObject2.x = display.contentCenterX - leftRight() newObject2.y = display.contentCenterY + math.random ( -300, 300) physics.addBody( newObject2, "static", {radius=10 , isSensor=true}) newObject2.myName = "SubObject" .. randomNum end local function ObjectMatch2(randomNum) newObject3 = display.newImageRect(mainGroup,"Objects2/Object" .. randomNum .. ".png", 69.2 , 60.8) newObject3.x = display.contentCenterX + 140 newObject3.y = display.contentCenterY + math.random ( -300, 300) end -------------------------------------------------------------------------------- local function createObjects() randomNum = math.random(18) newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8) table.insert( ObjectsTable, newObject ) physics.addBody( newObject, "dynamic", {radius = 10} ) newObject.myName = "Object" .. randomNum ObjectMatch(randomNum) --ObjectMatch2(randomNum) -------------------------------------------------------------------------------- local function dragObject ( event ) newObject = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( newObject, event.id ) newObject.isFocus = true newObject.touchOffsetX = event.x - newObject.x newObject.touchOffsetY = event.y - newObject.y elseif ( newObject.isFocus ) then if ( "moved" == phase) then newObject.x = event.x - newObject.touchOffsetX newObject.y = event.y - newObject.touchOffsetY elseif ( "ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus(newObject, nil ) newObject.isFocus = false end end return true end newObject:addEventListener( "touch", dragObject) -------------------------------------------------------------------------------- local whereFrom --from top newObject.y = -60 newObject.x = display.contentCenterX - math.random(-55, 60) -- keeps Objects within middle of screen newObject:setLinearVelocity( 0, fallRate) end local function gameLoop() createObjects() for i = #ObjectsTable, 1, -1 do local thisObject = ObjectsTable[i] if ( thisObject.x \< -100 or thisObject.x \> display.contentWidth + 100 or thisObject.y \< -100 or thisObject.y \> display.contentHeight + 100) then display.remove( thisObject ) table.remove( ObjectsTable, i ) end end end gameLoopTimer = timer.performWithDelay( delayRate, gameLoop, 0 ) -- param one sets delay between new Object spawn local function onCollsion( event ) if ( event.phase == "began") then local obj1 = event.object1 local obj2 = event.object2 if ((obj1.myname == "Object" and obj2.myName == "SubObject") or (obj1.myName == "SubObject" and obj2.myName == "Object")) then display.remove( obj1 ) display.remove( obj2 ) for i = #ObjectsTable, l, -l do if (ObjectsTable[i] == obj1 or ObjectsTable[i] == obj2) then table.remove ( ObjectsTable, i ) break end end score = score + 1 ScoreText.text = "Score: " .. score end end end Runtime:addEventListener( "collison", onCollsion)

@veracode I’m not quite following.  Can you explain one more time what you are trying to achieve.  Something like:

  1. object one spawns identical object

  2. those two object can collide.

  3. object 2 spawns an non-identical object

  4. those non-identical objects can’t collide.

correct, maybe its easier if the names are more identifiable.

folder one contains a1,b1,c1,d1 and so on, folder two also contains the same content a1,b1,c1,d1.

so when the code runs it will spawn 

a1(folder one) & a1 (folder two)    then it will loop spawning the next selection randomly 

d1(folder one) & d1(folder two)    

a1 and a1 should be allowed to collide but a1 and d1 should not

if the names match they should be able to collide, if they do not then nothing should occur.

currently nothing occurs no matter what objects collide nothing at all happens they simply phase through one another. 

**Update**
 
so i was able to get the collision listener to work … sort of the main reason nothing was happening was because of a misspelling
 

Runtime:addEventListener( "collison", onCollsion)

collison … should actually be collision … wanted to slam my head into my desk after catching this one.
 
 
now that i get some sort of response , its a bit odd.
 
when A1 (folder one) spawns and A1 (folder two) spawns  they don’t collide the hit boxes just run into each other doing nothing but, whatever obeject spawns next say K1. K1 can collide with A1\

the red text was my initial thought, but after some tinkering it seems like it almost completely random what will and wont collide.  

Are you checking everything in the “hybrid” physics drawmode?

from what i can tell everything is working as far as the physics go.

objectmatch is spawning static green hit boxes

and createobject is spawning dynamic orange hitboxes 

as of now the hit boxes will touch and not phase through each other anymore but only some of the hit boxes will register the oncollision(event) function. its almost every other object that works 

was there something specific you thought i should check for?

How fast are your objects moving?

If you could post a video in “hybrid” mode that would be helpful.

will do i actually already have a video of the debug mode.

https://drive.google.com/file/d/10gj8gr8bObGF_UF-kNDWuegN-b0iG9AS/view

i will add one of the hybrid. 

hybrid mode 

https://drive.google.com/open?id=1YrTSIwMdBN5ieeA50Y3dfHaMDXW9EfkKc3

changed link i broke the other one 

i resolved the issues with some object colliding only every other spawn… the issue was within the onCollifion function if statement

the if statement needed to be reversed in order to run properly, the only thing im working on now is verification.

meaning a1 and a1 can collide but a1 and anything else can not.

isSensor = true in your ObjectMatch( ) function

That means it will travel through other objects

[lua]

local function ObjectMatch()

      physics.addBody( newObject2, “static”, {radius=10 , isSensor=true})

end

[/lua]

yes i do understand that, this is working as i expect. the objects once they touch perform the onCollision function.

**update**

i tried to get the collision listener to check if the names of the spawned files matched  but there does not seem to be a built in way to do this. 

i tried just having the code read the file name then react based on that for example 

if (obj1 == objects/object1.png and obj2 == objects2/object1.png) or 

   (obj1 == objects2/object1.png and obj2 == objects/object1.png) then

display.remove (obj1)

display.remove (obj2)

but doing this did not work. the objects just phase through each other.

the closest i got to being able to make only objects that match collide was with a custom function i pulled from stack overflow which read and compared contents in table, this worked so long there were not multiples of the same objects on the screen and if there were and those object collided then the rest of the object stopped colliding with their match all together.