Skip to main content
Godot Fundamentals – Complete Beginner to Advanced Guide
CHAPTER 07 Beginner

Physics and Collision Detection

Updated: May 16, 2026
20 min read

# CHAPTER 7

Physics and Collision Detection

1. Introduction

Video games are built on the illusion of physical reality. A screen is just flat pixels, but the Physics Engine makes a digital ball bounce off a wall, makes wooden crates tumble down a hill, and allows a sword to deal damage when it strikes an enemy. In Godot, the physics system is handled by three distinct families of Nodes, each designed for a specific physical purpose. In this chapter, we will master Physics and Collision Detection. We will explore the chaotic simulation of RigidBodies, understand how to create invisible tripwires using Area2D, and learn how to optimize collisions using Collision Layers.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Differentiate between CharacterBody2D, RigidBody2D, and StaticBody2D.
  • Create an Area2D to act as an invisible trigger or hitbox.
  • Use Signals (bodyentered) to detect when objects overlap.
  • Understand and configure Collision Layers and Masks.
  • Simulate gravity, mass, and bouncing with Physics Materials.

3. The Three Physics Bodies

Godot requires you to choose *how* an object should behave in the physics world:
  1. 1. StaticBody2D (The Wall): Objects that never move. Floors, walls, and mountains. They stop other objects from passing through them, but cost almost zero processing power.
  1. 2. CharacterBody2D (The Puppet): Objects controlled by code. (e.g., The Player). They only move when your code tells them to, but they respect walls.
  1. 3. RigidBody2D (The Simulation): Objects controlled entirely by the physics engine. (e.g., A bouncing ball, a tumbling wooden crate). You give them mass and drop them. Gravity and physics take over. *Do not move these with code!*

4. Area2D (The Invisible Tripwire)

An Area2D does not stop objects from moving. It is a "ghost" node used strictly for detection.
  • Use Cases: Coins (detecting when the player touches it to collect it), Spikes (detecting the player to deal damage), and Doorways (detecting the player to load the next level).
  • The Workflow:
  1. 1. Add an Area2D and a CollisionShape2D.
  1. 2. In the Node dock, connect the bodyentered signal to a script.
  1. 3. When the Player walks into the area, the code runs!

5. Collision Layers and Masks (The Filter)

If you shoot a bullet, you want it to hit Enemies, but you want it to pass through friendly NPCs and tall grass.
  • Layer: "What am I?" (e.g., The Player is on Layer 1. The Enemy is on Layer 2).
  • Mask: "What can I hit?" (e.g., The Player's Mask is set to Layer 2. This means the Player can hit Enemies).
By carefully setting up Layers and Masks in the Inspector, you optimize your game (so bullets don't waste CPU calculating collisions against grass) and easily script complex interactions.

6. Physics Material (Bounce and Friction)

A rock and a rubber ball are both RigidBodies, but they react differently when dropped.
  • In the Inspector for a RigidBody2D or StaticBody2D, you can add a PhysicsMaterial.
  • Friction: A high number (1.0) acts like sandpaper. A low number (0.0) acts like slippery ice.
  • Bounce: A high number (1.0) creates a super bouncy rubber ball.

7. Visual Learning: The Area2D Trigger Flow

txt
123456789101112131415
[ Player (CharacterBody2D) ] walks right --->
                          |
                          v
           [ Coin (Area2D + CollisionShape2D) ]

*Overlap Occurs!*
                          |
[ Coin Node System ] emits signal: `body_entered(body)`
                          |
                          v
[ Coin Script ]
func _on_body_entered(body):
    if body.name == "Player":
        add_score(10)
        queue_free()  # Deletes the coin from the level

8. Best Practices

  • Name Your Layers: Do not just rely on "Layer 1" and "Layer 2." Go to Project Settings -> Layer Names -> 2D Physics and explicitly name Layer 1 "Player," Layer 2 "World," Layer 3 "Enemies." This makes assigning Masks in the Inspector much less confusing later in development.

9. Common Mistakes

  • Scaling Collision Shapes: NEVER scale a CollisionShape2D node using the main Scale tool (the S key or Transform property). Doing so warps the math inside the physics engine and creates bizarre collision bugs. Always change the size of the shape using the red handles on the shape itself or the specific "Size/Radius" parameters inside the Shape resource in the Inspector.

10. Mini Project: Build a Bouncing Ball Pit

Objective: Let the physics engine do the heavy lifting.
  1. 1. Create a Node2D Level scene.
  1. 2. Build a floor and two walls using three StaticBody2D nodes. Add a CollisionShape2D (Rectangle) to each so they are solid.
  1. 3. Create a new Scene. Root node: RigidBody2D. Name it Ball.
  1. 4. Add a Sprite2D (a circle graphic) and a CollisionShape2D (Circle shape).
  1. 5. In the RigidBody2D inspector, add a New PhysicsMaterial. Set Bounce to 0.8.
  1. 6. Go back to your Level scene. Instance 20 Ball scenes hovering in the air above the floor.
  1. 7. Hit Play. Watch gravity pull the balls down, colliding, bouncing, and tumbling over each other perfectly with zero lines of code!

11. Practice Exercises

  1. 1. If you are creating a wooden crate that the player can push down a hill using physics, which root node should you choose: StaticBody2D or RigidBody2D?
  1. 2. Explain the purpose of the queuefree() command in Godot.

12. MCQs with Answers

Question 1

You want to create a zone of toxic gas. It shouldn't block the player from walking through it, but it needs to detect when the player is inside so it can deal damage. Which node should be the root of the toxic gas?

Question 2

When configuring physics collisions in Godot, what is the difference between a "Layer" and a "Mask"?

13. Interview Questions

  • Q: Explain the distinct differences between StaticBody2D, RigidBody2D, and CharacterBody2D. When would you use each in a platformer game?
  • Q: A junior developer is trying to make a RigidBody ball move to the right by using position.x += 10 in the process function. The ball is glitching wildly. Why is this wrong, and how should forces be applied to a RigidBody? (Hint: applyimpulse).
  • Q: Walk me through the exact steps to script a collectible Coin using an Area2D and the bodyentered signal.

14. FAQs

Q: Can I change the gravity of the whole game? A: Yes. Go to Project Settings -> Physics -> 2D. Here you can change the Default Gravity value. You can also use an Area2D to create "Gravity Zones" that pull the player sideways or upwards (like a tornado) using the Area's Space Override settings.

15. Summary

In Chapter 7, we defined the physical laws of our digital universe. We learned that the physics engine offers three distinct body types: Static walls, Character puppets, and simulated RigidBodies. We mastered the Area2D node to create invisible triggers, using Godot's Signal system to detect overlaps and execute code. Finally, we learned how to optimize the physics engine by sorting objects into Collision Layers and Masks. The game world is no longer just a painting; it is solid, reactive, and dangerous.

16. Next Chapter Recommendation

The world works perfectly, but the player doesn't know their score or health. We need to display information on the screen. Proceed to Chapter 8: UI and HUD Systems.

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