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

Controlling Collisions

Controlling Collisions

Aug 29 2019, 9:18 AM PST 5 min

Controlling collisions, sometimes referred to as collision filtering, defines which physical objects can collide with others.

Collision Groups

Collision groups let you place objects in dedicated groups and control whether they can collide or not collide with objects in other groups. Objects within non-colliding groups will simply pass through each other as shown with these green balls.

Studio Configuration

To set up collision groups in Studio:

  1. Click the Collision Groups button () within the Advanced section of the Model tab.

  2. In the Collision Groups Editor, click the small Add Group field in the lower area of the window. Type in a new group name like Obstacles, then press Enter/Return. Notice that the new group is added to both the left column and top row.

  1. Repeat this process and add a third group named GreenObjects.

  2. Select one or more objects in your place that would qualify as obstacles, then add them to the Obstacles collision group by clicking the button for its row.

  1. Similarly, select one or more objects in your place that would qualify as green objects, then add them to the GreenObjects group by clicking its button.

  2. At this point, all objects will collide with all other objects (default behavior). To prevent objects in the GreenObjects group from colliding with objects in the Obstacles group, uncheck the box in the respective row/column.

  1. If desired, create additional collision groups, add objects to them, and check/uncheck the appropriate boxes to control collision filtering throughout the place.

Script Configuration

Collision groups can also be configured through scripting as follows:

  1. Create two empty collision groups via PhysicsService/CreateCollisionGroup|PhysicsService:CreateCollisionGroup().
local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacles"
local greenObjects = "GreenObjects"

-- Create two collision groups
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)
  1. Once a group is created, objects can be added to it with PhysicsService/SetPartCollisionGroup|PhysicsService:SetPartCollisionGroup(). This function requires the object to add and the string name of the collision group.
local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacles"
local greenObjects = "GreenObjects"

-- Create two collision groups
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)

-- Add an object to each group
PhysicsService:SetPartCollisionGroup(workspace.Obstacle1, obstacles)
PhysicsService:SetPartCollisionGroup(workspace.GreenBall, greenObjects)
  1. To control whether objects in the GreenObjects group collide with objects in the Obstacles group, call PhysicsService/CollisionGroupSetCollidable|PhysicsService:CollisionGroupSetCollidable(), providing the two collision groups and a boolean true or false.
local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacles"
local greenObjects = "GreenObjects"

-- Create two collision groups
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)

-- Add an object to each group
PhysicsService:SetPartCollisionGroup(workspace.Obstacle1, obstacles)
PhysicsService:SetPartCollisionGroup(workspace.GreenBall, greenObjects)

PhysicsService:CollisionGroupSetCollidable(greenObjects, obstacles, false)
Collision Group Notes/Limitations
  • Objects can only belong to one collision group at a time. If you put them in a new group, they will be removed from their current group.
  • Only objects with BasePart/CanCollide|CanCollide enabled are affected by this system. If BasePart/CanCollide|CanCollide is disabled, the object will not collide under any circumstances.
  • The "Default" group cannot be removed.
  • Each Roblox place supports up to 32 collision groups.

Part-to-Part Filtering

If you need to prevent specific part-to-part collisions, like stopping the wheel of a car from scraping against the car’s body, the NoCollisionConstraint object is more convenient than setting up collision groups. Advantages include:

  • The NoCollisionConstraint lets you create and share models with customized collision filtering. While you could still achieve filtering through collision groups, it would require adding a configuration script which executes when the game runs.

  • Connected parts will not collide with each other, but they can still collide with other objects.

Studio Creation

The easiest way to add a NoCollisionConstraint is through the Create menu in the Constraints section of the Model tab. Similar to other constraints, this tool will act differently based on how many parts are selected when the tool is activated.
  • If no parts are selected when the tool is activated, the next two parts that are clicked will be connected.
  • If one part is selected when the tool is activated, the next part that is clicked will be connected to the selected part.
  • If two parts are selected when the tool is activated, those two parts will be connected.

Script Creation

A NoCollisionConstraint can also be added through scripting by creating a new instance and assigning its NoCollisionConstraint/Part0|Part0 and NoCollisionConstraint/Part1|Part1 properties to the appropriate two parts.

local noCollisionConstraint = Instance.new("NoCollisionConstraint")
noCollisionConstraint.Parent = workspace
noCollisionConstraint.Part0 = workspace.CarModel.FrontLeftWheel
noCollisionConstraint.Part1 = workspace.CarModel.Chassis
Tags:
  • collision
  • filtering
  • physics