Godot Physics (4.x, 2D + 3D)
Pick the right physics body, wire up collision layers/masks, detect overlaps, and cast
rays. Concepts apply to both 2D and 3D (swap the 2D/3D suffix). Targets
Godot 4.7. In 3D, projects created with Godot 4.6 or later run Jolt Physics by
default; older projects keep GodotPhysics3D unless someone switched them. 2D always
uses Godot's own 2D engine.
When to use
- Use when choosing between body types, setting collision layers/masks so the right
things collide, detecting overlaps (triggers, hurtboxes) with
Area, applying
forces/impulses to a RigidBody, or casting rays for line-of-sight/ground checks.
When not to use: kinematic character controllers (move_and_slide) →
godot-2d-movement; tile collision setup → godot-tilemap; tuning the feel of physics
(timestep, mass, jitter) → physics-tuning.
Core workflow
- For 3D, check which engine runs before debugging behavior: Project Settings >
Physics > 3D > Physics Engine (
[physics] 3d/physics_engine="Jolt Physics" in
project.godot). Projects created in 4.6+ get Jolt written there by default; with no
such line (DEFAULT) the engine is GodotPhysics3D. The API is the same, but joints,
ray-cast face indices, kinematic contacts, and collision margins behave differently
(see Pitfalls).
- Choose the body type:
StaticBody — never moves (floors, walls). Collides, no simulation.
RigidBody — fully simulated (gravity, forces, bouncing). Don't set its
position directly; apply forces/impulses or set linear_velocity.
CharacterBody — script-driven kinematic (see godot-2d-movement).
Area — detects overlaps and can apply gravity/damping; no solid collision.
Every body needs a CollisionShape (or CollisionPolygon) child.
- Configure layers and masks. A body is on its layers and scans for its
masks. Two bodies interact only if one's layer is in the other's mask. Name layers
in Project Settings > Layer Names for clarity.
- Detect overlaps with
Area signals (body_entered, area_entered).
- Drive RigidBodies with forces/impulses, or override
_integrate_forces for full
control.
- Cast rays with a
RayCast2D/3D node (polled each frame) or a one-shot space-state
query from code.
Patterns
1. Collision layers vs masks (set from code)
# Player is on layer 1, scans layers 2 (walls) and 3 (enemies).
func _ready() -> void:
set_collision_layer_value(1, true) # I am on layer 1
set_collision_mask_value(2, true) # I collide with things on layer 2
set_collision_mask_value(3, true) # ...and layer 3
# Bit-field forms also exist: collision_layer = 1; collision_mask = 0b110
2. Area2D as a trigger / hurtbox
extends Area2D # e.g. a damage zone
func _ready() -> void:
body_entered.connect(_on_body_entered)
area_entered.connect(_on_area_entered)
func _on_body_entered(body: Node2D) -> void:
if body.has_method("take_damage"):
body.take_damage(10)
func _on_area_entered(area: Area2D) -> void:
print("Overlapped area: ", area.name)
3. Applying force and impulse to a RigidBody3D
extends RigidBody3D
func push(direction: Vector3) -> void:
apply_central_impulse(direction * 8.0) # instantaneous velocity change
func _physics_process(_delta: float) -> void:
apply_central_force(Vector3.FORWARD * 4.0) # continuous force (per tick)
# Never set `position` on a RigidBody to move it; use forces/impulses or
# set linear_velocity. Use freeze=true if you must hold it in place.
4. Raycast two ways
# A) RayCast2D node: enable it, then poll after physics has updated.
@onready var ray: RayCast2D = $RayCast2D # set target_position in the editor
func _physics_process(_delta: float) -> void:
if ray.is_colliding():
var hit := ray.get_collider()
var point := ray.get_collision_point()
# B) One-shot query from code (no node needed).
func ground_under(global_from: Vector2) -> Dictionary:
var space := get_world_2d().direct_space_state
var query := PhysicsRayQueryParameters2D.create(global_from, global_from + Vector2(0, 64))
query.collision_mask = 1 # only layer 1
return space.intersect_ray(query) # {} if nothing hit, else collider/position/normal
Pitfalls
- Layer vs mask confusion is the #1 bug. Layer = "what I am"; mask = "what I look
for". For A to detect B, B's layer must be in A's mask. Detection can be one-directional.
- Moving a RigidBody by
position fights the solver and causes tunneling/jitter.
Use impulses/forces, set linear_velocity, or freeze it. To teleport, set position
and zero the velocities inside _integrate_forces.
Area doesn't fire when neither monitoring nor monitorable is set, or layers/masks
don't overlap. monitoring must be on for the Area to detect; monitorable lets others
detect it.
- RayCast2D/3D read stale or no data if
enabled is false, or if you read it before
physics updated — read in _physics_process, and call force_raycast_update() after
moving it within the same tick.
- Forgetting a
CollisionShape (or leaving it empty) means the body never collides.
- Fast objects tunnel through thin walls; enable continuous CD on the RigidBody
(
continuous_cd) or use a raycast-based check.
intersect_ray excludes its own body? Pass query.exclude = [self.get_rid()] (an
Array[RID], not an array of nodes) to skip self-hits.
Jolt Physics (3D) differences
face_index is always -1 in intersect_ray() / RayCast3D results under Jolt.
Turn on Project Settings > Physics > Jolt Physics 3D > Queries > Enable Ray Cast Face
Index if you need it (it costs roughly 25% more memory for ConcavePolygonShape3D).
- A joint with only one body gets inverted limits after switching engines. Jolt treats
the lone body as
node_b with node_a as the world; GodotPhysics3D always treats it as
node_a. Assign the body to the slot Jolt expects, or use Physics > Jolt Physics 3D >
Joints > World Node for compatibility with an older project.
- Joint soft-limit properties do nothing under Jolt:
bias/damping/impulse_clamp on
PinJoint3D; bias/softness/relaxation on HingeJoint3D and ConeTwistJoint3D;
angular_* and limit softness/restitution/damping on SliderJoint3D; limit
softness/restitution/damping/erp on Generic6DOFJoint3D. Godot warns when they are set
to non-default values.
- A frozen kinematic
RigidBody3D reports no contacts with static or kinematic bodies,
even with max_contacts_reported > 0. Enable Physics > Jolt Physics 3D > Simulation >
Generate All Kinematic Contacts if gameplay depends on those contacts.
- Shape
margin shrinks the shape instead of padding it under Jolt, so sizes stay
true but shape queries can return odd normals on small shapes; the effective margin comes
from Physics > Jolt Physics 3D > Collisions > Collision Margin Fraction.
Area3D now fires body_entered for SoftBody3D under Jolt (GodotPhysics3D never
did). Filter soft bodies out with layers/masks if the old behavior is expected.
- A
WorldBoundaryShape3D floor sits at the wrong height after upgrading to 4.7 → under
Jolt, 4.7 reads plane.d with Godot's sign convention, the opposite of 4.6; flip the sign
of d in projects tuned on 4.6. Jolt also caps the plane's effective size (Physics > Jolt
Physics 3D > Limits > World Boundary Shape Size), so bodies far from the origin can fall
off an "infinite" floor.
References
- For
_integrate_forces, joints, one-way collision, PhysicsServer direct access,
shape queries (intersect_shape), and 3D move_and_collide, read
references/bodies-and-queries.md.
- Jolt specifics: Godot docs "Using Jolt Physics"
(
https://docs.godotengine.org/en/stable/tutorials/physics/using_jolt_physics.html).
Related skills
godot-2d-movement — kinematic CharacterBody2D controllers.
godot-tilemap — tile collision shapes and their layers.
physics-tuning — engine-agnostic feel: timestep, mass, drag, CCD.
godot-3d-essentials — 3D scene setup these bodies live in.
1---2name: godot-physics3description: >4 Use Godot 4.7 physics bodies and detection in 2D and 3D: RigidBody, StaticBody,5 Area, and CharacterBody; collision layers vs masks; contact/overlap signals; and6 raycasts (RayCast nodes and direct space-state queries). Use when configuring7 collision layers/masks, detecting overlaps with Area2D/Area3D, applying forces to a8 RigidBody, or casting rays in a Godot project (.tscn with physics bodies), including9 3D projects running Jolt Physics, the default 3D engine for projects created in 4.6+.10metadata:11 source: https://skillmd.com/skills/junmystery/godot-physics12---1314# Godot Physics (4.x, 2D + 3D)1516Pick the right physics body, wire up collision layers/masks, detect overlaps, and cast17rays. Concepts apply to both 2D and 3D (swap the `2D`/`3D` suffix). Targets18**Godot 4.7**. In 3D, projects created with Godot 4.6 or later run **Jolt Physics** by19default; older projects keep **GodotPhysics3D** unless someone switched them. 2D always20uses Godot's own 2D engine.2122## When to use2324- Use when choosing between body types, setting collision layers/masks so the right25 things collide, detecting overlaps (triggers, hurtboxes) with `Area`, applying26 forces/impulses to a `RigidBody`, or casting rays for line-of-sight/ground checks.2728**When *not* to use:** kinematic character controllers (`move_and_slide`) →29`godot-2d-movement`; tile collision setup → `godot-tilemap`; tuning the *feel* of physics30(timestep, mass, jitter) → `physics-tuning`.3132## Core workflow33341. **For 3D, check which engine runs** before debugging behavior: Project Settings >35 Physics > 3D > Physics Engine (`[physics]` `3d/physics_engine="Jolt Physics"` in36 `project.godot`). Projects created in 4.6+ get Jolt written there by default; with no37 such line (`DEFAULT`) the engine is GodotPhysics3D. The API is the same, but joints,38 ray-cast face indices, kinematic contacts, and collision margins behave differently39 (see Pitfalls).402. **Choose the body type:**41 - `StaticBody` — never moves (floors, walls). Collides, no simulation.42 - `RigidBody` — fully simulated (gravity, forces, bouncing). Don't set its43 `position` directly; apply forces/impulses or set `linear_velocity`.44 - `CharacterBody` — script-driven kinematic (see `godot-2d-movement`).45 - `Area` — detects overlaps and can apply gravity/damping; no solid collision.46 Every body needs a `CollisionShape` (or `CollisionPolygon`) child.473. **Configure layers and masks.** A body is *on* its **layers** and *scans for* its48 **masks**. Two bodies interact only if one's layer is in the other's mask. Name layers49 in Project Settings > Layer Names for clarity.504. **Detect overlaps** with `Area` signals (`body_entered`, `area_entered`).515. **Drive RigidBodies with forces/impulses**, or override `_integrate_forces` for full52 control.536. **Cast rays** with a `RayCast2D/3D` node (polled each frame) or a one-shot space-state54 query from code.5556## Patterns5758### 1. Collision layers vs masks (set from code)5960```gdscript61# Player is on layer 1, scans layers 2 (walls) and 3 (enemies).62func _ready() -> void:63 set_collision_layer_value(1, true) # I am on layer 164 set_collision_mask_value(2, true) # I collide with things on layer 265 set_collision_mask_value(3, true) # ...and layer 366 # Bit-field forms also exist: collision_layer = 1; collision_mask = 0b11067```6869### 2. Area2D as a trigger / hurtbox7071```gdscript72extends Area2D # e.g. a damage zone7374func _ready() -> void:75 body_entered.connect(_on_body_entered)76 area_entered.connect(_on_area_entered)7778func _on_body_entered(body: Node2D) -> void:79 if body.has_method("take_damage"):80 body.take_damage(10)8182func _on_area_entered(area: Area2D) -> void:83 print("Overlapped area: ", area.name)84```8586### 3. Applying force and impulse to a RigidBody3D8788```gdscript89extends RigidBody3D9091func push(direction: Vector3) -> void:92 apply_central_impulse(direction * 8.0) # instantaneous velocity change9394func _physics_process(_delta: float) -> void:95 apply_central_force(Vector3.FORWARD * 4.0) # continuous force (per tick)96 # Never set `position` on a RigidBody to move it; use forces/impulses or97 # set linear_velocity. Use freeze=true if you must hold it in place.98```99100### 4. Raycast two ways101102```gdscript103# A) RayCast2D node: enable it, then poll after physics has updated.104@onready var ray: RayCast2D = $RayCast2D # set target_position in the editor105106func _physics_process(_delta: float) -> void:107 if ray.is_colliding():108 var hit := ray.get_collider()109 var point := ray.get_collision_point()110111# B) One-shot query from code (no node needed).112func ground_under(global_from: Vector2) -> Dictionary:113 var space := get_world_2d().direct_space_state114 var query := PhysicsRayQueryParameters2D.create(global_from, global_from + Vector2(0, 64))115 query.collision_mask = 1 # only layer 1116 return space.intersect_ray(query) # {} if nothing hit, else collider/position/normal117```118119## Pitfalls120121- **Layer vs mask confusion** is the #1 bug. Layer = "what I am"; mask = "what I look122 for". For A to detect B, B's layer must be in A's mask. Detection can be one-directional.123- **Moving a RigidBody by `position`** fights the solver and causes tunneling/jitter.124 Use impulses/forces, set `linear_velocity`, or `freeze` it. To teleport, set position125 and zero the velocities inside `_integrate_forces`.126- **`Area` doesn't fire** when neither monitoring nor monitorable is set, or layers/masks127 don't overlap. `monitoring` must be on for the Area to detect; `monitorable` lets others128 detect it.129- **RayCast2D/3D read stale or no data** if `enabled` is false, or if you read it before130 physics updated — read in `_physics_process`, and call `force_raycast_update()` after131 moving it within the same tick.132- **Forgetting a `CollisionShape`** (or leaving it empty) means the body never collides.133- **Fast objects tunnel** through thin walls; enable **continuous CD** on the RigidBody134 (`continuous_cd`) or use a raycast-based check.135- **`intersect_ray` excludes its own body?** Pass `query.exclude = [self.get_rid()]` (an136 `Array[RID]`, not an array of nodes) to skip self-hits.137138### Jolt Physics (3D) differences139140- **`face_index` is always `-1`** in `intersect_ray()` / `RayCast3D` results under Jolt.141 Turn on Project Settings > Physics > Jolt Physics 3D > Queries > Enable Ray Cast Face142 Index if you need it (it costs roughly 25% more memory for `ConcavePolygonShape3D`).143- **A joint with only one body gets inverted limits after switching engines.** Jolt treats144 the lone body as `node_b` with `node_a` as the world; GodotPhysics3D always treats it as145 `node_a`. Assign the body to the slot Jolt expects, or use Physics > Jolt Physics 3D >146 Joints > World Node for compatibility with an older project.147- **Joint soft-limit properties do nothing** under Jolt: `bias`/`damping`/`impulse_clamp` on148 `PinJoint3D`; `bias`/`softness`/`relaxation` on `HingeJoint3D` and `ConeTwistJoint3D`;149 `angular_*` and limit softness/restitution/damping on `SliderJoint3D`; limit150 softness/restitution/damping/erp on `Generic6DOFJoint3D`. Godot warns when they are set151 to non-default values.152- **A frozen kinematic `RigidBody3D` reports no contacts** with static or kinematic bodies,153 even with `max_contacts_reported > 0`. Enable Physics > Jolt Physics 3D > Simulation >154 Generate All Kinematic Contacts if gameplay depends on those contacts.155- **Shape `margin` shrinks the shape instead of padding it** under Jolt, so sizes stay156 true but shape queries can return odd normals on small shapes; the effective margin comes157 from Physics > Jolt Physics 3D > Collisions > Collision Margin Fraction.158- **`Area3D` now fires `body_entered` for `SoftBody3D`** under Jolt (GodotPhysics3D never159 did). Filter soft bodies out with layers/masks if the old behavior is expected.160- **A `WorldBoundaryShape3D` floor sits at the wrong height after upgrading to 4.7** → under161 Jolt, 4.7 reads `plane.d` with Godot's sign convention, the opposite of 4.6; flip the sign162 of `d` in projects tuned on 4.6. Jolt also caps the plane's effective size (Physics > Jolt163 Physics 3D > Limits > World Boundary Shape Size), so bodies far from the origin can fall164 off an "infinite" floor.165166## References167168- For `_integrate_forces`, joints, one-way collision, `PhysicsServer` direct access,169 shape queries (`intersect_shape`), and 3D `move_and_collide`, read170 `references/bodies-and-queries.md`.171- Jolt specifics: Godot docs "Using Jolt Physics"172 (`https://docs.godotengine.org/en/stable/tutorials/physics/using_jolt_physics.html`).173174## Related skills175176- `godot-2d-movement` — kinematic `CharacterBody2D` controllers.177- `godot-tilemap` — tile collision shapes and their layers.178- `physics-tuning` — engine-agnostic feel: timestep, mass, drag, CCD.179- `godot-3d-essentials` — 3D scene setup these bodies live in.