Sensory Perception & Spatial Grids for AI

πŸ‘οΈ Sensory Perception & Spatial Awareness for Research AI

For an autonomous AI agent in a Godot sandbox to fight, evade, or navigate, it cannot rely on "god-mode" global engine variables. Giving an agent direct access to all enemy coordinates prevents realistic behaviors like flanking, ambushing, hiding behind walls, or losing visual contact.

Research AI uses Sensor Layers: Radial Raycasts, Field-of-View Cones, Acoustic Sound Events, and Spatial Influence Grids.


πŸ›οΈ The Multi-Modal Perception Hierarchy

+-------------------------------------------------------------+
| 1. Vision Cone & Raycasts (High Precision, Directional)     |
|    - 8 to 16 radial rays, line-of-sight checks, occlusion   |
+-------------------------------------------------------------+
                              β”‚
                              β–Ό
+-------------------------------------------------------------+
| 2. Acoustic Sense (Omnidirectional, Distance-Attenuated)    |
|    - Gunshots, footsteps, wall impacts trigger sound events |
+-------------------------------------------------------------+
                              β”‚
                              β–Ό
+-------------------------------------------------------------+
| 3. Spatial Memory & Decay (Where did I last see them?)      |
|    - Position memory decaying over time if not refreshed    |
+-------------------------------------------------------------+
                              β”‚
                              β–Ό
+-------------------------------------------------------------+
| 4. Influence Maps & Danger Grids (Tactical Spatial Heatmaps)|
|    - 2D/3D grid of territory control and incoming danger   |
+-------------------------------------------------------------+

πŸ“‘ 1. High-Speed Radial Raycasting in Godot 4

Instead of creating 16 individual RayCast3D scene nodes (which introduces node tree traversal overhead), query the PhysicsDirectSpaceState3D directly inside _physics_process() using batch ray queries:

# Fast batch raycasting without RayCast3D nodes
func get_raycast_sensor_readings(num_rays: int = 12, max_range: float = 20.0) -> Array[float]:
    var readings: Array[float] = []
    var space_state: PhysicsDirectSpaceState3D = get_world_3d().direct_space_state
    var step_angle: float = TAU / float(num_rays)

    for i in range(num_rays):
        var angle: float = step_angle * float(i)
        # Calculate ray direction relative to agent's local forward orientation
        var local_dir: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
        var world_dir: Vector3 = global_transform.basis * local_dir
        var ray_target: Vector3 = global_position + (world_dir * max_range)

        var query := PhysicsRayQueryParameters3D.create(global_position, ray_target)
        query.exclude = [get_rid()] # Exclude self
        
        var hit_result: Dictionary = space_state.intersect_ray(query)
        if hit_result.is_empty():
            readings.append(1.0) # Maximum distance (open path)
        else:
            var hit_distance: float = global_position.distance_to(hit_result.position)
            readings.append(hit_distance / max_range) # Normalized [0.0, 1.0]

    return readings

πŸ—ΊοΈ 2. Spatial Influence Maps (Tactical Heatmaps)

An Influence Map divides the arena into a 2D grid where each cell stores numeric tactical weights:

[ Safe Zone ]          [ Combat Heatmap ]          [ Danger Zone ]
 0.0  0.1  0.1           0.2  0.5  0.8               1.0  1.0  0.9
 0.1  0.0  0.2           0.3  [Agent A]              0.8  [Enemy B]
 0.0  0.1  0.4           0.6  0.9  1.0               0.7  0.9  0.8
  1. Friendly Influence (+): Propagates outward from player/ally positions with Gaussian distance falloff.
  2. Enemy Influence (-): Propagates outward from known enemy positions.
  3. Danger / Line-of-Fire Layer: High values on open sightlines; low values behind stone cover.

Using Influence Maps in Neural Network Observations

Downscale the influence grid into an 8Γ—8 matrix and feed it directly into the agent's neural network input tensor. This gives the AI instant intuition about territory control, flanking lanes, and escape routes.


🧠 3. Short-Term Spatial Memory (Handling Occlusion)

When an enemy runs behind a wall, a naive neural network instantly forgets they exist because the current raycast returns empty.

To give agents object permanence:

pestimated=plast_seen+vlast_seenΓ—Ξ”t