Godot raycasting queries

From thedivergentai/GD-Agentic-Skills · View interactive version

Godot raycasting queries is an AI agent skill from the GitHub repository thedivergentai/GD-Agentic-Skills. Expert blueprint for physics queries using RayCast, ShapeCast, and DirectSpaceState. Covers hit detection, volume overlap, mouse picking, and high-performance server-side intersection queries. Use when implementing projectiles, LOS, terrain grounding, or AI sensors. Keywords raycast, shapecast, dire

How to use this skill

Skill file

Available Scripts

MANDATORY for common paths — read before implementing (do not improvise query APIs from memory):

- direct_space_state_raycast.gd — high-frequency intersect_ray without RayCast nodes.

- query_exclusion_optimization.gd — RID exclude lists so casters never self-hit.

- shapecast_ground_detection.gd — footing / volume casts when thin rays tunnel or miss.

Do NOT Load every script below for one task. Open only the row that matches the decision table.

[direct_space_state_raycast.gd](scripts/direct_space_state_raycast.gd)

Expert usage of PhysicsDirectSpaceState2D/3D for bypassing node-based overhead in high-frequency queries.

[shapecast_ground_detection.gd](scripts/shapecast_ground_detection.gd)

Reliable ground/footing detection using volume-based ShapeCast instead of thin rays.

[multiple_hit_piercing_ray.gd](scripts/multiple_hit_piercing_ray.gd)

Implementing piercing projectiles that detect and return multiple hits in a single line.

[field_of_view_scanner.gd](scripts/field_of_view_scanner.gd)

AI sensor logic using a fan of raycasts to detect targets within a FOV cone.

[raycast_reflection_logic.gd](scripts/raycast_reflection_logic.gd)

Calculating bounces for lasers or bullets using collision normal reflection.

[point_in_shape_query.gd](scripts/point_in_shape_query.gd)

Checking for overlapping physics bodies at a single point (Explosion epicenters).

[rest_info_3d_stuck_fix.gd](scripts/rest_info_3d_stuck_fix.gd)

Using get_rest_info to detect stuck objects and resolve overlaps immediately.

[mouse_pick_3d_query.gd](scripts/mouse_pick_3d_query.gd)

Converting 2D screen coordinates to 3D world rays for point-and-click interaction.

[water_buoyancy_surface_calc.gd](scripts/water_buoyancy_surface_calc.gd)

Finding water surface height for buoyancy systems using high-to-low raycasting.

[query_exclusion_optimization.gd](scripts/query_exclusion_optimization.gd)

Optimizing performance by excluding specific RIDs (Resource IDs) from intersection checks.

NEVER Do in Physics Queries

Query-Type Decision Table

Pick the cheapest API that answers the question. Always pair rays/shapes with RID exclude + masks (query_exclusion_optimization.gd).

| Need | Prefer | Cost | When | Script |

|------|--------|------|------|--------|

| Persistent sensor in the scene (ledge, aim assist debug) | RayCast2D/RayCast3D node | Low–med | Few casts; OK waiting one physics frame (or force_raycast_update()) | Scene node + NEVER rules |

| Hitscan / LOS / one-shot mid-frame ray | PhysicsDirectSpaceState*.intersect_ray | Low | High frequency, no permanent node | MANDATORY direct_space_state_raycast.gd |

| Footing, thick walls, melee volume | ShapeCast* / intersect_shape | Med–high | Thin ray tunnels or misses volume | MANDATORY shapecast_ground_detection.gd |

| Explosion / occupancy at a point | intersect_point | Low–med | Epicenter overlap list | point_in_shape_query.gd |

| Stuck / penetration resolve | get_rest_info | Med | Overlap recovery | rest_info_3d_stuck_fix.gd |

| Pierce / multi-hit along a line | Repeated intersect_ray + exclude RIDs | Med | Projectiles that keep going | multiple_hit_piercing_ray.gd |

| Screen → world click | Camera project + intersect_ray | Low | Picking | mouse_pick_3d_query.gd |


3D Mouse Picking Example

func screen_point_to_ray():
    var space_state = get_world_3d().direct_space_state
    var mouse_pos = get_viewport().get_mouse_position()
    
    var origin = project_ray_origin(mouse_pos)
    var end = origin + project_ray_normal(mouse_pos) * 2000
    
    var query = PhysicsRayQueryParameters3D.create(origin, end)
    var result = space_state.intersect_ray(query)
    
    if result:
        return result.collider
    return null

Expert WHY (query timing & LOS)

Deep dive (load on demand)

NavMesh LOS validator, surface metadata, picking baseline, tunneling notes — references/query-elite-patterns.md.

Reference

Progressive disclosure: open Official Documentation links only when researching a specific API;

load Related Skills when routing work to a peer domain — do not preload the whole lattice.

Official Documentation

Related Skills

Prerequisites
Complements
Downstream / consumers
Master
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.