PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Collision Filtering: Intro

Collision Filtering: Intro

Jul 03 2018, 10:03 AM PST 5 min

Collision filtering is a component of the Roblox physics engine that allows for customization of which parts collide with each other. It allows a developer to define collision groups, which are simply collections of parts. These groups can be set to collide or not collide with other groups. When two parts collide, the engine will check the groups of the parts involved. If the groups allow collisions, then the parts will bounce off each other, otherwise they will pass through each other.

No Collision Groups

NoCollisionGroup.gif

With Collision Groups

CollisionGroup.gif

Setting up Collision Groups

Before collision groups can be used, they must be created. Creating a collision group is done with the PhysicsService/CreateCollisionGroup function of the PhysicsService. This function takes the name of the group as a parameter. It is recommended to store this name in a variable, as this name is used to reference the group in other functions.

local PhysicsService = game:GetService("PhysicsService")

local wallGroup = "WallGroup"
local ballGroup = "BallGroup"

PhysicsService:CreateCollisionGroup(wallGroup)
PhysicsService:CreateCollisionGroup(ballGroup)

Once a group has been created, parts can be added to it with PhysicsService/SetPartCollisionGroup. This function takes two arguments: the part to add and the name of the collision group to add it to. Note that a part can only be in one group at a time.

local PhysicsService = game:GetService("PhysicsService")

local wallGroup = "WallGroup"
local ballGroup = "BallGroup"

PhysicsService:CreateCollisionGroup(wallGroup)
PhysicsService:CreateCollisionGroup(ballGroup)

local wall = game.Workspace.Wall
local ball = game.Workspace.Ball

PhysicsService:SetPartCollisionGroup(wall, wallGroup)
PhysicsService:SetPartCollisionGroup(ball, ballGroup)

Setting collision behavior between collision groups is done with the PhysicsService/CollisionGroupSetCollidable function. This function takes the name of the two groups to configure, then a boolean indicating whether or not those groups should collide with each other.

local PhysicsService = game:GetService("PhysicsService")

local wallGroup = "WallGroup"
local ballGroup = "BallGroup"

PhysicsService:CreateCollisionGroup(wallGroup)
PhysicsService:CreateCollisionGroup(ballGroup)

local wall = game.Workspace.Wall
local ball = game.Workspace.Ball

PhysicsService:SetPartCollisionGroup(wall, wallGroup)
PhysicsService:SetPartCollisionGroup(ball, ballGroup)

PhysicsService:CollisionGroupSetCollidable(wallGroup, ballGroup, false)

Default Group

All parts in a Roblox game are part of the “Default” collision group initially. This group is configured to collide with itself and any new groups that are added. CollisionGroupSetCollidable can change the behavior of the Default group however as needed.

Limits

There are several limitations on the collision filtering feature that should be known while developing:

  • Each Roblox place can support up to 32 collision groups (including the Default group).

  • The “Default” group cannot be removed.

  • Only parts with BasePart/CanCollide enabled will be affected by this system.

  • Collision groups can only be created and configured on the server.

  • Parts can only be part of one collision group at a time.

Tags:
  • collision
  • filtering
  • customization