Collision Problem

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

     

local w = display.contentWidth
local h = display.contentHeight
local cx = display.contentCenterX
local cy = display.contentCenterY

elements = display.newGroup ()
elements.anchorChildren = true
elements.anchorX = 0
elements.anchorY = 1
elements.x = 0
elements.y = 0

gameStarted = false

local floor = display.newRect( 0, 0, 320, 80 )
floor.x = cx
floor.y = cy
physics.addBody( floor, “static”, { bounce=0} )
 

local player = display.newRect( 80, 80, 20, 20 )
player:setFillColor( 0, 0, 1 )
physics.addBody( player, “dynamic”, { density=3.0, friction=10.5, bounce=0 } )

function onCollision ( event )
     if ( event.phase == “began” ) then
          print (“1”)
     end
end

function speedUp (event)
     if event.phase == “began” then
          if gameStarted == false then
               addRedTimer = timer.performWithDelay ( 1800, addRed, -1)
               moveRedTimer = timer.performWithDelay ( 20, moveRed, -1)
               gameStarted = true
          end
     end
end

function moveRed ()
     for a = elements.numChildren, 1, -1 do

          if ( elements[a].x > -100 ) then
               elements[a].x = elements[a].x - 7
          else
               elements:remove (elements[a])
               elements[a] = nil
          end
     end
end

function addRed ()

     Red = display.newRect( 90, 80, 20, 20 )
     Red.x = 320
     Red.y = 180
     Red:setFillColor( 1, 0, 0 )
     physics.addBody( Red, “static”, { density=3.0, friction=0.5, bounce=0 } )
     elements:insert (Red)

end

function PlayerJump ( event )
     if event.phase == “began” then
          player:applyForce ( 0, -251, player.x, player.y )
     end
end

Runtime:addEventListener (“touch”, PlayerJump)
Runtime:addEventListener (“touch”, speedUp)

player:addEventListener (“collision”, onCollision)

[/lua]

So my problem is about my player’s collisions

As you see, Player is dynamic. The Red and The Floor are static

What I wrote is when collision on player, It print (“1”)

However, it print (“1”) when player hit floor and red

What i want is to print (“1”) when player hit the red, and not print anything when player hit the floor

Thanks alot 

I think you can add conditions in your collision listener function and properties to yours physics object.

Like: red.name = “red”, player.name = “player”

and inside your listener:

local function collisionDetect( self, event )     if ( event.phase == "began" ) then         if( self.name == "player" and event.other.name == "red") then print(1); end

or your could use collision filter, there’s a tutorial in the blog and sample code with the corona sdk.

Im sorry, I dont really understand how this work

[lua]local function collisionDetect( self, event )
if ( event.phase == “began” ) then
if( self.name == “player” and event.other.name == “red”) then
print(1);

end[/lua]

can u be more specific

Ok!

So, you want to print only when the player hits the red rectangle, right?

In order to do that, we have to add one more parameter in the function collisionDetect called “(self, event)” 

The self will be the player, and the event will contain the informations of who collides with your player. 

Now that we have the information of who collides, we want to check if it’s the floor or the rect so the function can print, ok?

That kind of information we have to add to each physics object of your main…

Liike the example i gave you: 

player.name = “player”.

Here’s the code so you can test: 

local physics = require ("physics") physics.start() local w = display.contentWidth local h = display.contentHeight local cx = display.contentCenterX local cy = display.contentCenterY elements = display.newGroup () elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 gameStarted = false local floor = display.newRect( 0, 0, 320, 80 ) floor.x = cx floor.y = cy floor.name = "floor" physics.addBody( floor, "static", { bounce=0} ) local player = display.newRect( 80, 80, 20, 20 ) player:setFillColor( 0, 0, 1 ) player.name = "player" physics.addBody( player, "dynamic", { density=3.0, friction=10.5, bounce=0 } ) function onCollision ( self, event ) if ( event.phase == "began" ) then if( self.name == "player" and event.other.name == "red") then print ("1") end end end function speedUp (event) if event.phase == "began" then if gameStarted == false then addRedTimer = timer.performWithDelay ( 1800, addRed, -1) moveRedTimer = timer.performWithDelay ( 20, moveRed, -1) gameStarted = true end end end function moveRed () for a = elements.numChildren, 1, -1 do if ( elements[a].x \> -100 ) then elements[a].x = elements[a].x - 7 else elements:remove (elements[a]) elements[a] = nil end end end function addRed () Red = display.newRect( 90, 80, 20, 20 ) Red.x = 320 Red.y = 180 Red:setFillColor( 1, 0, 0 ) Red.name = "red" physics.addBody( Red, "static", { density=3.0, friction=0.5, bounce=0 } ) elements:insert (Red) end function PlayerJump ( event ) if event.phase == "began" then player:applyForce ( 0, -251, player.x, player.y ) end end Runtime:addEventListener ("touch", PlayerJump) Runtime:addEventListener ("touch", speedUp) player.collision = onCollision player:addEventListener ("collision")

I think you can add conditions in your collision listener function and properties to yours physics object.

Like: red.name = “red”, player.name = “player”

and inside your listener:

local function collisionDetect( self, event )     if ( event.phase == "began" ) then         if( self.name == "player" and event.other.name == "red") then print(1); end

or your could use collision filter, there’s a tutorial in the blog and sample code with the corona sdk.

Im sorry, I dont really understand how this work

[lua]local function collisionDetect( self, event )
if ( event.phase == “began” ) then
if( self.name == “player” and event.other.name == “red”) then
print(1);

end[/lua]

can u be more specific

Ok!

So, you want to print only when the player hits the red rectangle, right?

In order to do that, we have to add one more parameter in the function collisionDetect called “(self, event)” 

The self will be the player, and the event will contain the informations of who collides with your player. 

Now that we have the information of who collides, we want to check if it’s the floor or the rect so the function can print, ok?

That kind of information we have to add to each physics object of your main…

Liike the example i gave you: 

player.name = “player”.

Here’s the code so you can test: 

local physics = require ("physics") physics.start() local w = display.contentWidth local h = display.contentHeight local cx = display.contentCenterX local cy = display.contentCenterY elements = display.newGroup () elements.anchorChildren = true elements.anchorX = 0 elements.anchorY = 1 elements.x = 0 elements.y = 0 gameStarted = false local floor = display.newRect( 0, 0, 320, 80 ) floor.x = cx floor.y = cy floor.name = "floor" physics.addBody( floor, "static", { bounce=0} ) local player = display.newRect( 80, 80, 20, 20 ) player:setFillColor( 0, 0, 1 ) player.name = "player" physics.addBody( player, "dynamic", { density=3.0, friction=10.5, bounce=0 } ) function onCollision ( self, event ) if ( event.phase == "began" ) then if( self.name == "player" and event.other.name == "red") then print ("1") end end end function speedUp (event) if event.phase == "began" then if gameStarted == false then addRedTimer = timer.performWithDelay ( 1800, addRed, -1) moveRedTimer = timer.performWithDelay ( 20, moveRed, -1) gameStarted = true end end end function moveRed () for a = elements.numChildren, 1, -1 do if ( elements[a].x \> -100 ) then elements[a].x = elements[a].x - 7 else elements:remove (elements[a]) elements[a] = nil end end end function addRed () Red = display.newRect( 90, 80, 20, 20 ) Red.x = 320 Red.y = 180 Red:setFillColor( 1, 0, 0 ) Red.name = "red" physics.addBody( Red, "static", { density=3.0, friction=0.5, bounce=0 } ) elements:insert (Red) end function PlayerJump ( event ) if event.phase == "began" then player:applyForce ( 0, -251, player.x, player.y ) end end Runtime:addEventListener ("touch", PlayerJump) Runtime:addEventListener ("touch", speedUp) player.collision = onCollision player:addEventListener ("collision")