Colliders and Collision Detection Primer

GameDev Dustin
9 min readFeb 8, 2022

--

Colliders are a key component of any video game.

Without them, the game engine would never generate physics interactions between different game objects and terrain.

In Unity, there is also another component called a Rigidbody that must be used, especially for gravity effects to take place.

Generally speaking, a game developer should always try to use the least computationally expensive collider necessary.

Most of the time, primitive colliders such as the box collider, sphere collider, capsule collider, or a combination thereof to create a compound collider should be used.

The mesh collider should be used sparingly and only to achieve a particular functionality requirement.

Otherwise, due to its computational intensity, it can lag done the game.
The more gameobjects there are using the mesh collider, the more computing power that will be required.

This will vary by game type and the functionality that is desired.

Rigidbody

“A Rigidbody is the main component that enables physical behavior for a GameObject.

With a Rigidbody attached, the object will immediately respond to gravity.

If one or more Collider components are also added, the GameObject is moved by incoming collisions.

Since a Rigidbody component takes over the movement of the GameObject it is attached to, you shouldn’t try to move it from a script by changing the Transform properties such as position and rotation.

Instead, you should apply forces to push the GameObject and let the physics engine calculate the results.

There are some cases where you might want a GameObject to have a Rigidbody without having its motion controlled by the physics engine.

For example, you may want to control your character directly from script code but still allow it to be detected by triggers.

This kind of non-physical motion produced from a script is known as kinematic motion.

The Rigidbody component has a property called Is Kinematic which removes it from the control of the physics engine and allow it to be moved kinematically from a script.

It is possible to change the value of Is Kinematic from a script to allow physics to be switched on and off for an object, but this comes with a performance overhead and should be used sparingly.

Sleeping: When a Rigidbody is moving slower than a defined minimum linear or rotational speed, the physics engine assumes it has come to a halt.

When this happens, the GameObject does not move again until it receives a collision or force, and so it is set to “sleeping” mode.

This optimization means that no processor time is spent updating the Rigidbody until the next time it is “awoken” (that is, set in motion again).”

Colliders Overview

“Collider components define the shape of a GameObject for the purposes of physical collisions.

A collider, which is invisible, does not need to be the exact same shape as the GameObject’s mesh.

A rough approximation of the mesh is often more efficient and indistinguishable in gameplay.

The simplest (and least processor-intensive) colliders are primitive collider types. In 3D, these are the Box Collider , Sphere Collider and Capsule Collider.

In 2D, you can use the Box Collider 2D and Circle Collider 2D.

You can add any number of these to a single GameObject to create compound colliders.”

Static Colliders

“You can add colliders to a GameObject without a Rigidbody component to create floors, walls and other motionless elements of a Scene.

These are referred to as static colliders.”

Dynamic Colliders

“Colliders on a GameObject that has a Rigidbody are known as dynamic colliders.

Static colliders can interact with dynamic colliders but since they don’t have a Rigidbody, they don’t move in response to collisions. “

Physics Materials

“When colliders interact, their surfaces need to simulate the properties of the material they are supposed to represent.

For example, a sheet of ice will be slippery while a rubber ball will offer a lot of friction and be very bouncy.

Although the shape of colliders is not deformed during collisions, their friction and bounce can be configured using Physics Materials.”

Triggers

“The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function.

However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision.

A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through.

When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.”

Box Collider

“The Box Collider is a basic cuboid-shaped collision primitive.

Box Colliders are rectangular cuboids and are useful for items such as crates or chests.

However, you can use a thin box as a floor, wall or ramp.

The Box Collider is also a useful element in a Compound Collider.”

Sphere Collider

“The Sphere Collider is a basic sphere-shaped collision primitive.

The collider can be resized via the Radius property but cannot be scaled along the three axes independently (ie, you can’t flatten the sphere into an ellipse).

As well as the obvious use for spherical objects like tennis balls, etc, the sphere also works well for falling boulders and other objects that need to roll and tumble.”

Capsule Collider

“The Capsule Collider is made of two half-spheres joined together by a cylinder.

It is the same shape as the Capsule primitive.

You can adjust the Capsule Collider’s Radius and Height independently of each other.

It is used in the Character Controller and works well for poles, or can be combined with other Colliders for unusual shapes.”

Compound Colliders

As described on the Rigidbody class page of Unity Documentation:

“Compound Colliders are combinations of primitive Colliders, collectively acting as a single Rigidbody. They come in handy when you have a model that would be too complex or costly in terms of performance to simulate exactly, and want to simulate the collision of the shape in an optimal way using simple approximations.

To create a Compound Collider, create child objects of your colliding object, then add a Collider component to each child object.

This allows you to position, rotate, and scale each Collider easily and independently of one another.

You can build your compound collider out of a number of primitive colliders and/or convex mesh colliders.

Mesh Colliders can’t normally collide with each other.

If a Mesh Collider is marked as Convex, then it can collide with another Mesh Collider.

The typical solution is to use primitive Colliders for any objects that move, and Mesh Colliders for static background objects.”

Mesh Collider

“The Mesh Collider takes a Mesh Asset and builds its Collider based on that Mesh.

It is more accurate for collision detection than using primitives for complicated Meshes.

Mesh Colliders that are marked as Convex can collide with other Mesh Colliders.”

Continuous Collision Detection

As described on the Rigidbody class page of Unity Documentation:

“Continuous collision detection is a feature to prevent fast-moving colliders from passing each other. This may happen when using normal (Discrete) collision detection, when an object is one side of a collider in one frame, and already passed the collider in the next frame.

To solve this, you can enable continuous collision detection on the rigidbody of the fast-moving object. Set the collision detection mode to Continuous to prevent the rigidbody from passing through any static (ie, non-rigidbody) MeshColliders.

Set it to Continuous Dynamic to also prevent the rigidbody from passing through any other supported rigidbodies with collision detection mode set to Continuous or Continuous Dynamic.

Continuous collision detection is supported for Box-, Sphere- and CapsuleColliders.

Note that continuous collision detection is intended as a safety net to catch collisions in cases where objects would otherwise pass through each other, but will not deliver physically accurate collision results, so you might still consider decreasing the fixed Time step value in the TimeManager inspector to make the simulation more precise, if you run into problems with fast moving objects.”

Sweep-based Continuous Collision Detection (CCD) vs Speculative CCD

“CCD ensures that fast-moving bodies collide with objects instead of passing, or tunnelling, through those objects.

To use sweep-based CCD, select a RigidBody in the Inspector window and set Collision Detection to Continuous or Continuous Dynamic.

For speculative CCD, set Collision Detection to Continuous Speculative.

Sweep-based CCD

“Sweep-based CCD uses a Time Of Impact (TOI) algorithm to compute potential collisions for an object by sweeping its forward trajectory using its current velocity.

However, because this method relies on linear sweep, it ignores the body’s angular motion, which can cause tunnelling effects when objects are rotating at speed.”

Speculative CCD

“Speculative CCD works by increasing an object’s broad-phase axis-aligned minimum bounding box (AABB), based on the object’s linear and angular motion. The algorithm is speculative because it picks all potential contacts during the next physics step.

Speculative CCD is generally cheaper than the sweep-based method because contacts are only computed during the collision detection phase, not during the solving and integrating phase.

Also, because Speculative CCD expands the broad-phase AABB based on the object’s linear and angular motion, it finds the contacts that sweep-based CCD can miss.

However, speculative CCD can cause a ghost collision, where an object’s motion is affected by a speculative contact point when it shouldn’t be.

Speculative CCD can also cause tunneling because speculative contacts are only computed during the collision detection phase.

During contact solving, if an object gains too much energy from the solver, it may end up outside the initial inflated AABB after integration.

If there are collisions just outside the AABB, the object will tunnel right though.”

to make the simulation more precise, if you run into problems with fast moving objects.”

Sweep-based Continuous Collision Detection (CCD) vs Speculative CCD

https://docs.unity3d.com/Manual/ContinuousCollisionDetection.html

“CCD ensures that fast-moving bodies collide with objects instead of passing, or tunnelling, through those objects.

To use sweep-based CCD, select a RigidBody in the Inspector window and set Collision Detection

to Continuous or Continuous Dynamic.

For speculative CCD, set Collision Detection to Continuous Speculative.

--

--

No responses yet