When developing 2D games in Godot Engine, choosing which node to use to give objects "physical behavior" is crucial. Particularly, the three main physics body nodes—CharacterBody2D, RigidBody2D, and StaticBody2D—each have different design philosophies and purposes. Understanding and properly using these directly impacts game performance, control ease, and bug reduction.
This article provides a thorough explanation of the basic definitions, characteristics, and appropriate application of these three physics body node types to various game objects, with concrete code examples.
Why Physics Body Selection Matters
Godot Engine's physics engine automatically handles complex calculations like collision detection, gravity, and friction. However, leaving all objects entirely to the physics engine can prevent you from achieving intended movements or cause unexpected behaviors (bugs).
Player characters, where you want complete control over movement, and falling boxes, which should follow physics laws, require different node selections. This selection is the first step that determines game development efficiency and quality.
Definitions of the Three Main Physics Body Nodes
All three nodes inherit from CollisionObject2D and can have collision shapes (CollisionShape2D), but their "movement style" and "control methods" fundamentally differ.
1. StaticBody2D (Static Body)
As the name suggests, StaticBody2D is a "static" physics body.
- Characteristics:
- Does Not Move: Unaffected by physics engine influences (gravity, forces, etc.) and doesn't move.
- Collision Wall: Detects collisions with other physics bodies and functions as a "wall" blocking their movement.
- Control: While you can directly change
positionin code, it's primarily used for non-moving objects.
- Main Uses:
- Ground, walls, ceilings, non-moving platforms
- Map boundaries, obstacles
2. RigidBody2D (Rigid Body)
RigidBody2D is a body completely simulated by the physics engine as a "Rigid Body."
- Characteristics:
- Physics Engine Control: Follows all physics laws including gravity, friction, inertia, and forces from other objects (collisions).
- Control: Rather than directly manipulating object
position, developers control movement by applying "forces" using methods likeapply_forceorapply_torque. - Modes: Has four modes—
MODE_RIGID(default),MODE_STATIC,MODE_CHARACTER,MODE_KINEMATIC—but typically uses the defaultMODE_RIGID.
- Main Uses:
- Falling boxes, balls, ragdolls (physics-simulated characters)
- Projectiles, explosion debris
3. CharacterBody2D (Character Body)
CharacterBody2D is designed for users to control movement "kinematically" through code (renamed from KinematicBody2D in Godot 4.0).
- Characteristics:
- Code Control: While affected by physics engine forces like gravity, it won't be pushed around by collisions from other
RigidBody2Dobjects. - Movement Method: Uses the
move_and_slide()method for movement. This method receives a movement vector and smoothly moves objects while detecting collisions. - Flexibility: Optimal for implementing precise movements based on player input (jumping, dashing, wall climbing, etc.).
- Code Control: While affected by physics engine forces like gravity, it won't be pushed around by collisions from other
- Main Uses:
- Player characters, enemy characters, NPCs
- Elevators and moving platforms with predictable movement
Physics Body Comparison Table
| Node Name | Control Method | Physics Engine Influence | Interaction with Other Objects | Main Uses |
|---|---|---|---|---|
| StaticBody2D | None (fixed) | Not affected | Becomes collision-blocking wall | Ground, walls, immovable obstacles |
| RigidBody2D | Forces (apply_force, etc.) | Affected (fully simulated) | Moved by collisions | Balls, boxes, physics puzzle elements |
| CharacterBody2D | Code (move_and_slide) | Affected by gravity, etc. | Blocks collisions but isn't moved | Players, enemies, moving platforms |
Practical Example: Player Control with CharacterBody2D
In most games, CharacterBody2D is used for player characters. This is because you want immediate, predictable responses to player input. Using RigidBody2D often leads to poor control feel due to physics calculation delays and unexpected inertia.
Here's a basic implementation of movement and gravity using CharacterBody2D:
extends CharacterBody2D
# Movement speed (pixels/second)
const SPEED = 300.0
# Gravity acceleration
const JUMP_VELOCITY = -450.0
const GRAVITY = 980.0
# Player velocity vector
var velocity = Vector2.ZERO
func _physics_process(delta):
# 1. Apply gravity
# Only apply gravity when not on ground
if not is_on_floor():
velocity.y += GRAVITY * delta
# 2. Process input
var direction = Input.get_axis("ui_left", "ui_right")
# Horizontal movement
if direction:
velocity.x = direction * SPEED
else:
# Smoothly decelerate when no input
velocity.x = move_toward(velocity.x, 0, SPEED)
# Jump
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# 3. Execute movement
# CharacterBody2D's core method.
# Moves according to velocity vector and performs slide processing when collisions occur.
move_and_slide()
In this code, developers directly manipulate velocity and pass the result to move_and_slide(), achieving safe and precise movement that considers collisions.
Tips for Appropriate Node Selection
When unsure about node selection, try answering these questions:
- Does the object move?
- No: Choose
StaticBody2D. (e.g., walls, ground)
- No: Choose
- Do you want to completely entrust object movement to physics laws (gravity, collisions, forces)?
- Yes: Choose
RigidBody2D. (e.g., balls, boxes)
- Yes: Choose
- Do you want to precisely control object movement through player input or AI logic?
- Yes: Choose
CharacterBody2D. (e.g., players, enemies)
- Yes: Choose
Particularly, remember that CharacterBody2D should almost always be used for player characters. Using RigidBody2D for players often requires complex code to counteract physics engine behavior, making development more difficult.
Summary
Godot Engine's 2D physics bodies have clearly defined roles:
- StaticBody2D: Non-moving environmental elements.
- RigidBody2D: Objects fully governed by physics laws.
- CharacterBody2D: Characters precisely controlled through code.
Understanding these differences and selecting appropriate nodes based on game object characteristics enables smoother game development with intended behaviors.