About collision filtering

I’m working on some collision filter that avoid some collision detections

here is my code

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

physics.start()

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



local floorCollisionFilter = { categoryBits=1, maskBits=6 }
 

local floor = display.newRect( cx, 300, 320, 20 )

physics.addBody( floor, “static”, { bounce=0, filter=floorCollisionFilter } )

local redCollisionFilter = { categoryBits=2, maskBits=3 }
 

local redOtherCFilter = { categoryBits = 1, maskBits=3 }

local blueCollisionFilter = { categoryBits=4, maskBits=5 }

local blueOtherCFilter = { categoryBits = 4, maskBits = 5 }

     


local redSquare = display.newRect( cx, 20, 40, 40 )
redSquare:setFillColor( 1, 0, 0 )
physics.addBody( redSquare, “dynamic”, { friction=0, bounce = 0, filter=redCollisionFilter, otherFilter = redOtherCFilter} )

local blueSquare = display.newRect( cx, 80, 40, 40 )
blueSquare:setFillColor( 0, 0, 1 )

physics.addBody( blueSquare, “dynamic”, { friction=0, bounce = 0, filter=blueCollisionFilter, otherFilter = blueOtherCFilter} )



function playerJump (event)
     if event.phase == “began” then

          blueSquare:applyForce ( 0, -5.5, blueSquare.x, blueSquare.y )

     end

end

Runtime:addEventListener (“touch”, playerJump)



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

gameStarted = false

function gameStart (event)
     if event.phase == “began” then
          if gameStarted == false then
          addObsTimer = timer.performWithDelay ( 1800, addb, -1)
          moveMouseTimer = timer.performWithDelay ( 20, moveb, -1)
          gameStarted = true
          end
     end
end
 

function moveb ()
     for a = elements.numChildren, 1, -1 do
          if ( elements[a].x > -100 ) then
               elements[a].x = elements[a].x - 6
          else
               elements:remove (elements[a])
               elements[a] = nil
          end
     end
end

function addb ()
     

     box = display.newRect (300, cy+30, 40, 40)

    boxCfilter = { categoryBits = 1, maskBits = 4}

     physics.addBody (box,“static”, {radius=7 , density = 0.01, bounce = 0, otherFilter=boxCfilter })

    elements:insert (box)

end

Runtime:addEventListener (“touch”, gameStart)[/lua]

What i have here is 

  1. redSquare not collide with blueSquare but they won’t penetrate the ground

In addition, I have coded the addb () to create boxes and move them to hit both redSquare and blueSquare

But what i want is to only hit blueSquare, and penetrate or not affecting the RedSquare.

My question is, Can I have 2 different filters for both redSquare and blueSquare to

  1. avoid detecting redSquare not collide with blueSquare ( which I have it in the code )

  2. and the boxes only hit blueSquare and avoiding hitting redSquare

I tried to code 

[lua]local redOtherCFilter = { categoryBits = 1, maskBits=3 }

local blueOtherCFilter = { categoryBits = 4, maskBits = 5 }

physics.addBody( redSquare, “dynamic”, { friction=0, bounce = 0, filter=redCollisionFilter, otherFilter= redOtherCFilter} )

physics.addBody( blueSquare, “dynamic”, { friction=0, bounce = 0, filter=blueCollisionFilter, otherFilter =  blueOtherCFilter} )

boxCfilter = { categoryBits = 1, maskBits = 4}

physics.addBody (box, “static”, {radius = 7, density = 0.01, bounce = 0, otherFilter = boxCfilter })

[/lua]

to make another filter, but it won’t work

Hi @xming0529,

You can only have one table of collision filters defined for an object (thus, only “filter={}”, not an additional "otherFilter={}).

I assume you followed through the collision filter guide here?

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#filtering

You can specify both the categoryBits/maskBits and the groupIndex, of which the groupIndex will take precedence. The guide describes this as well.

Brent

YEA, I have already checked, but i don’t actually understand the table. and Ill try more about it. Thanks

Hi @xming0529,

You can only have one table of collision filters defined for an object (thus, only “filter={}”, not an additional "otherFilter={}).

I assume you followed through the collision filter guide here?

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#filtering

You can specify both the categoryBits/maskBits and the groupIndex, of which the groupIndex will take precedence. The guide describes this as well.

Brent

YEA, I have already checked, but i don’t actually understand the table. and Ill try more about it. Thanks