# PhysicsService

PhysicsService : `Instance`

## Overview

As a service that finely controls collision rules between objects in physics computation, PhysicsService allows the creation of collision groups and setting whether groups interact with each other

For example, it can be used to allow players to pass through specific walls upon reaching a certain level.

## Properties

## Methods

### CollisionGroupsAreCollidable

Provides a function to check whether two collision groups can collide.

#### Parameters

| `string` Group1 | The name of collision group 1. |
| --------------- | ------------------------------ |
| `string` Group2 | The name of collision group 2. |

#### Return

| `bool` | Specifies whether the groups can collide. |
| ------ | ----------------------------------------- |

#### Code Samples

### CollisionGroupSetCollidable

Determines whether two specified collision groups can collide with each other.

If you attempt to set interactions for unregistered groups, it will cause an error. Therefore, it is recommended to first check each group's existence using the IsCollisionGroupRegistered() method.

#### Parameters

| `string` Group1    | The name of collision group 1.            |
| ------------------ | ----------------------------------------- |
| `string` Group2    | The name of collision group 2.            |
| `bool` bCollidable | Specifies whether the groups can collide. |

#### Return

| `void` |   |
| ------ | - |

#### Code Samples

```lua
local Part = script.Parent
local PhysicsService = game:GetService("PhysicsService")

local DefaultGroup = "Default"

local GroupA = "GroupA"
PhysicsService:RegisterCollisionGroup(GroupA)

Part.CollisionGroup = GroupA

local collidable = false
PhysicsService:CollisionGroupSetCollidable(DefaultGroup, GroupA, collidable)
```

### GetMaxCollisionGroups

Returns the maximum number of collision groups that can be created simultaneously in the physics engine.

Currently, the limit is 17, and exceeding this prevents the creation of additional groups, so efficient group management is important when designing complex collision rules

#### Parameters

#### Return

| `Value` | The maximum number of collision groups that can be created. |
| ------- | ----------------------------------------------------------- |

#### Code Samples

```lua
local PhysicsService = game:GetService("PhysicsService")

print(PhysicsService:GetMaxCollisionGroups())
```

### GetRegisteredCollisionGroups

Returns a table of all registered collision groups.

#### Parameters

#### Return

| `Array` | The list of registered collision groups. |
| ------- | ---------------------------------------- |

#### Code Samples

```lua
local CollisionGroups = PhysicsService:GetRegisteredCollisionGroups()
print(CollisionGroups)
```

### IsCollisionGroupRegistered

Checks whether a specific collision group is registered.

Before calling methods related to collision group settings, such as CollisionGroupSetCollidable(), you can prevent errors by checking if the group is registered.

#### Parameters

| `string` Group | The name of the collision group to check. |
| -------------- | ----------------------------------------- |

#### Return

| `bool` | Specifies whether it is registered. |
| ------ | ----------------------------------- |

#### Code Samples

```lua
local PhysicsService = game:GetService("PhysicsService")
local DefaultGroup = "Default"

local IsRegistered = PhysicsService:IsCollisionGroupRegistered(DefaultGroup)
print(IsRegistered)
```

### RegisterCollisionGroup

Creates a collision group with the specified name.

Names already registered, such as Default, RootPart, BodyPart, and Projectile, cannot be used.

As collision group settings can impact performance, it's recommended to set them in Studio's editing phase rather than creating them at runtime.

#### Parameters

| `string` Group | The name of the collision group to register. |
| -------------- | -------------------------------------------- |

#### Return

| `void` |   |
| ------ | - |

#### Code Samples

```lua
local Part = script.Parent
local PhysicsService = game:GetService("PhysicsService")

local GroupA = "GroupA"
PhysicsService:RegisterCollisionGroup(GroupA)
```

### RenameCollisionGroup

This function renames a specified collision group while maintaining its settings for collision with other groups.

Names of default collision groups, such as Default, RootPart, BodyPart, and Projectile, cannot be changed.

As renaming can impact performance, it's recommended to set them in Studio's editing phase rather than modifying at runtime.

#### Parameters

| `string` FromGroup | The name of the collision group to rename. |
| ------------------ | ------------------------------------------ |
| `string` ToGroup   | The name to change.                        |

#### Return

| `void` |   |
| ------ | - |

#### Code Samples

### UnregisterCollisionGroup

This function deletes a specified collision group, and automatically assigns parts belonging to that group to the Default group.

Default collision groups, such as Default, RootPart, BodyPart, and Projectile, cannot be deleted.

As deleting collision groups can impact performance, it's recommended to set them in Studio's editing phase rather than deleting at runtime.

#### Parameters

| `string` Group | The name of the collision group to delete. |
| -------------- | ------------------------------------------ |

#### Return

| `void` |   |
| ------ | - |

#### Code Samples

```lua
local PhysicsService = game:GetService("PhysicsService")

local GroupA = "GroupA"
PhysicsService:UnregisterCollisionGroup(GroupA)
```

## Events

## See also

{% content-ref url="../../../manual/studio-manual/game-development/collision-groups" %}
[collision-groups](https://docs.overdare.com/manual/studio-manual/game-development/collision-groups)
{% endcontent-ref %}
