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.normalgives 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
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.
csharp
6. Penetration and Resolution
What happens when an object moves so fast that it ends up halfway inside a wall?- 1. Detection: The engine detects a deep overlap.
- 2. Resolution: The engine calculates the shortest possible distance required to push the object back outside the wall.
- 3. Application: The engine applies a massive, instantaneous opposing force (often pushing it completely out in a single frame).
7. Visual Learning: Vector Reflection
txt
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
11. Practice Exercises
- 1. What does the "Surface Normal" vector represent in physics?
-
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.