Documentation
¶
Overview ¶
Package aabbworld is a 2D world of axis-aligned boxes: a plane with its own edge rules, a spatial index over the boxes placed in it, and the questions a simulation asks of them — who is near whom, who sees what, what overlaps. Space is the whole of it; geom and plane are its vocabulary.
Index ¶
- type Capability
- type Cone
- type Config
- type Edges
- type Space
- func (s *Space) Bounds() (width, height uint32, edges Edges)
- func (s *Space) Flush(onDirty func(geom.AABB))
- func (s *Space) Insert(id uid.UID64, box *plane.AABB) bool
- func (s *Space) MoveTo(id uid.UID64, box *plane.AABB, to geom.Vec) bool
- func (s *Space) Query(box geom.AABB, want Capability, fn func(id uid.UID64)) int
- func (s *Space) Remove(id uid.UID64)
- func (s *Space) Scan(observer uid.UID64, cone Cone, v *View) bool
- func (s *Space) SetCapabilities(id uid.UID64, c Capability)
- func (s *Space) Translate(id uid.UID64, box *plane.AABB, delta geom.Vec) bool
- func (s *Space) WrapAABB(box geom.AABB) plane.AABB
- type View
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Capability ¶
type Capability uint8
Capability is what may be done with an entity, as a set of up to eight bits — a question names the bits it wants, and only entities sharing one are answered with.
const ( // Plain is an entity that is nothing but geometry — what one is until SetCapabilities. Plain Capability = 1 << 0 // CanCollide is an entity that takes part in collisions — what collide.BroadPhase asks for. CanCollide Capability = 1 << 1 // AnyCapability accepts every entity. AnyCapability Capability = 0xFF )
type Cone ¶
Cone bounds a visibility query: a direction, a half-angle either side of it, and how far it reaches.
type Config ¶
type Config struct {
// Width and Height of the world, in world units.
Width, Height uint32
// Edges is what happens to a box at the edges of the world; the zero value stops it whole.
Edges Edges
// BucketSize is the side of one index bucket in world units, rounded up to a power of two.
BucketSize uint32
// BucketCapacity is how many entities a bucket holds before it grows.
BucketCapacity int
// OpsBufferSize is how many queued changes fit between two Flushes.
OpsBufferSize int
}
Config sizes a Space.
type Edges ¶
type Edges uint8
Edges is what happens to a box at the edges of the world, per axis; zero stops it whole.
const ( // WrapX returns a box leaving by the left or right edge through the opposite one. WrapX Edges = 1 << iota // WrapY returns a box leaving by the top or bottom edge through the opposite one. WrapY // OpenX lets a box leave by the left or right edge; wholly outside, it is dropped from the index. OpenX // OpenY lets a box leave by the top or bottom edge; wholly outside, it is dropped from the index. OpenY // Torus wraps on both axes. Torus = WrapX | WrapY )
type Space ¶
type Space struct {
Config
// contains filtered or unexported fields
}
Space is a plane with boundary rules and a spatial index over the boxes placed in it. Changes are queued and take effect at the next Flush.
func (*Space) Flush ¶
Flush applies every queued change, reporting each changed box to onDirty if given.
func (*Space) Insert ¶
Insert places box under the edge rules and queues it for the index; false if past an open edge.
func (*Space) MoveTo ¶
MoveTo puts box with its top-left at to, under the edge rules; false once it has left that way.
func (*Space) Query ¶
Query calls fn for every indexed piece in box sharing want, seams included, and returns how many.
func (*Space) Scan ¶
Scan fills v with what observer sees through cone and reports whether it could.
func (*Space) SetCapabilities ¶
func (s *Space) SetCapabilities(id uid.UID64, c Capability)
SetCapabilities records what may be done with id, from the next Flush on.
func (*Space) Translate ¶
Translate moves box by delta under the edge rules; false once it has left by an open edge.
Example ¶
package main
import (
"fmt"
"github.com/kjkrol/aabbworld"
"github.com/kjkrol/aabbworld/geom"
"github.com/kjkrol/aabbworld/plane"
"github.com/kjkrol/uid"
)
func main() {
space, err := aabbworld.NewSpace(aabbworld.Config{
Width: 16, Height: 16, Edges: aabbworld.Torus, BucketSize: 4, BucketCapacity: 4,
})
if err != nil {
fmt.Println(err)
return
}
id := uid.UID64(1)
box := space.WrapAABB(geom.NewAABBAt(geom.NewVec(0, 0), 2, 2))
space.Insert(id, &box)
space.Translate(id, &box, geom.NewVec(-1, -1))
fmt.Printf("New position: %s\n", box)
box.VisitFragments(func(pos plane.FragPosition, piece geom.AABB) bool {
fmt.Printf("- Fragment %d: %s\n", pos, piece)
return true
})
}
Output: New position: {(15,15) (16,16)} - Fragment 1: {(0,15) (1,16)} - Fragment 2: {(15,0) (16,1)} - Fragment 3: {(0,0) (1,1)}
type View ¶
type View struct {
// contains filtered or unexported fields
}
View is one observer's line of sight: filled by Space.Scan, then read as many ways as needed. The zero value is ready; it must not be copied once scanned, and serves one goroutine.
Example ¶
space, err := aabbworld.NewSpace(aabbworld.Config{
Width: 1000, Height: 1000,
BucketSize: 256, BucketCapacity: 8,
})
if err != nil {
panic(err)
}
const guard = uid.UID64(1)
space.Insert(guard, ptr(plane.NewAABB(geom.NewVec(100, 100), 10, 10)))
space.Insert(uid.UID64(2), ptr(plane.NewAABB(geom.NewVec(300, 100), 10, 10)))
space.Insert(uid.UID64(3), ptr(plane.NewAABB(geom.NewVec(500, 100), 10, 10)))
space.Flush(nil)
cone := aabbworld.Cone{
Direction: geom.NewVec(1.0, 0.0),
HalfAngle: math.Pi / 6,
Radius: 600,
}
var view aabbworld.View
var fog []geom.Vec
grew := 0
for tick := 1; tick <= 3; tick++ {
before := cap(fog)
if !space.Scan(guard, cone, &view) {
continue
}
view.Entities(func(id uid.UID64, dist float64) {
if tick == 1 {
fmt.Printf("sees %d at %.0f\n", id, dist)
}
})
fog = view.Outline(0, fog[:0])
if cap(fog) != before {
grew++
}
}
fmt.Printf("outline buffer grew %d time(s) over 3 ticks\n", grew)
Output: sees 2 at 195 outline buffer grew 1 time(s) over 3 ticks
func (*View) Depths ¶
Depths appends to dst the reach at k ≥ 2 angles evenly spaced across the cone.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package collide finds and separates overlapping boxes of a Space: BroadPhase names the pairs that may touch, a NarrowPhase gathers them as Pairs, and Separate tests each exactly and pushes the overlapping apart.
|
Package collide finds and separates overlapping boxes of a Space: BroadPhase names the pairs that may touch, a NarrowPhase gathers them as Pairs, and Separate tests each exactly and pushes the overlapping apart. |
|
Package geom holds the 2D vocabulary of aabbworld: Vec and the axis-aligned box AABB.
|
Package geom holds the 2D vocabulary of aabbworld: Vec and the axis-aligned box AABB. |
|
internal
|
|
|
core
Package core is the inside of a Space, shared by the packages of this module that work on one.
|
Package core is the inside of a Space, shared by the packages of this module that work on one. |
|
plane
Package plane is the surface behind a Space: normalisation, translation, growth and distance on a bounded plane and on a torus, working on the public plane's boxes.
|
Package plane is the surface behind a Space: normalisation, translation, growth and distance on a bounded plane and on a torus, working on the public plane's boxes. |
|
raycast
Package raycast is the state and the algorithm behind the public raycast package: the sweep, the shadows, and the View they fill.
|
Package raycast is the state and the algorithm behind the public raycast package: the sweep, the shadows, and the View they fill. |
|
spatial
Package spatial provides a discrete spatial index over a 2D power-of-two grid for storing and querying objects by integer coordinates, with support for range queries (AABB) and bulk operations.
|
Package spatial provides a discrete spatial index over a 2D power-of-two grid for storing and querying objects by integer coordinates, with support for range queries (AABB) and bulk operations. |
|
Package plane defines 2D spaces (cartesian and torus) plus plane-aware boxes and metrics.
|
Package plane defines 2D spaces (cartesian and torus) plus plane-aware boxes and metrics. |
