$npx -y skills add thedivergentai/GD-Agentic-Skills --skill godot-2d-physicsExpert patterns for Godot 2D physics including collision layers/masks, Area2D triggers, raycasting, and PhysicsDirectSpaceState2D queries. Use when implementing collision detection, trigger zones, line-of-sight systems, or manual physics queries. Trigger keywords: CollisionShape2
| 1 | # 2D Physics |
| 2 | |
| 3 | Expert guidance for collision detection, triggers, and raycasting in Godot 2D. |
| 4 | |
| 5 | ## NEVER Do |
| 6 | |
| 7 | - **NEVER scale `CollisionShape2D` nodes** — Use the shape handles in the editor, NOT the Node2D scale property. Scaling causes unpredictable physics behavior and incorrect collision normals [12]. |
| 8 | - **NEVER confuse `collision_layer` with `collision_mask`** — Layer = "What AM I?", Mask = "What do I DETECT?". Setting both to the same value is usually wrong [13]. |
| 9 | - **NEVER multiply velocity by delta when using `move_and_slide()`** — `move_and_slide()` automatically includes timestep. Only multiply gravity/acceleration by delta [14]. |
| 10 | - **NEVER forget `force_raycast_update()` for manual mid-frame raycasts** — Raycasts update once per physics frame. If you change target_position, you MUST force an update [15]. |
| 11 | - **NEVER use `get_overlapping_bodies()` every frame** — It is expensive. Cache results with `body_entered`/`body_exited` signals instead [16]. |
| 12 | - **NEVER modify `RigidBody2D` state directly in `_process`** — Use `_integrate_forces()` for safe, synchronized access to `PhysicsDirectBodyState2D` [17, 411]. |
| 13 | - **NEVER move `PhysicsBody2D` nodes in `_process()`** — Use `_physics_process()`. Moving bodies outside the physics step causes stutter and unreliable collision detection. |
| 14 | - **NEVER use `RigidBody2D` for 1000+ simple entities** — Use `PhysicsServer2D` to bypass node overhead for massive performance gains (Swarms/Bullets) [18, 397]. |
| 15 | - **NEVER use `Area2D` for high-frequency blocking (Bullets)** — Area signals can be delayed. Use `move_and_collide()` or `ShapeCast2D` for frame-perfect results [19]. |
| 16 | - **NEVER ignore 'Physics Jitter' on high-refresh monitors** — Enable Physics Interpolation to prevent micro-stutter in motion [21, 400]. |
| 17 | - **NEVER scale collision shapes directly at runtime** — It causes major instability. Resize the shape resource (size/radius) instead. |
| 18 | - **NEVER use `set_deferred` for immediate physics transform logic** — It happens at the end of the frame. Use `force_raycast_update()` or `PhysicsServer2D` instead. |
| 19 | - **NEVER leave Continuous CD (CCD) enabled for slow objects** — It adds significant CPU overhead. Reserve it for high-speed projectiles to prevent tunneling. |
| 20 | - **NEVER use a single collision layer for all tiles/entities** — Separate layers (Ground, Walls, Enemies) to allow selective filtering via masks. |
| 21 | - **NEVER forget to free `PhysicsServer2D` RIDs manually** — They are not garbage collected and will leak memory permanently. |
| 22 | |
| 23 | --- |
| 24 | |
| 25 | ## Godot 4.7: 2D Physics |
| 26 | |
| 27 | - `body_set_shape_as_one_way_collision` adds **direction** parameter — set relative to shape orientation for one-way platforms. |
| 28 | - `CollisionShape2D` supports one-way collision **direction relative to the shape** (not just global up). |
| 29 | |
| 30 | ## Available Scripts |
| 31 | |
| 32 | > **MANDATORY**: Read the script matching your use case before implementation. |
| 33 | |
| 34 | ### [collision_setup.gd](scripts/collision_setup.gd) |
| 35 | Programmatic layer/mask management with named layer constants and debug visualization. |
| 36 | |
| 37 | ### [physics_query_cache.gd](scripts/physics_query_cache.gd) |
| 38 | Frame-based caching for PhysicsDirectSpaceState2D queries - eliminates redundant expensive queries. |
| 39 | |
| 40 | ### [custom_physics.gd](scripts/custom_physics.gd) |
| 41 | Custom physics integration patterns for CharacterBody2D. Covers non-standard gravity, forces, and manual stepping. Use for non-standard physics behavior. |
| 42 | |
| 43 | ### [physics_queries.gd](scripts/physics_queries.gd) |
| 44 | PhysicsDirectSpaceState2D query patterns for raycasting, point queries, and shape queries. Use for line-of-sight, ground detection, or area scanning. |
| 45 | |
| 46 | ### [physics_server_swarm.gd](scripts/physics_server_swarm.gd) |
| 47 | Low-level `PhysicsServer2D` usage for thousands of moving objects. Bypasses node overhead for massive performance gains in bullet hells or swarms. |
| 48 | |
| 49 | ### [substepping_logic.gd](scripts/substepping_logic.gd) |
| 50 | Manual physics sub-stepping for high-velocity projectiles. Ensures frame-perfect collision for objects moving faster than the physics tick. |
| 51 | |
| 52 | ### [safe_rigidbody_state.gd](scripts/safe_rigidbody_state.gd) |
| 53 | Thread-safe `RigidBody2D` modification using `_integrate_forces`. Ideal for teleporting bodies or applying custom impulses without jitter. |
| 54 | |
| 55 | ### [physics_direct_query.gd](scripts/physics_direct_query.gd) |
| 56 | Lighweight environment sensing using `PhysicsDirectSpaceState2D`. Performs ray queries without the overhead of RayCast2D no |