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:
We generally call the first two problems collisions.
In Godot, four physics nodes address these three problems:
We generally use them like so:
Area2D
CollisionObject2D
entered or exited from
it.
An
will not stop by default if it encounters another object.You use
for items, doors, traps, chests, buttons, contextual actions, bullets, …’s most used signals are:
area_entered(area: Area2D)
and
area_exited(area: Area2D)
when another enters or exits the area.body_entered(body: PhysicsBody2D)
and
body_exited(body: PhysicsBody2D)
when a , a or a enters or exits the area.KinematicBody2D
stops when it touches the ground, a wall, or another .
You use
for moving entities that need to collide with walls like the player’s character, enemies, non-playable characters, and more.most used functions are:
move_and_slide(velocity: Vector2, up_direction: Vector2)
,
which moves a body and slides along walls if the collision angle isn’t
90 degrees.move_and_collide(velocity: Vector2)
, which lets the
programmer decide what happens when a collision occurs.is_on_floor()
, is_on_wall()
,
is_on_ceiling()
, which give you useful information about
the body’s location.RigidBody2D
, like , collides with obstacles. But, contrary to , the physics engine moves it automatically.
You rarely move a
yourself. Instead, you give it base properties and apply impulses or forces to it. You use for bouncing balls, bullet shells, pushable blocks, ropes, and what we call “physics games.” Think of games like Angry Birds, for example.StaticBody2D
stops and .
Despite the word “static” in the name, a
can move, but it will not detect collisions and will not stop if it encounters another physics node. You use for walls, the ground, or moving platforms.Each physics object needs one or more geometrical shapes to calculate collisions: at least one
or one node.You can give a
or a node as many shapes as you need to get accurate collisions.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
allows you to draw a polygon by hand or programmatically. We use it for irregular shapes.
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:
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.
RigidBody2D
for my player or enemies?It’s not advised, because controlling a
or a 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
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
as a child of a .We can even add several! We could have a
KinematicObject2D
for collisions, and then:
This is a common setup for many games.