Skip to main content

Collision Filtering

Collision filtering is a very important aspect of any physics engine. Nature2D has everything you need to filter collisions between RigidBodies. Here's a video that illustrates the same. You can see that only the RigidBodies in red color are able to pass through the entry, while blue ones are not!

In order to choose which RigidBodies collide with whom. We use methods such as RigidBody:FilterCollisionsWith(body: RigidBody).

local Red = Engine:Create("RigidBody", {
Object = RedObject,
Collidable = true,
Anchored = false
})

local Blue1 = Engine:Create("RigidBody", {
Object = BlueObject,
Collidable = true,
Anchored = false
})

local Blue2 = Blue1:Clone()

Red:FilterCollisionsWith(Blue1)
Red:FilterCollisionsWith(Blue2)

In the above code snippet, we declare that Red shall not collide with any blue RigidBodies, while the blue RigidBodies can still collide with each other!

In order to make Red collide with Blue1 and Blue2 again, we use RigidBody:UnfilterCollisionsWith().

Red:UnfilterCollisionsWith(Blue1)
Red:UnfilterCollisionsWith(Blue2)

If you ever lose track of which RigidBodies a body is not asked to collide with, use RigidBody:GetFilteredRigidBodies()! This method returns an array of all RigidBodies the body is not allowed to collide with.