Multi-element objects and collision filters

Hey guys, I just fixed a major performance bug by implementing collision filters. Fist pump! But now my multi-element physics bodies aren’t working as before: collisions are only firing for one of the two elements in the body. I.e. my projectile bodies collide with the shield (element 1) but pass right through the rest of the body without registering a hit. Do I need to define each element of the body as a separate category within the collision filter or something?

I’ve used PhysicsEditor to create the shape outlines, but defined the filters in code. The “tracers” file is what’s output by PhysicsEditor. The “swordsman” enemy has two elements, a shield and a body.

local creepCollisionFilter = { categoryBits = 2, maskBits = 5 } local scaleFactor = 0.25 local physicsData = (require("tracers")).physicsData(scaleFactor) local physicsParams = physicsData:get("swordsman") physicsParams.filter = creepCollisionFilter physics.addBody( creep, "kinematic", physicsParams)

Here’s the relevant snippet from PhysicsEditor. Note that I did not set the filter properties myself – I only want to use PhysicsEditor to trace the sprites, but it added the filters anyway. As far as I can tell I’m overwriting them, and all the single-element bodies in the game collide just fine. 

["swordsman"] = {                     {                     pe\_fixture\_id = "shield", density = 2, friction = 0, bounce = 0,                      filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },                     shape = {   49, 22  ,  49, 39  ,  36, 79  ,  24, 74  ,  9, 58  ,  1, 22  ,  14, 2  }                     },                     {                     pe\_fixture\_id = "body", density = 2, friction = 0, bounce = 0,                      filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },                     shape = {   57, 19  ,  1, -4  ,  47, -31  ,  65, -9  }                     },                     {                     pe\_fixture\_id = "body", density = 2, friction = 0, bounce = 0,                      filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },                     shape = {   1, -4  ,  -14, -35  ,  -13, -55  ,  4, -69  ,  30, -67  ,  47, -31  }                     } }

I’ve also tried setting the filter manually in PE or in the output above, but the result is exactly the same. Thanks!

a multi-element body is defined by a “table of tables” for each fixture within.  i think  Physics Editor used to (because i don’t actually use it) return that table of tables, and you’d know because your addBody call would need an unpack() around the physics data.  you don’t seem to need that unpack() any more, so it would seem that PE’s get() in recent/current versions must be doing it for you.

that sort of multi return value(s) is not suitable for this kind of assignment:

local physicsParams = physicsData:get(“swordsman”)

because you’re “throwing away” all but the first return value.

it’s really only suitable for use directly within addBody().

there are (of course) ways around that, but I’d instead suggest either using Physics Editor the way it was intended (define the filters within the prog then export ready-to-go) or edit its output manually to set your filters.  Then, either way, put your physicsData:get() directly in addBody() to retain all returned values.

Nailed it, thanks Dave. 

a multi-element body is defined by a “table of tables” for each fixture within.  i think  Physics Editor used to (because i don’t actually use it) return that table of tables, and you’d know because your addBody call would need an unpack() around the physics data.  you don’t seem to need that unpack() any more, so it would seem that PE’s get() in recent/current versions must be doing it for you.

that sort of multi return value(s) is not suitable for this kind of assignment:

local physicsParams = physicsData:get(“swordsman”)

because you’re “throwing away” all but the first return value.

it’s really only suitable for use directly within addBody().

there are (of course) ways around that, but I’d instead suggest either using Physics Editor the way it was intended (define the filters within the prog then export ready-to-go) or edit its output manually to set your filters.  Then, either way, put your physicsData:get() directly in addBody() to retain all returned values.

Nailed it, thanks Dave.