boids

package
v0.0.0-...-32a13a6 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Boid

type Boid struct {
	ID  int
	Pos Vector
	Vel Vector
}

Boid represents a single boid. It will try to fit in with a Swarm by: - moving towards the center of nearby Boids (Cohesion). - matching with nearby Boids' velocity (Alignment). - avoiding collisions with nearby Boids (Separation).

It can optionally move towards a target.

type Conf

type Conf struct {
	Spawn       [2]Vector // Bounding box of min/max vector where boids spawn.
	Seed        int64     // Randomisation seed.
	Boids       int       // Number of boids to spawn.
	Workers     int       // Number of goroutines that runs boid calculations.
	IndexOffset int       // Size (in pixels) of each "cell" in the spatial index used to group boids.

	// Variables used for boid movement calculation.
	CohesionFactor      float64
	AlignmentFactor     float64
	SeparationRange     float64
	SeparationFactor    float64
	TargetRange         float64
	TargetRepelFactor   float64
	TargetAttractFactor float64
	VelocityMax         float64
	VelocityMin         float64
}

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index groups Boids into neighbouring bins.

func NewIndex

func NewIndex(offset int) *Index

func (*Index) IterBounds

func (i *Index) IterBounds(min, max Vector, fun func(int))

func (*Index) IterNeighbours

func (i *Index) IterNeighbours(b *Boid, fun func(n int))

IterNeighbours iterates over all Boids in the same bin and the 8 neighbouring bins.

func (*Index) Key

func (i *Index) Key(b *Boid) IndexKey

Key returns the key for the neighbouring bin a Boid is part of.

func (*Index) Update

func (i *Index) Update(boids []*Boid)

Update clears the index and reinserts all Boids into new neighbouring bins.

type IndexBin

type IndexBin []int

IndexBin contains IDs for Boids in the same bin.

type IndexKey

type IndexKey [2]int

type Swarm

type Swarm struct {
	Conf  Conf
	Boids []*Boid
	Index *Index
	// contains filtered or unexported fields
}

Swarm is a group of Boids. It is moving together most of the time.

func New

func New(conf Conf) *Swarm

New creates a new swarm of Boids, using Conf. It randomises the positions of each Boid and fires up a group of background workers to perform the actual Boid movement updates.

func (*Swarm) Update

func (s *Swarm) Update(dirty bool, target Vector)

Update all Boids' velocity (dirty, slow) or position (non-dirty, fast). It also updates the Boid neighbour index if dirty, before hand.

type Vector

type Vector struct {
	X, Y float64
}

Vector represents a 2D vector.

func NewVector

func NewVector(x, y float64) Vector

func (Vector) Add

func (v Vector) Add(f float64) Vector

func (Vector) Addv

func (v Vector) Addv(other Vector) Vector

func (Vector) Angle

func (v Vector) Angle() float64

Calculates the vector angle and returns radians. To get degrees: multiply radians with 180/Pi

func (Vector) Div

func (v Vector) Div(f float64) Vector

func (Vector) Divv

func (v Vector) Divv(other Vector) Vector

func (Vector) Dot

func (v Vector) Dot(other Vector) float64

Calculates the dot product of two vectors.

func (Vector) InRange

func (v Vector) InRange(r float64) float64

Checks if vector length is within a target range. WARNING: target range r should be squared (r^2) by the caller! This odd behaviour is required for cutting down on the amount of square/square-roots needed within this function and hence provides some nice performance boost.

func (Vector) Length

func (v Vector) Length() float64

Calculates the vector length/magnitude. It's an expensive call so use with care!

func (Vector) Mul

func (v Vector) Mul(f float64) Vector

func (Vector) Mulv

func (v Vector) Mulv(other Vector) Vector

func (Vector) Round

func (v Vector) Round() Vector

func (Vector) String

func (v Vector) String() string

func (Vector) Sub

func (v Vector) Sub(f float64) Vector

func (Vector) Subv

func (v Vector) Subv(other Vector) Vector

func (Vector) Within

func (v Vector) Within(min, max Vector) bool

Checks if the current vector is within a bounding box.

Jump to

Keyboard shortcuts

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