Game Physics — Collision Detection and Rigid Bodies Explained
Game physics simulates real-world mechanics in virtual environments — objects fall, collide, bounce, and slide. Without physics, every game would need hand-coded movement for every object. Physics engines handle this automatically.
What You’ll Learn
You’ll master collision detection algorithms (AABB, sphere, mesh), rigid body dynamics, raycasting, joints, and physics materials. You’ll build a raycast shooting mechanic in Unity C#.
Why Game Physics Matters
Physics separates toy-like games from immersive ones. The satisfying thud of a grenade landing, the realistic car crash in a racing game, the precise hit detection in a fighting game — all rely on physics. At DodaTech, physics simulation techniques are used in Durga Antivirus Pro for heuristic behavior analysis of suspicious processes.
Game Physics Learning Path
flowchart LR
A[Unity/Unreal Guide] --> B[Game Physics]
B --> C{You Are Here}
C --> D[Multiplayer Networking]
C --> E[Game Design]
style C fill:#f90,color:#fff
Collision Detection
Collision detection answers: “Did these two objects touch?” There are several methods with different trade-offs:
Axis-Aligned Bounding Box (AABB)
The simplest method. Each object is enclosed in a rectangle (2D) or box (3D) aligned to the world axes. Two AABBs overlap when their projections overlap on all axes.
bool AABBOverlap(Vector2 aMin, Vector2 aMax, Vector2 bMin, Vector2 bMax)
{
return aMin.x < bMax.x && aMax.x > bMin.x &&
aMin.y < bMax.y && aMax.y > bMin.y;
}Bounding Sphere
Each object has a sphere with center and radius. Two spheres collide when the distance between centers is less than the sum of radii.
bool SphereOverlap(Vector3 centerA, float radiusA, Vector3 centerB, float radiusB)
{
float dist = Vector3.Distance(centerA, centerB);
return dist < radiusA + radiusB;
}Mesh Collision
The most accurate (and expensive). Checks triangle-level intersection. Used for complex terrain and detailed props. In Unity, use MeshCollider with “Convex” enabled for moving objects.
Rigid Bodies
A rigid body component gives an object physics properties:
| Property | Effect |
|---|---|
| Mass | How heavy the object is |
| Drag | Air resistance |
| Angular Drag | Rotation resistance |
| Use Gravity | Whether gravity pulls it down |
| Is Kinematic | Script-controlled (no physics forces) |
Raycasting
Raycasting fires an invisible line and reports what it hits. It’s the backbone of shooting mechanics, interaction prompts, and AI vision:
using UnityEngine;
public class RaycastShooter : MonoBehaviour
{
public float range = 100f;
public float damage = 25f;
public Camera playerCamera;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
Ray ray = playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
if (Physics.Raycast(ray, out hit, range))
{
Debug.Log("Hit: " + hit.transform.name + " at " + hit.point);
// Damage the target if it has a health component
Health targetHealth = hit.transform.GetComponent<Health>();
if (targetHealth != null)
{
targetHealth.TakeDamage(damage);
}
// Visual feedback
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}Expected behavior: Clicking fires a ray from the center of the screen. The ray travels up to 100 units. If it hits something with a Health component, damage is applied. An impact effect spawns at the hit point.
Joints
Joints connect rigid bodies with constraints:
| Joint Type | Behavior | Use Case |
|---|---|---|
| Fixed | Locks two bodies together | Welded objects |
| Hinge | Rotates around one axis | Doors, pendulums |
| Spring | Bounces between bodies | Suspension, trampolines |
| Character | Capsule-shaped movement | Player characters |
Physics Materials
Physics materials control surface properties:
| Property | Effect |
|---|---|
| Dynamic Friction | Friction when moving |
| Static Friction | Friction when stationary |
| Bounciness | How much energy is retained on bounce |
| Friction Combine | How friction combines (Average, Min, Max, Multiply) |
Common Mistakes Beginners Make
1. Using Mesh Colliders on Moving Objects
Mesh colliders are expensive and cause performance spikes. Use primitive colliders (box, sphere, capsule) for moving objects. Reserve mesh colliders for static geometry.
2. Ignoring Kinematic Bodies
A kinematic rigid body doesn’t respond to forces but can push other objects. Use it for moving platforms and doors that need to affect physics objects.
3. Setting Mass Too Low or Too High
Mass ratio matters more than absolute mass. A mass-1000 object hitting mass-1 creates extreme reactions. Keep masses within 0.1–100 range.
4. Raycasting from the Wrong Origin
Raycast from the camera center for FPS shooters. Raycasting from the weapon model misses because the crosshair may not align with the barrel.
5. Not Using Layer-Based Collision Filtering
Without layer collision matrices, every object checks collision against every other object. Set layers (Player, Enemy, Environment) and disable irrelevant pairs.
6. Overlapping Colliders on Same Object
Two colliders on one object without a rigid body causes undefined behavior. Either add a rigid body or use a single compound collider.
7. Forgetting Continuous Collision Detection
Fast-moving objects (bullets, cars) can pass through thin walls. Enable Collision Detection > Continuous on the rigid body.
Practice Questions
1. What’s the difference between AABB and sphere collision?
AABB uses axis-aligned boxes (cheaper but orientation matters). Spheres use center+radius (orientation-independent, slightly more expensive).
2. What is a raycast?
A raycast fires an invisible line from a point in a direction and returns the first object it intersects.
3. What does a rigid body’s mass affect?
Mass affects how forces (gravity, collisions) affect the object. Heavier objects push lighter ones more.
4. When would you use Continuous collision detection?
For fast-moving objects that could pass through thin geometry between frames.
5. Challenge: Implement a grappling hook.
Fire a raycast. On hit, attach a SpringJoint from the player to the hit point. Pull the player toward it with AddForce. Detach with right-click.
Mini Project: Physics Shooting Gallery
Build a shooting range with physics targets:
- Create target objects with
RigidbodyandBoxCollider - Write the
RaycastShooterscript from this tutorial - Add a
Healthscript that destroys targets at 0 HP - Add a
PhysicMaterialwith bounciness to make targets wobble - Score points for each destroyed target
- Add a reset button to respawn targets
FAQ
What’s Next
Congratulations on completing this Game Physics tutorial! Here’s where to go from here:
- Practice daily — Experiment with one physics property per day
- Build a project — Create a physics-based puzzle game
- Explore related topics — Vehicle physics, cloth simulation, destructible environments
- Join the community — Share your physics experiments and get feedback
Remember: every expert was once a beginner. Keep simulating!
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro