2
votes

In Corona SDK, I have a curved LineObject with which I need other objects to be able to collide. To achieve this, I am creating a large number (hundreds) of small circular physics bodies along this curved line (like a string of pearls).

The physical interaction (collisions) between the line (the "pearls") and other objects needs to change at runtime. Since it is not possible to modify collision filters at runtime, is there a way to do this without displaying all the circles again from scratch, which would affect performance?

1
This question isn't really answerable as it. What do you when you say they share physical properties? What's a collisionFilter? Are you using some library with Lua bindings? Also StackOverflow is better suited for questions that have specific answers - it's less suited for general brainstorming. - Alex
i just mean the filter. i'm using Corona SDK - Sincizio
When you say "group of objects", do you mean you are using the groupIndex parameter in the collision filter? - GoojajiGreg
I mean creating an object in wich i ":insert" all the others - Sincizio
I see. No, each physics body needs to have the collision filter parameter set when you declare it. - GoojajiGreg

1 Answers

1
votes

No Changes To Collision Filter

As you say, you cannot change the collision filter parameter on a physics body once it has been created. To clarify what this means, consider this example of the use of the filter from the Corona SDK documentation on Collisions (which also explains very well the use of categoryBits and maskBits):

local floorCollisionFilter = { categoryBits=1, maskBits=6 }
local floor = display.newRect( 0, 0, 320, 80 )
physics.addBody( floor, "static", { filter=floorCollisionFilter } )

After the physics body is instantiated, changes to floorCollisionFilter will have no effect on the collision properties of floor.

Precollision Detection

You can, however, change the collision behaviour of objects using preCollision detection. If you have a preCollision listener on your objects, you can determine if they should be involved in the collision event that is being triggered and set the event.contact.isEnabled = false if they should not. There are gotchas: this can be costly if you have many physics bodies and the precollision calls can be noisy (many possible calls per collision event).

Multi-Element Body and isBodyActive Property

Maybe you should reconsider how you are adding physics to the curved line. You say you are placing many circles along a path (like a string of beads/pearls) to detect collisions between that path and other bodies. If the shape of the path does not need to change, you should consider using an edge shape (chain) body. There do not seem to be limitations on the number of vertices used in creating it or convexity requirements (unlike polygonal bodies).

This would allow you to remove the edge shape body (assume it is called object) from the simulation (other bodies will ignore/pass through it) by setting object.isBodyActive = false. This won't remove the object from the display and you can always set isBodyActive = true later. See the documentation for gotchas about changing physics body properties (i.e. you need to use small delays).

One last note about the use of the edge shape body. Even if the shape of the path did need to change in a controlled way, you might get better performance destroying the existing edge body and creating a new one along the new path rather than moving around a huge number of little circular bodies.