perception3d

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package perception3d is ai's perception for agents in a 3D world: sight cones around a forward vector, hearing and per-stimulus memory on math3d.Vec3, with line of sight through physics3d raycasts or a game's own check.

It shares ai's engine, so its sight, hearing, memory, events, teams, line-of-sight budget and determinism behave exactly as ai.PerceptionSystem's do, and it uses ai's StimulusID, Team, SightConfig, HearingConfig and event types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Kind is what happened.
	Kind ai.EventKind
	// Memory is the memory as the event left it; for ai.EventForgotten it is the memory that was dropped.
	Memory Memory
}

Event reports a change to one memory during the last step.

type LineOfSight

type LineOfSight func(from, to math3d.Vec3) bool

LineOfSight reports whether nothing blocks the segment from an agent's eye at from to a target at to. It must be deterministic for perception to be.

func PhysicsLineOfSight

func PhysicsLineOfSight(w *physics3d.World, layers physics3d.LayerMask) LineOfSight

PhysicsLineOfSight returns a LineOfSight that is blocked by the first hit of w.RaycastFirstLayer with layers; zero selects every layer.

A ray starting inside a body hits it, and a ray to a target hits the target's own body, so keep agents' and targets' bodies off the layers that block sight.

type Memory

type Memory struct {
	// ID is the stimulus's ID.
	ID ai.StimulusID
	// Team is the stimulus's team when it was last sensed.
	Team ai.Team
	// Attitude is how the agent regarded the stimulus when it was last sensed.
	Attitude ai.Attitude
	// Sense is the sense that last refreshed the memory.
	Sense ai.Sense
	// Pos is the last known position.
	Pos math3d.Vec3
	// Vel is the last known velocity: the target's velocity when seen, zero when heard.
	Vel math3d.Vec3
	// Age is the time since the stimulus was last sensed; zero while it is in sight.
	Age time.Duration
	// Strength is 1 when seen, and hearing raises it to at least 1 - distance/reach (1 for an infinite reach); it falls linearly to 0 over ForgetAfter.
	Strength float32
	// Visible reports whether the stimulus is in sight now.
	Visible bool
}

Memory is what an agent remembers about one stimulus; its fields mean what ai.Memory's do.

type Noise

type Noise struct {
	// Source identifies what made the noise; the memory of it is kept under this ID.
	Source ai.StimulusID
	// Team is the source's team.
	Team ai.Team
	// Pos is where the noise was made.
	Pos math3d.Vec3
	// Loudness scales each listener's hearing range; zero or less is inaudible.
	Loudness float32
}

Noise is a sound posted to a System and heard on its next Step.

type Perception

type Perception struct {
	// Self is the agent's own StimulusID, so the agent never senses itself; zero means the agent is not a stimulus, so an agent that is one needs a non-zero ID.
	Self ai.StimulusID
	// Team is the agent's team.
	Team ai.Team
	// Pos is the agent's position.
	Pos math3d.Vec3
	// Forward is the direction the sight cone points along; its length does not matter, and zero faces -Z.
	Forward math3d.Vec3
	// EyeOffset is added to Pos, in world space, to give the point both senses measure from and sight looks from, such as head height on +Y.
	EyeOffset math3d.Vec3
	// Sight is the agent's vision; its angles are measured in 3D around Forward.
	Sight ai.SightConfig
	// Hearing is the agent's hearing.
	Hearing ai.HearingConfig
	// ForgetAfter is how long a stimulus is remembered after it was last sensed; zero or less forgets it on the step after.
	ForgetAfter time.Duration
	// contains filtered or unexported fields
}

Perception is one 3D agent's senses and memory. Set its exported fields, add it to a System, and step the system on game.Loop's fixed steps; the system reads the fields at the start of every Step.

func (*Perception) AppendMemories

func (p *Perception) AppendMemories(dst []Memory) []Memory

AppendMemories appends every memory, sorted by ID, to dst and returns the extended slice.

func (*Perception) Clear

func (p *Perception) Clear()

Clear drops every memory and the last step's events.

func (*Perception) Events

func (p *Perception) Events() []Event

Events returns the events of the last Step; the slice is reused by the next Step.

func (*Perception) Forget

func (p *Perception) Forget(id ai.StimulusID) bool

Forget drops the memory of id without an event and reports whether there was one.

func (*Perception) Memories

func (p *Perception) Memories() []Memory

Memories returns a new slice of every memory, sorted by ID.

func (*Perception) Memory

func (p *Perception) Memory(id ai.StimulusID) (Memory, bool)

Memory returns the memory of the stimulus id, if any.

func (*Perception) Sees

func (p *Perception) Sees(id ai.StimulusID) bool

Sees reports whether the stimulus id is in sight now.

type System

type System struct {
	// LineOfSight decides whether a target in view is seen; nil means nothing blocks sight.
	LineOfSight LineOfSight
	// Attitude decides how agents regard targets and noises; nil means ai.DefaultAttitude.
	Attitude ai.AttitudeFunc
	// Budget caps the LineOfSight calls of one step; zero or less checks every pair in view, and a nil LineOfSight makes no calls, so the budget does not apply and every pair in view is seen at once. When the cap is reached, the remaining pairs are checked first on the next step, round-robin, and keep their previous result until then.
	Budget int
	// contains filtered or unexported fields
}

System steps the Perceptions of many 3D agents together, so their line-of-sight checks share one per-step Budget, as ai.PerceptionSystem does in 2D. The zero System is ready to use, with every line of sight clear.

Example

A guard looking down -Z sees an intruder in the open but not one behind a wall, with the wall's body on a layer of its own.

package main

import (
	"fmt"
	"math"
	"time"

	"github.com/nsgo-dev/game/pkg/ai"
	"github.com/nsgo-dev/game/pkg/ai/perception3d"
	"github.com/nsgo-dev/game/pkg/math3d"
	"github.com/nsgo-dev/game/pkg/physics3d"
)

func main() {
	const walls physics3d.LayerMask = 1 << 1
	world := physics3d.NewWorld()
	wall := physics3d.NewStaticBody(physics3d.BoxShape(physics3d.Box{HalfExtents: physics3d.Vec3{X: 2, Y: 3, Z: 0.5}}), physics3d.Vec3{Z: -10})
	wall.Layer = walls
	world.AddBody(wall)

	guard := &perception3d.Perception{
		Team:        1,
		EyeOffset:   math3d.Vec3{Y: 1.7},
		Sight:       ai.SightConfig{Range: 40, HalfAngle: math.Pi / 4},
		ForgetAfter: 3 * time.Second,
	}
	sys := perception3d.System{LineOfSight: perception3d.PhysicsLineOfSight(world, walls)}
	sys.Add(guard)
	sys.Step(time.Second/60, []perception3d.Target{
		{ID: 1, Team: 2, Pos: math3d.Vec3{Y: 1.7, Z: -20}},
		{ID: 2, Team: 2, Pos: math3d.Vec3{X: 8, Y: 1.7, Z: -20}},
	})
	fmt.Println(guard.Sees(1), guard.Sees(2))
}
Output:
false true

func (*System) Add

func (s *System) Add(p *Perception)

Add adds p to the system, after the agents already in it. Adding p again does nothing; adding a Perception that belongs to another system panics. A copy of a Perception that was added, such as a configured prototype, is a separate agent: Add gives it its own copy of the memories and no events.

func (*System) Len

func (s *System) Len() int

Len returns the number of agents in the system.

func (*System) PostNoise

func (s *System) PostNoise(n Noise)

PostNoise queues n to be heard on the next Step. Noises are heard in the order they were posted.

func (*System) Remove

func (s *System) Remove(p *Perception) bool

Remove removes p from the system and reports whether it was there. p keeps its memories, but none of them is visible any more and Events is empty, so Sees reports false until p is added and stepped again.

func (*System) Stats

func (s *System) Stats() ai.PerceptionStats

Stats returns the line-of-sight work of the last Step.

func (*System) Step

func (s *System) Step(dt time.Duration, targets []Target)

Step advances every agent by dt: memories age, targets in view are checked for line of sight within the budget, posted noises are heard, and memories not sensed for ForgetAfter are forgotten. targets is read only during the call.

type Target

type Target struct {
	// ID identifies the target; a later target with the same ID in one step is ignored.
	ID ai.StimulusID
	// Team is the target's team.
	Team ai.Team
	// Pos is the point agents look at.
	Pos math3d.Vec3
	// Vel is the target's velocity, remembered while it is seen.
	Vel math3d.Vec3
}

Target is a stimulus that agents can see during one step.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL