Skip to main content
Game Physics – Complete Beginner to Advanced Guide
CHAPTER 15 Intermediate

Physics Optimization Techniques

Updated: May 16, 2026
30 min read

# CHAPTER 15

Physics Optimization Techniques

1. Introduction

Physics calculations are notoriously expensive. A single explosion pushing 50 rigidbodies, checking collisions against a complex 3D mesh, and resolving penetrations can cripple a CPU, dropping the framerate from 60 FPS to an unplayable 10 FPS. To build commercial games—especially for mobile devices or the Nintendo Switch—you must understand how to aggressively optimize the physics pipeline. In this chapter, we will master Physics Optimization Techniques. We will learn how to prune unnecessary math using the Collision Matrix, manipulate the Fixed Timestep, utilize Rigidbody sleeping, and bypass the physics engine entirely using clever math tricks.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Optimize the Collision Matrix to prevent unnecessary overlap checks.
  • Balance the Fixed Timestep for performance vs. accuracy.
  • Understand and configure Rigidbody Sleep mechanics.
  • Replace heavy physical projectiles with cheap Raycast math.
  • Avoid common CPU spikes caused by modifying colliders at runtime.

3. The Collision Matrix (Pruning the Tree)

The absolute best way to optimize physics is to *not calculate them*. If you have 100 bullets in the air, the physics engine checks: "Is Bullet 1 hitting Bullet 2? Is Bullet 1 hitting Bullet 3?"
  • This is wasted CPU power. Bullets don't need to collide with other bullets!
  • The Fix: Open the Engine's Physics/Collision Matrix. Uncheck the box where the "Bullet" layer intersects with the "Bullet" layer. The CPU will instantly ignore thousands of math checks every frame.

4. Tuning the Fixed Timestep

As learned, physics calculations happen in the FixedUpdate loop.
  • By default, engines run this 50 times a second (0.02 timestep).
  • If your game is a slow-paced puzzle game, 50 calculations a second is massive overkill. You can change the Fixed Timestep to 0.04 (25 times a second). You instantly cut the CPU physics load in half!
  • *Warning:* If you have fast-moving cars, lowering the timestep will cause the cars to tunnel through walls.

5. Rigidbody Sleeping

Physics engines are smart. If a wooden crate stops moving, the engine puts its Rigidbody to "Sleep." A sleeping Rigidbody requires zero CPU calculations until another object bumps into it and wakes it up.
  • The Optimization: Ensure your rigidbodies actually go to sleep! If you have a script that applies a tiny random jitter force to the crate every frame, it will NEVER sleep, and 100 crates will lag the game permanently.

6. Primitive Colliders vs. Mesh Colliders

We discussed this in Chapter 5, but it is the #1 cause of lag in indie games.
  • A Sphere Collider takes exactly 1 math calculation (Distance < Radius).
  • A Mesh Collider (a 10,000 polygon rock) requires the CPU to check intersection against thousands of tiny triangles.
  • The Rule: NEVER use moving Mesh Colliders. Always wrap complex models in an invisible compound of 2 or 3 simple Box/Sphere colliders.

7. Avoiding Runtime Collider Changes

If you change the transform.localScale of an object with a Collider, or if you turn a Collider on and off (enabled = false) during gameplay, the physics engine must recalculate the entire internal spatial tree (Broadphase). Doing this frequently causes massive CPU spikes.
  • The Fix: If an object is destroyed, just move it far below the floor and disable the visual renderer (Object Pooling), rather than constantly deleting and rebuilding colliders in memory.

8. Visual Learning: The Collision Matrix

txt
1234567
          [Player]   [Enemy]   [Bullet]   [Environment]
[Player]     [ ]       [X]       [ ]          [X]
[Enemy]      [X]       [ ]       [X]          [X]
[Bullet]     [ ]       [X]       [ ]          [X]
[Environ]    [X]       [X]       [X]          [ ]

*Blank spaces are completely ignored by the CPU! Massive savings.*

9. Best Practices

  • Use Non-Allocating Overlaps: When creating an explosion, Physics.OverlapSphere creates a new Array in RAM every time it fires, generating Garbage. Advanced programmers use Physics.OverlapSphereNonAlloc. You provide a pre-existing empty Array, and the engine fills it. Zero garbage created!

10. Common Mistakes

  • Raycasting Everything: Raycasts are cheap, but they aren't free. If an enemy AI shoots 50 raycasts every single frame to check for obstacles, the game will lag. Use timed coroutines to only shoot Raycasts 3 or 4 times a second per enemy.

11. Mini Project: Optimized Proximity Check

Objective: Replace an expensive physics overlap with a cheap math check. If you just want to know if the player is near a bomb, do NOT use OnTriggerStay or Physics.OverlapSphere. Use simple vector math!
csharp
123456789101112131415161718192021222324
class OptimizedBomb
{
    public Transform player;
    public float explosionRadius = 5.0f;
    
    // We pre-calculate the squared radius to avoid Square Roots!
    private float sqrRadius;

    void Start()
    {
        sqrRadius = explosionRadius * explosionRadius; // 25.0f
    }

    void Update()
    {
        // Vector3.sqrMagnitude is insanely fast for the CPU
        float distToPlayer = (transform.position - player.position).sqrMagnitude;

        if (distToPlayer <= sqrRadius)
        {
            Console.WriteLine("Player is in the blast zone!");
        }
    }
}

12. Practice Exercises

  1. 1. How does utilizing the Collision Matrix optimize CPU performance?
  1. 2. What happens mathematically when you change the engine's Fixed Timestep from 0.02 to 0.04?

13. MCQs with Answers

Question 1

A game has 1,000 coins scattered across a level. The coins spin using a C# script, but they do not fall or bounce. To ensure maximum performance, what physics configuration should the coins have?

Question 2

When checking for objects caught in an explosion blast, which C# method is the most highly optimized because it does not allocate new memory arrays (preventing Garbage Collection)?

14. Interview Questions

  • Q: Explain the concept of Rigidbody Sleeping. What programmatic actions can accidentally prevent a Rigidbody from sleeping, destroying performance?
  • Q: Contrast the performance costs of a Primitive Box Collider versus a Custom Mesh Collider. Why are Mesh Colliders strictly forbidden on moving objects in professional game studios?
  • Q: A mobile game is running at 20 FPS due to heavy physics load. Walk me through three specific steps you would take to optimize the physics pipeline within the engine settings.

15. FAQs

Q: Can I run physics on the GPU instead of the CPU? A: Yes! Modern techniques (like Unity's DOTS/Havok or compute shaders) offload physics math to the graphics card, which can handle thousands of collisions simultaneously. However, this requires writing highly advanced, specialized C# code that abandons standard object-oriented programming.

16. Summary

In Chapter 15, we became performance engineers. We learned that the secret to fast physics is avoiding unnecessary calculations. We pruned our math tree using the Collision Matrix. We reduced the global physics load by tweaking the Fixed Timestep, and ensured our inactive objects properly went to Sleep. Finally, we learned how to bypass the physics engine entirely using high-speed sqrMagnitude math for proximity checks. Our simulation is now blazing fast.

17. Next Chapter Recommendation

We have mastered the universal concepts of game physics. Now, let's look at how the industry's most popular engine implements them. Proceed to Chapter 16: Physics Systems in Unity.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·