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

Advanced Collision and Physics Responses

Updated: May 16, 2026
30 min read

# CHAPTER 9

Advanced Collision and Physics Responses

1. Introduction

When a rubber ball hits a concrete wall at a 45-degree angle, how does the physics engine know exactly which direction it should bounce? When a heavy safe falls on a thin wooden table, how does the engine prevent the safe from sinking halfway through the wood? Up until now, we have let the physics engine handle these interactions automatically. However, to build unique gameplay mechanics—like a pool/billiards game or a ricocheting laser beam—you must understand the math behind the curtain. In this chapter, we will master Advanced Physics Responses. We will explore the Surface Normal, utilize Vector Reflection, understand the Dot Product, and learn how engines resolve object penetration.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and utilize the "Surface Normal" Vector.
  • Calculate perfect bounce/ricochet angles using Vector3.Reflect.
  • Understand how physics engines resolve overlapping colliders (Penetration).
  • Use the Dot Product to calculate the angle between two vectors.
  • Program a custom ricocheting projectile.

3. The Surface Normal

If you shoot a laser at a wall, the math needs to know exactly how the wall is angled to calculate the bounce.
  • A Surface Normal is an invisible mathematical arrow that points perfectly 90 degrees outward from a surface.
  • If the floor is flat, the normal is (0, 1, 0) (Straight Up).
  • When you perform a Raycast, the hitInfo.normal gives you this exact vector!

4. Vector Reflection (The Bounce)

The law of reflection states: *The angle of incidence equals the angle of reflection.* If a bullet hits a wall, the engine uses the bullet's incoming direction and the wall's Surface Normal to calculate the new outbound direction.
  • Engine math libraries provide a built-in method for this: Vector3.Reflect(incomingDirection, surfaceNormal).
csharp
1234567891011
void OnCollisionEnter(Collision hit)
{
    // Grab the first point of contact
    ContactPoint contact = hit.contacts[0];
    
    // Calculate the perfect bounce direction
    Vector3 bounceDirection = Vector3.Reflect(currentVelocity, contact.normal);
    
    // Apply the new direction
    rb.velocity = bounceDirection;
}

5. The Dot Product (Facing Calculations)

The Dot Product is a magical mathematical formula that compares two normalized vectors and returns a single float between -1 and 1.
  • If the Dot Product is 1: The objects are facing the exact same direction.
  • If the Dot Product is 0: The objects are perpendicular (90 degrees).
  • If the Dot Product is -1: The objects are facing exactly opposite directions.
Use Case: Is the enemy standing in front of me or behind me?
csharp
12345
Vector3 dirToEnemy = (enemy.position - player.position).normalized;
float dot = Vector3.Dot(player.forward, dirToEnemy);

if (dot > 0.5f) { Console.WriteLine("Enemy is in my forward field of view!"); }
else { Console.WriteLine("Enemy is behind me!"); }

6. Penetration and Resolution

What happens when an object moves so fast that it ends up halfway inside a wall?
  1. 1. Detection: The engine detects a deep overlap.
  1. 2. Resolution: The engine calculates the shortest possible distance required to push the object back outside the wall.
  1. 3. Application: The engine applies a massive, instantaneous opposing force (often pushing it completely out in a single frame).
*Warning:* If two objects are forced together continuously (like a door closing on a player), the engine's resolution forces will compound infinitely, often launching the player into space!

7. Visual Learning: Vector Reflection

txt
12345678
   Incoming Vector (Bullet)      Reflected Vector (Bounce)
             \                       ^
              \                     /
               v                   /
  --------------[ Impact Point ]---------------- (Wall)
                       |
                       v
                Surface Normal (90 degrees out)

8. Best Practices

  • Layer-Based Penetration Ignorance: If a sword passes through an enemy, you don't want the physics engine to violently push the player backward to resolve the penetration. You just want to deal damage. Set the sword's collider to be a Trigger, which completely disables Penetration Resolution for that object.

9. Common Mistakes

  • Assuming Physics Math is Perfect: Physics engines use floating-point math, which loses precision. If you stack 100 boxes on top of each other, the tiny math errors at the bottom will compound, and the tower will slowly jiggle and collapse. Never design a puzzle that requires 100 perfectly stacked physics objects!

10. Mini Project: Build a Ricocheting Laser

Objective: Use Raycasts and Surface Normals to draw a laser that bounces off mirrors.
csharp
12345678910111213141516171819202122232425
class BouncingLaser
{
    void Update()
    {
        Vector3 currentPos = transform.position;
        Vector3 currentDir = transform.forward;
        
        // Draw the laser for 3 bounces
        for (int i = 0; i < 3; i++)
        {
            RaycastHit hit;
            if (Physics.Raycast(currentPos, currentDir, out hit, 100f))
            {
                // Draw a debug line to the wall
                Debug.DrawLine(currentPos, hit.point, Color.red);
                
                // Calculate the bounce direction using the wall's Normal
                currentDir = Vector3.Reflect(currentDir, hit.normal);
                
                // Set the start position for the next bounce loop
                currentPos = hit.point;
            }
        }
    }
}

11. Practice Exercises

  1. 1. What does the "Surface Normal" vector represent in physics?
  1. 2. If the Dot Product of the Player's forward vector and an Enemy's forward vector is exactly -1, what are they doing?

12. MCQs with Answers

Question 1

You are building a pool (billiards) game. A ball hits the cushion. Which specific piece of data do you need from the wall's collider to accurately calculate the ricochet angle using Vector3.Reflect()?

Question 2

What is the fundamental purpose of the mathematical "Dot Product" in 3D game development?

13. Interview Questions

  • Q: Explain the law of reflection in vector mathematics. How do the incoming velocity vector and the impact point's surface normal combine to generate a bounce?
  • Q: What is Penetration Resolution in a physics engine? Describe a scenario where poor level design could cause Penetration Resolution to launch a player character out of the map.
  • Q: A stealth game requires the enemy guard to only see the player if the player is within a 45-degree cone in front of the guard's face. Explain how you would use Vector math and the Dot Product to calculate this Field of View.

14. FAQs

Q: How do I stop the physics jitter when objects rest on each other? A: In modern engines, you adjust the "Sleep Threshold." If an object's kinetic energy drops below a tiny number (0.01), the engine forcibly puts the object to sleep, completely halting the jittery micro-math calculations.

15. Summary

In Chapter 9, we looked under the hood of the collision engine. We discovered the Surface Normal—the invisible arrow that tells the computer how a wall is angled. We used this normal to calculate flawless ricochets via Vector Reflection. We mastered the Dot Product, allowing us to mathematically determine if two objects are facing each other. Finally, we learned how physics engines violently push objects apart to resolve Penetration. You now possess the math to build complex physics puzzles.

16. Next Chapter Recommendation

We understand bouncing and sliding. Now let's put wheels on an object and make it drive. Proceed to Chapter 10: Vehicle Physics.

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: ·