Collision Filter Detection

I am having trouble getting 2  different spawn objects not to collide with each other. I am using the Collision guide from the website as a reference, but everything  I have tried doesn’t work.It should be noted both spawn objects are coming from the same module.Thanks in advance!

Here my code

[lua]

local function addspawn ( )
      local spawnCollisionFilter = { categoryBits =4, maskBits =5 }
      local spawn = Spawn.new(10, 10, physics)
      
        spawn.objectType = “food”
        spawn.isFixedRotation = true
        spawn.speedFactor = 1
        spawn.isBullet = true
        spawn:applyForce( 1, 0, spawn.x, spawn.y )
        physics.addBody( spawn, “dynamic”, {density=0.1,filter=spawnCollisionFilter} )
        
       
        world:insert(spawn)

    print( “Working REpeat” )
   end
timerId = timer.performWithDelay( 1000, addspawn ,-1 )

local function addspawn2 ( )

    local GreenCollisionFilter = { categoryBits =2, maskBits =3 }
    local spawn2 = Spawn.new(10, 10, physics)

        spawn2.objectType = “food3”
        spawn2:setFillColor( 0, 255, 0 )
        spawn2.isFixedRotation = true
        spawn2.speedFactor = 1
        spawn2.isBullet = true
        spawn2:applyForce( 0, 1, spawn2.x, spawn2.y )
        spawn2.y = .3
        physics.addBody( spawn2, “dynamic”, {density=0.1,filter=GreenCollisionFilter} )
        world:insert(spawn2)

    print( “Working REpeat2” )
   end
timerId = timer.performWithDelay( 1000, addspawn2 ,-1 )

[/lua]

Hi @inveince,

A few comments on your code:

  1. I don’t understand this line, or what happens in this “Spawn” module:

[lua]

local spawn = Spawn.new(10, 10, physics)

[/lua]

Also, I don’t know what the argument “physics” is (third argument). Typically, users use the variable “physics” as the variable to “require()” the physics library, so if you’re doing that, you probably shouldn’t pass that to the module.

  1. Assuming that module is properly creating the object, you then have some sequence issues. Specifically, you attempt to set several physical properties like “.isBullet” and “.isFixedRotation” on the object before you make it physical. This will basically do nothing from a physics standpoint, because until the object is physical (via .addBody()), Corona doesn’t regard any of the physics library properties upon it anything related to physics. So, you should always make the object physical first , then apply various properties to it.

Hope this helps,

Brent

Hi thank you for this advice, but here is the spawn module I have made. I am very new to this thank you.

local physics local moving = 0 local \_W, \_H = display.contentWidth, display.contentHeight         local function new (x, y, worldPhysics)     physics = worldPhysics       local spawn = display.newRect( x, y, 30, 30 )      spawn.x = math.random(\_W)      spawn.y = math.random(\_H)          spawn:setFillColor( 0, 0, 0 )          spawn.objectType = "food"                                    physics.addBody( spawn, "dynamic")   return spawn   end   return { new = new }  

Hi @inveince,

Generally, I prefer to structure modules along these lines:

[lua]

–spawner.lua

local M = {}

function M.spawn( x, y, objType )

    local _W, _H = display.contentWidth, display.contentHeight

    local spawn = display.newRect( x, y, 30, 30 )

    spawn.x = math.random(_W)

    spawn.y = math.random(_H)

    spawn:setFillColor( 0, 0, 0 )

    spawn.objectType = objType

    return spawn

end

return M

[/lua]

Then later, you could call this function from “main.lua” like this:

[lua]

–main.lua

local physics = require( “physics” )

local spawner = require( “spawner” )

local newSpawn = spawner.spawn( 100, 100, “food” )

physics.addBody( newSpawn, “dynamic”, { density=0.1, filter=spawnCollisionFilter } )

[/lua]

Of course you will need to complete and test and modify this code for your specific project, but this is a general overview. Hope it helps.

Brent

Ok, thanks a Iot will test this out and report back!

Hi, so I tried the code you suggested but the two opposing blocks still collide with each other any advice. I have tried maskbit, category bits, and group index 

-----Spawn Module--------

local M = {}      function M.spawn( x, y, objType ) local \_W, \_H = display.contentWidth, display.contentHeight local spawn = display.newRect( x, y, 30, 30 )      physics.addBody( spawn, "dynamic")      spawn.x = math.random(\_W)      spawn.y = math.random(\_H)          spawn:setFillColor( 0, 0, 0 )          spawn.objectType = objType                   return spawn   end   return M
  local function addspawn ( )  local spawnCollisionFilter = { groupIndex = -2 }           local spawn = Spawn.spawn( 10, 10, "food" )   physics.addBody( spawn, "dynamic", { density=0.1, filter=spawnCollisionFilter } )  spawn:applyForce( 1, 0, spawn.x, spawn.y ) spawn.objectType = "food"  spawn.isBullet = true    world:insert(spawn)                 print( "Working REpeat" )    end timerId = timer.performWithDelay( 1000, addspawn ,-1 ) ----------------------------------------------------------------------------------------------- local function addspawn2 ( ) local GreenCollisionFilter = { groupIndex = 2 }           local spawn2 = Spawn.spawn(10, 10, "food3")            physics.addBody( spawn2, "dynamic", {density=0.1,filter=GreenCollisionFilter} )         spawn2.objectType = "food3"         spawn2:setFillColor( 0, 255, 0 )         spawn2.isFixedRotation = true         spawn2.speedFactor = 1         spawn2.isBullet = true         spawn2:applyForce( 0, 1, spawn2.x, spawn2.y )         spawn2.y = .3                           world:insert(spawn2)           print( "Working REpeat2" )    end timerId = timer.performWithDelay( 1000, addspawn2 ,-1 )    

Hi @inveince,

It looks like you’re adding a physics body to the object twice. I suggest that you only add it to the object after it’s returned from the module (not in the module).

As for collisions, did you follow through the worksheet that we document, making sure that you followed every step carefully? This method has been tested/proven by many developers in countless projects, but if you skip one step, then it won’t work.

The worksheet method is outlined here, toward the bottom of the page:

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

Take care,

Brent

Hey, I finally got a chance to get back to this project. I figured out what I was doing wrong, I had the filters out of scope - Thank you for your insight in refining my code :D  . 

Hi @inveince,

A few comments on your code:

  1. I don’t understand this line, or what happens in this “Spawn” module:

[lua]

local spawn = Spawn.new(10, 10, physics)

[/lua]

Also, I don’t know what the argument “physics” is (third argument). Typically, users use the variable “physics” as the variable to “require()” the physics library, so if you’re doing that, you probably shouldn’t pass that to the module.

  1. Assuming that module is properly creating the object, you then have some sequence issues. Specifically, you attempt to set several physical properties like “.isBullet” and “.isFixedRotation” on the object before you make it physical. This will basically do nothing from a physics standpoint, because until the object is physical (via .addBody()), Corona doesn’t regard any of the physics library properties upon it anything related to physics. So, you should always make the object physical first , then apply various properties to it.

Hope this helps,

Brent

Hi thank you for this advice, but here is the spawn module I have made. I am very new to this thank you.

local physics local moving = 0 local \_W, \_H = display.contentWidth, display.contentHeight         local function new (x, y, worldPhysics)     physics = worldPhysics       local spawn = display.newRect( x, y, 30, 30 )      spawn.x = math.random(\_W)      spawn.y = math.random(\_H)          spawn:setFillColor( 0, 0, 0 )          spawn.objectType = "food"                                    physics.addBody( spawn, "dynamic")   return spawn   end   return { new = new }  

Hi @inveince,

Generally, I prefer to structure modules along these lines:

[lua]

–spawner.lua

local M = {}

function M.spawn( x, y, objType )

    local _W, _H = display.contentWidth, display.contentHeight

    local spawn = display.newRect( x, y, 30, 30 )

    spawn.x = math.random(_W)

    spawn.y = math.random(_H)

    spawn:setFillColor( 0, 0, 0 )

    spawn.objectType = objType

    return spawn

end

return M

[/lua]

Then later, you could call this function from “main.lua” like this:

[lua]

–main.lua

local physics = require( “physics” )

local spawner = require( “spawner” )

local newSpawn = spawner.spawn( 100, 100, “food” )

physics.addBody( newSpawn, “dynamic”, { density=0.1, filter=spawnCollisionFilter } )

[/lua]

Of course you will need to complete and test and modify this code for your specific project, but this is a general overview. Hope it helps.

Brent

Ok, thanks a Iot will test this out and report back!

Hi, so I tried the code you suggested but the two opposing blocks still collide with each other any advice. I have tried maskbit, category bits, and group index 

-----Spawn Module--------

local M = {}      function M.spawn( x, y, objType ) local \_W, \_H = display.contentWidth, display.contentHeight local spawn = display.newRect( x, y, 30, 30 )      physics.addBody( spawn, "dynamic")      spawn.x = math.random(\_W)      spawn.y = math.random(\_H)          spawn:setFillColor( 0, 0, 0 )          spawn.objectType = objType                   return spawn   end   return M
  local function addspawn ( )  local spawnCollisionFilter = { groupIndex = -2 }           local spawn = Spawn.spawn( 10, 10, "food" )   physics.addBody( spawn, "dynamic", { density=0.1, filter=spawnCollisionFilter } )  spawn:applyForce( 1, 0, spawn.x, spawn.y ) spawn.objectType = "food"  spawn.isBullet = true    world:insert(spawn)                 print( "Working REpeat" )    end timerId = timer.performWithDelay( 1000, addspawn ,-1 ) ----------------------------------------------------------------------------------------------- local function addspawn2 ( ) local GreenCollisionFilter = { groupIndex = 2 }           local spawn2 = Spawn.spawn(10, 10, "food3")            physics.addBody( spawn2, "dynamic", {density=0.1,filter=GreenCollisionFilter} )         spawn2.objectType = "food3"         spawn2:setFillColor( 0, 255, 0 )         spawn2.isFixedRotation = true         spawn2.speedFactor = 1         spawn2.isBullet = true         spawn2:applyForce( 0, 1, spawn2.x, spawn2.y )         spawn2.y = .3                           world:insert(spawn2)           print( "Working REpeat2" )    end timerId = timer.performWithDelay( 1000, addspawn2 ,-1 )    

Hi @inveince,

It looks like you’re adding a physics body to the object twice. I suggest that you only add it to the object after it’s returned from the module (not in the module).

As for collisions, did you follow through the worksheet that we document, making sure that you followed every step carefully? This method has been tested/proven by many developers in countless projects, but if you skip one step, then it won’t work.

The worksheet method is outlined here, toward the bottom of the page:

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

Take care,

Brent

Hey, I finally got a chance to get back to this project. I figured out what I was doing wrong, I had the filters out of scope - Thank you for your insight in refining my code :D  .