Rigidbody: Collisions - Gr8-Tools/game-developer-roadmap-doc GitHub Wiki
Rigidbody in Unity: Collisions
Total time: 3 hours
1. Key definitions and formulas
Collider - A Collider is a component that defines the shape of an object for physics interactions such as collisions or triggers. There are different types of Colliders for 2D and 3D worlds:
- Box Collider - A box shape collider defined by a center position and extents (half-widths) along each axis.
- Sphere Collider - A sphere shape collider defined by a center position and radius.
- Capsule Collider - A cylinder shape collider with rounded ends defined by a center position, radius, and height. The radius defines the thickness of the capsule.
- Mesh Collider - A collider that takes the shape of a mesh attached to a MeshFilter component. The mesh vertices define the surface of the collider.
Physical layers - Physical layers allow grouping of GameObjects for collision detection purposes. By default there are 8 layers that can be configured in a matrix for collision filtering. This matrix defines which layers will interact with each other. If matrix element is 1, objects from corresponding layers will interact via collision/trigger. If 0, they will pass through each other.
Collision detection algorithm - The basic algorithm for collision detection between two objects involves checking if the distance between the closest points on the objects is less than the sum of their radii. If it is, a collision has occurred. Unity uses more advanced Separating Axis Theorem (SAT) to resolve collisions accurately.
Trigger vs Collide - A trigger detects contact without causing collision response while a collision causes physical impact. Triggers are useful for interactions like entering/exiting areas. Collisions make objects push each other realistically. Triggers can be detected by adding a script using OnTriggerEnter.
2D vs 3D colliders - 2D colliders like Box, Circle and Polygon colliders are for 2D physics and have fewer properties. 3D colliders support true 3D collisions but may have performance overhead for 2D games. 2D colliders ignore the z-axis.
Math behind collision detection - Collision detection algorithms use concepts from linear algebra and geometry. SAT works by projecting object surfaces onto axes to check for overlap of intervals. Minkowski sums and support mapping help calculate minimum translation vectors for resolving penetrations.
2. Algorithms
Collision detection in Unity uses a SAT (Separating Axis Theorem) based algorithm. The main steps are:
-
Find the separating axis (normal) that maximally separates the objects' bounding volumes. This could be the axes of the objects themselves or the cross product of edges.
-
Project the bounding volumes onto this axis. This yields intervals on the axis, one for each volume.
-
Check if the intervals overlap. If they do not overlap, the volumes do not intersect. If they do overlap, a collision is detected.
-
Repeat steps 1-3 for all possible separating axes to fully resolve the collision.
Here is a simple C# script to detect collision between two boxes:
void CheckCollision(BoxCollider a, BoxCollider b) {
Vector3 normal = Vector3.zero;
if(OverlapOnAxis(a,b,Vector3.right)) {
// Collision detected on x axis
}
if(OverlapOnAxis(a,b,Vector3.up)) {
// Collision detected on y axis
}
// Check z axis and cross products
}
bool OverlapOnAxis(BoxCollider a, BoxCollider b, Vector3 axis) {
float aMin, aMax, bMin, bMax;
a.GetProjection(axis, out aMin, out aMax);
b.GetProjection(axis, out bMin, out bMax);
return aMax >= bMin && aMin <= bMax;
}
3. Video resources
Time: 40 minutes
Time: 10 minutes
Time: 40 minutes
4. Information sources
5. Practice tasks
Cube-game
- Set up 4 new Physical layers:
- 'Player': for your cube-player
- 'Target': for the cube-target, that you are try to achieve
- 'Floor': for the plane, where you are walking on
- 'Wall' : for the bound of plane (described below)
- Set up collision matrix for the provided layers. If collision is not obvious - turn it of.
- Create bounds on the edge of your 'floor' (plane, where you are walking on).
- Setup tag 'GameFail' for the 'wall'-objects and 'GameWin' for the 'target'.
- Configure colliders and triggers: trigger for
Target
and colliders for others. - Triggering object with tag 'GameWin' should incrementing your score. Colliding with objects with tag 'GameFail' should end your game early.
Time: 1.5 hours