godot-physics-cheat-sheet

Godot physics cheat sheet

When people speak of “game physics,” they often think of realistic simulations.

But in a game engine, the physics engine takes care of three problems:

  1. Detecting overlaps or intersections. For example, when looting an item or shooting an enemy.
  2. Moving objects without allowing them to intersect.
  3. Simulating believable physics, considering gravity, friction, elasticity, and more.

We generally call the first two problems collisions.

Godot’s Physics Nodes

In Godot, four physics nodes address these three problems:

We generally use them like so:

Area2D

Area2D handles overlaps. It only detects that another CollisionObject2D entered or exited from it.

An Area2D will not stop by default if it encounters another object.

You use Area2D for items, doors, traps, chests, buttons, contextual actions, bullets, …

Area2D’s most used signals are:

KinematicBody2D

KinematicBody2D stops when it touches the ground, a wall, or another KinematicBody2D.

You use KinematicBody2D for moving entities that need to collide with walls like the player’s character, enemies, non-playable characters, and more.

KinematicBody2D most used functions are:

RigidBody2D

RigidBody2D, like KinematicBody2D, collides with obstacles. But, contrary to KinematicBody2D, the physics engine moves it automatically.

You rarely move a RigidBody2D yourself. Instead, you give it base properties and apply impulses or forces to it. You use RigidBody2D for bouncing balls, bullet shells, pushable blocks, ropes, and what we call “physics games.” Think of games like Angry Birds, for example.

StaticBody2D

StaticBody2D stops KinematicBody2D and RigidBody2D.

Despite the word “static” in the name, a StaticBody2D can move, but it will not detect collisions and will not stop if it encounters another physics node. You use StaticBody2D for walls, the ground, or moving platforms.

Collision shapes

Each physics object needs one or more geometrical shapes to calculate collisions: at least one CollisionShape2D or one CollisionPolygon2D node.

You can give a CollisionShape2D or a CollisionPolygon2D node as many shapes as you need to get accurate collisions.

CollisionShape2D

CollisionShape2D gives you a choice between multiple pre-determined shapes like rectangles, circles, and capsules.

These are the shapes we use most in 2D games because they give you the easiest collisions to manage.

CollisionPolygon2D

CollisionPolygon2D allows you to draw a polygon by hand or programmatically. We use it for irregular shapes.

Collision Layers & Collisions Masks

Not every physics node must check against every other node in the game.

For example, pickups don’t need to detect enemies, and the player character doesn’t need to detect the bullets it shoots.

To determine what detects what, we can use collision layers and collision masks:

Your questions

Why do I need to add a collision node instead of defining the shape on the physics object directly?

It is allowed, and even frequent, to have more than one collision node for a physics object. If the physics object accepted one geometrical shape, we could have only one shape per object.

Can I use a RigidBody2D for my player or enemies?

It’s not advised, because controlling a RigidBody or a RigidBody2D is very difficult. Making that control fun is hard, and there could be edge cases that make the character misbehave.

We generally keep rigid bodies for less important parts of gameplay.

If we do need the character to be simulated, we will usually either create an invisible RigidBody2D and make the character follow it; or we will switch the character with a different node (for example, to make a character ragdoll on death).

KinematicBody2D does not have a signal for when a body enters it, how can I know if my player intersects an item?

There’s nothing to say we cannot add an Area2D as a child of a KinematicBody2D.

We can even add several! We could have a KinematicObject2D for collisions, and then:

  1. One Area2D for picking objects.
  2. A smaller Area2D for receiving hits (commonly referred to as a “hurtbox”).
  3. Another animated Area2D for the weapon (commonly referred to as a “hitbox”).

This is a common setup for many games.