Search code examples
luacollision-detectioncoronasdk

Corona SDK : How to identify the collision object?


I'm a beginner in Corona developping and i've to problem to identify the collision between 3 objects. In fact, this is my code :

The first object is my player : 

player = display.newImageRect("ballon.png", 150,170 )
player.anchorX = 0.5
player.anchorY = 0.5
player.x = display.contentCenterX - 450
player.y = display.contentCenterY+250
physics.addBody(player, "static", {density=0.1, bounce=0.1, friction=0.1,})
player.myName="player"
screenGroup:insert(player)

The second object is created within a function :

function addWindSlow(self,event)

height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windslow = display.newImage( "wind-neg.png")
windslow.x = display.contentWidth
windslow.y = height
windslow.speed = 4
windslow.myName="windslow"
physics.addBody(windslow, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windslow) end

function addWindFast(self,event)

height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windfast = display.newImage( "wind-pos.png")
windfast.x = display.contentWidth
windfast.y = height
windfast.speed = 4
windfast.myName="windfast"
physics.addBody(windfast, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windfast) end

And finally, the thirth object is the same but called "addWindFast() with a different image"

So i would like to know once my player is on collision with the windslow, i need to do an action... and if it's the object windfast, another action. That's my code :

function onCollision( event )


    if ( event.phase == "began" ) then
    if event.other.windslow and event.other.isVisible==true then
        print("windslow has been touch !")
    end

end

end

I found something about the pre-collision, I'll use it once i got the information how to "identify" the object...I'll need to prevent the collision and avoid it if the player is going to touch the windfast or windslow.

We thank you for all dude !


Solution

  • It looks like windslow and windfast are defined outside of onCollision event handler so you should compare to those:

    function onCollision( event )
        if ( event.phase == "began" ) then
            if event.other == windslow and event.other.isVisible==true then
                print("windslow has been touch !")
            end
            if event.other == windfast and event.other.isVisible==true then
                print("windfast has been touch !")
            end
        end
    end -- (missing from your post but probably in your code)
    

    The above assumes that you have registered for collision events on your player. You could also register a different collision handler on each wind:

    windslow:addEventListener("collision", onCollisionSlow)
    windfast:addEventListener("collision", onCollisionFast)
    

    In that case the two collision handlers could just check if event.other == player. The advantage of that is you are separating the code for each one. You could of course do the same with your player handler:

    function onCollisionSlow(other)
        print('collided with slow')
    end
    
    function onCollisionFast(other)
        print('collided with fast')
    end
    
    collisionHandlers = {
        [windslow] = onCollisionSlow, 
        [windfast] = onCollisionFast,
    }
    
    function onCollision( event )
        if ( event.phase == "began" ) then
            collisionFunc = collisionHandlers[event.other]
            if collisionFunc ~= nil and event.other.isVisible then
                collisionFunc(event.other)
            end
        end
    end