shape2d

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const InvalidCompactTreeItemID = CompactTreeItemID(0xFFFFFFFF)

InvalidCompactTreeItemID is an identifier that can be used by user code to mark an identifier as invalid. However, such an identifier will never be returned by the library and must also never be passed to the library.

View Source
const InvalidObjectID = ObjectID(invalidObjectIndex)

InvalidObjectID indicates an object that can never be part of the scene.

View Source
const InvalidShapeID = ShapeID(invalidShapeRef)

InvalidShapeID indicates a shape that can never be part of the scene.

Variables

This section is empty.

Functions

func CheckCircleCircleIntersection

func CheckCircleCircleIntersection(source, target Circle, yield IntersectionYieldFunc)

CheckCircleCircleIntersection checks if a Circle shape intersects with another Circle shape.

func CheckCircleEdgeIntersection

func CheckCircleEdgeIntersection(circle Circle, edge Edge, yield IntersectionYieldFunc)

CheckCircleEdgeIntersection checks for intersection between a circle and an edge. The orientation of the edge matters.

func CheckCirclePolygonIntersection

func CheckCirclePolygonIntersection(circle Circle, polygon Polygon, yield IntersectionYieldFunc)

CheckCirclePolygonIntersection checks for intersection between a circle and a polygon.

func CheckCircleRectangleIntersection

func CheckCircleRectangleIntersection(circle Circle, rectangle Rectangle, yield IntersectionYieldFunc)

CheckCircleRectangleIntersection checks if a circle shape intersects with a rectangle shape.

func CheckRectanglePolygonIntersection

func CheckRectanglePolygonIntersection(rectangle Rectangle, polygon Polygon, yield IntersectionYieldFunc)

CheckRectanglePolygonIntersection checks if a Rectangle shape intersects with a Polygon shape.

func CheckSegmentCircleIntersection

func CheckSegmentCircleIntersection(segment Segment, circle Circle, yield IntersectionYieldFunc)

CheckSegmentCircleIntersection checks if the specified segment intersects the specified circle and returns the intersection point.

This implementation assumes that the circle has backface culling. Hence, a segment starting from inside the circle will not produce an intersection. For such cases you can flip the segment to get the intersection going out.

func CheckSegmentEdgeIntersection

func CheckSegmentEdgeIntersection(segment Segment, edge Edge, yield IntersectionYieldFunc)

CheckSegmentEdgeIntersection checks if a segment intersects a polygon edge.

func CheckSegmentPolygonIntersection

func CheckSegmentPolygonIntersection(segment Segment, polygon Polygon, yield IntersectionYieldFunc)

CheckSegmentPolygonIntersection checks if a segment intersects a polygon.

func CheckSegmentRectangleIntersection

func CheckSegmentRectangleIntersection(segment Segment, box Rectangle, yield IntersectionYieldFunc)

CheckSegmentRectangleIntersection checks if the specified segment intersects the specified rectangle and returns the intersection information.

This implementation uses the slab method adapted for oriented rectangles. It returns the closest intersection point along the segment.

func IsCircleCircleIntersection

func IsCircleCircleIntersection(source, target Circle) bool

IsCircleCircleIntersection checks if the source circle intersects with the target circle.

Only a bool result is returned and no collision points or separation normals are evaluated.

func IsSegmentCircleOverlap

func IsSegmentCircleOverlap(segment Segment, circle Circle) bool

IsSegmentCircleOverlap checks if the specified segment overlaps in any way the specified circle, including being contained by the circle.

Types

type Circle

type Circle struct {

	// Position specifies the position of the circle.
	Position dprec.Vec2

	// Radius specifies the radius of the circle.
	Radius float64
}

Circle represents a 2D circle.

func NewCircle

func NewCircle(position dprec.Vec2, radius float64) Circle

NewCircle returns a circle from the specified position and radius.

func TransformedCircle

func TransformedCircle(source Circle, transform Transform) Circle

TransformedCircle creates a new circle based off of an existing one by applying the specified transformation.

type CircleInfo

type CircleInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Circle contains the circle information.
	Circle Circle
}

CircleInfo contains the information needed to create a circle shape.

type CompactQuad

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

CompactQuad represents a square area used for inserting items into the tree.

func NewCompactQuad

func NewCompactQuad(x, y, size float64) CompactQuad

NewCompactQuad creates a new CompactQuad instance from the specified position and size.

func NewCompactQuadFromCircle

func NewCompactQuadFromCircle(circle Circle) CompactQuad

NewCompactQuadFromCircle creates a new CompactQuad that wraps a circle.

type CompactQueryAABB

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

CompactQueryAABB represents an axis-aligned bounding box used for querying the tree.

func NewCompactQueryAABB

func NewCompactQueryAABB(minX, maxX, minY, maxY float64) CompactQueryAABB

NewCompactQueryAABB creates a new CompactQueryAABB instance from the specified bounds.

func NewCompactQueryAABBFromCircle

func NewCompactQueryAABBFromCircle(circle Circle) CompactQueryAABB

NewCompactQueryAABBFromCircle creates a new CompactQueryAABB that wraps a circle.

type CompactQuerySegment

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

CompactQuerySegment represents a line segment used for querying the tree.

func NewCompactQuerySegment

func NewCompactQuerySegment(a, b dprec.Vec2) CompactQuerySegment

NewCompactQuerySegment creates a new CompactQuerySegment instance from the specified endpoints.

type CompactTree

type CompactTree[T any] struct {
	// contains filtered or unexported fields
}

CompactTree is a spatial structure that uses a loose quadtree implementation with shrinking bounding box to enable the fast searching of items.

func NewCompactTree

func NewCompactTree[T any](settings CompactTreeSettings) *CompactTree[T]

NewCompactTree creates a new CompactTree using the provided settings.

func (*CompactTree[T]) GC

func (t *CompactTree[T]) GC()

GC runs cleanup and optimization logic. You should call this at least once per frame.

func (*CompactTree[T]) Insert

func (t *CompactTree[T]) Insert(quad CompactQuad, value T) CompactTreeItemID

Insert adds an item, which occupies the specified quad area, to this tree.

func (*CompactTree[T]) QueryAABB

func (t *CompactTree[T]) QueryAABB(queryBox CompactQueryAABB, yield VisitorFunc[T])

QueryAABB finds all items that are inside or intersect the specified axis-aligned bounding box. Each found item is passed to the specified yield function. The order in which items are passed is undefined and might change between invocations.

func (*CompactTree[T]) QuerySegment

func (t *CompactTree[T]) QuerySegment(querySegment CompactQuerySegment, yield VisitorFunc[T])

QuerySegment finds all items that intersect the specified segment. Each found item is passed to the specified yield function. The order in which items are passed is undefined and might change between invocations.

func (*CompactTree[T]) Remove

func (t *CompactTree[T]) Remove(id CompactTreeItemID)

Remove removes the item with the specified id from this tree.

func (*CompactTree[T]) Stats

func (t *CompactTree[T]) Stats() CompactTreeStats

Stats returns statistics on the current state of this tree.

func (*CompactTree[T]) Update

func (t *CompactTree[T]) Update(id CompactTreeItemID, quad CompactQuad)

Update repositions the item with the specified id to the new area.

func (*CompactTree[T]) VisitStats

func (t *CompactTree[T]) VisitStats() CompactTreeVisitStats

VisitStats returns statistics information on the last executed search in this tree.

type CompactTreeItemID

type CompactTreeItemID uint32

CompactTreeItemID is an identifier used to control the placement of an item into a compact tree.

type CompactTreeSettings

type CompactTreeSettings struct {

	// Size specifies the dimension (side to side) of the tree node.
	//
	// If not specified, a default size of 4096 is used.
	//
	// Inserting an item outside these bounds has undefined behavior.
	Size opt.T[float64]

	// MaxDepth controls the maximum depth that the tree can reach.
	//
	// If not specified, a default max depth of 8 is used.
	MaxDepth opt.T[uint32]

	// InitialNodeCapacity is a hint as to the number of nodes that will be
	// needed to place all items. Usually one would find that number empirically.
	// This allows the tree to preallocate memory and avoid dynamic allocations.
	//
	// By default the initial capacity is 4096.
	InitialNodeCapacity opt.T[uint32]

	// InitialItemCapacity is a hint as to the likely upper bound of items that
	// will be inserted into the tree. This allows the tree to preallocate
	// memory and avoid dynamic allocations during insertion.
	//
	// By default the initial capacity is 1024.
	InitialItemCapacity opt.T[uint32]
}

CompactTreeSettings contains the settings for a CompactTree.

type CompactTreeStats

type CompactTreeStats struct {

	// NodeCount is the total number of nodes in the tree.
	NodeCount uint32

	// ItemCount is the total number of items in the tree.
	ItemCount uint32

	// ItemCountPerDepth contains the number of items at each depth level.
	ItemCountPerDepth []uint32
}

CompactTreeStats represents the current state of a CompactTree.

type CompactTreeVisitStats

type CompactTreeVisitStats struct {

	// NodeCountVisited is the number of nodes that were visited during the last
	// visit operation.
	NodeCountVisited uint32

	// NodeCountAccepted is the number of nodes that were determined relevant
	// during the last visit operation.
	NodeCountAccepted uint32

	// NodeCountRejected is the number of nodes that were determined irrelevant
	// during the last visit operation.
	NodeCountRejected uint32

	// ItemCountVisited is the number of items that were visited during the last
	// visit operation.
	ItemCountVisited uint32

	// ItemCountAccepted is the number of items that were determined relevant
	// during the last visit operation.
	ItemCountAccepted uint32

	// ItemCountRejected is the number of items that were determined irrelevant
	// during the last visit operation.
	ItemCountRejected uint32
}

CompactTreeVisitStats represents statistics on the last visit operation performed on a CompactTree.

type Edge

type Edge struct {

	// A is the starting point of the edge.
	A dprec.Vec2

	// B is the ending point of the edge.
	B dprec.Vec2
}

Edge represents an edge in 2D space.

func NewEdge

func NewEdge(a, b dprec.Vec2) Edge

NewEdge creates a new Edge from points a to b.

func TransformedEdge

func TransformedEdge(edge Edge, transform Transform) Edge

TransformedEdge returns a new Edge that is the result of applying the given transform to the provided edge.

func (Edge) BoundingCircle

func (e Edge) BoundingCircle() Circle

BoundingCircle returns a Circle that encompasses this segment.

func (Edge) Center

func (e Edge) Center() dprec.Vec2

Center returns the center point of the segment.

func (Edge) Length

func (e Edge) Length() float64

Length computes and returns the length of the edge.

func (Edge) Normal

func (e Edge) Normal() dprec.Vec2

Normal computes and returns the normal vector of the edge.

It assumes a counter-clockwise winding order.

type Filter

type Filter struct {

	// Mask is a bitmask used to filter shapes based on their assigned layers.
	Mask opt.T[uint32]

	// SkipDynamic indicates whether dynamic shapes should be excluded from the
	// results.
	SkipDynamic bool

	// SkipStatic indicates whether static shapes should be excluded from the
	// results.
	SkipStatic bool
}

Filter represents a set of criteria to filter 2D shapes in a scene.

type Intersection

type Intersection struct {

	// TargetContact contains the point on the target shape where the
	// intersection first occurred.
	TargetContact dprec.Vec2

	// TargetNormal contains the best direction in which the source shape
	// must be translated to avoid the intersection.
	TargetNormal dprec.Vec2

	// Depth returns the amount of penetration between the two shapes.
	Depth float64
}

Intersection represents the intersection of two shapes.

func (*Intersection) EvalSourceContact

func (i *Intersection) EvalSourceContact() dprec.Vec2

EvalSourceContact calculates the contact point on the source shape.

func (*Intersection) EvalSourceNormal

func (i *Intersection) EvalSourceNormal() dprec.Vec2

EvalSourceNormal calculates the normal on the source shape along which the target shape needs to be repositioned to resolve the intersection.

func (*Intersection) Flipped

func (i *Intersection) Flipped() Intersection

Flipped returns a flipped (source and target swapped) version of this intersection.

type IntersectionCollection

type IntersectionCollection interface {

	// AddIntersection adds an intersection to the collection.
	AddIntersection(intersection Intersection)
}

IntersectionCollection represents a data structure that can hold one or more intersections.

type IntersectionYieldFunc

type IntersectionYieldFunc func(intersection Intersection)

IntersectionYieldFunc is a function type that can be used to yield intersections as they are found.

type LargestIntersection

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

LargestIntersection is an implementation of IntersectionCollection that keeps track of the closest (largest depth) observed intersection.

func (*LargestIntersection) AddIntersection

func (i *LargestIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*LargestIntersection) Intersection

func (i *LargestIntersection) Intersection() (Intersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*LargestIntersection) Reset

func (i *LargestIntersection) Reset()

Reset clears any observed intersection.

type LargestObjectIntersection

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

LargestObjectIntersection is an implementation of ObjectIntersectionCollection that keeps track of the closest (largest depth) observed intersection.

func (*LargestObjectIntersection) AddIntersection

func (i *LargestObjectIntersection) AddIntersection(intersection ObjectIntersection)

AddIntersection tracks the specified intersection.

func (*LargestObjectIntersection) Intersection

func (i *LargestObjectIntersection) Intersection() (ObjectIntersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*LargestObjectIntersection) Reset

func (i *LargestObjectIntersection) Reset()

Reset clears any observed intersection.

type LastIntersection

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

LastIntersection is an implementation of IntersectionCollection that keeps track of the last observed intersection.

func (*LastIntersection) AddIntersection

func (i *LastIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*LastIntersection) Intersection

func (i *LastIntersection) Intersection() (Intersection, bool)

Intersection returns the last observed intersection and a flag whether there was actually any intersection observed.

func (*LastIntersection) Reset

func (i *LastIntersection) Reset()

Reset clears any observed intersection.

type ObjectID

type ObjectID uint32

ObjectID is a reference to an object in the scene.

type ObjectInfo

type ObjectInfo[O any] struct {

	// Position optionally specifies a position where the object should be
	// placed.
	//
	// Defaults to the origin.
	Position opt.T[dprec.Vec2]

	// Rotation optionally specifies a rotation of the object.
	//
	// Defaults to the identity rotation.
	Rotation opt.T[dprec.Angle]

	// Static marks the object as static. Static objects are not checked for
	// intersections with other static objects.
	Static bool

	// UserData allows one to attach custom user data to an object.
	UserData O
}

ObjectInfo contains the information needed to create an object in a scene.

type ObjectIntersection

type ObjectIntersection struct {

	// SourceObjectID contains the ID of the first involved object.
	//
	// This ID is equal to InvalidObjectID if the check was not performed with
	// a scene object.
	SourceObjectID ObjectID

	// SourceShapeID contains the ID of the shape from the first involved object.
	//
	// This ID is equal to InvalidShapeID if the check was not performed with
	// a scene object.
	SourceShapeID ShapeID

	// TargetObjectID contains the ID of the second involved object.
	//
	// This ID is equal to InvalidObjectID if the check was not performed with
	// a scene object.
	TargetObjectID ObjectID

	// TargetShapeID contains the ID of the shape from the second involved object.
	//
	// This ID is equal to InvalidShapeID if the check was not performed with
	// a scene object.
	TargetShapeID ShapeID

	// Intersection holds the underlying raw shape intersection.
	Intersection
}

ObjectIntersection represents the intersectio of two objects.

func (*ObjectIntersection) Flipped

Flipped returns a flipped (source and target swapped) version of this intersection.

type ObjectIntersectionBucket

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

ObjectIntersectionBucket is a container for object intersections.

func NewIntersectionBucket

func NewIntersectionBucket(initialCapacity int) *ObjectIntersectionBucket

NewIntersectionBucket creates a new IntersectionBucket instance with the specified initial capacity.

func (*ObjectIntersectionBucket) AddIntersection

func (b *ObjectIntersectionBucket) AddIntersection(intersection ObjectIntersection)

Add adds a new Intersection to this set.

func (*ObjectIntersectionBucket) Intersections

func (s *ObjectIntersectionBucket) Intersections() []ObjectIntersection

Intersections returns a slice of all intersections that have been observed.

NOTE: The slice must not be modified or cached as it will be reused.

func (*ObjectIntersectionBucket) IsEmpty

func (b *ObjectIntersectionBucket) IsEmpty() bool

IsEmpty returns whether no intersections were found.

func (*ObjectIntersectionBucket) Reset

func (b *ObjectIntersectionBucket) Reset()

Reset clears the buffer of this result set so that it can be reused.

type ObjectIntersectionCollection

type ObjectIntersectionCollection interface {

	// AddIntersection adds an intersection to the collection.
	AddIntersection(intersection ObjectIntersection)
}

ObjectIntersectionCollection represents a data structure that can hold one or more object intersections.

type Polygon

type Polygon struct {

	// Edges specifies the segments that make up the polygon.
	Edges []Edge
}

Polygon represents a 2D polygon defined by a series of edges. It is more similar to a 3D mesh than a mathematical polygon as it does not enforce any constraints on the edges and they can be disjoint.

func NewPolygon

func NewPolygon(edges []Edge) Polygon

NewPolygon creates a polygon from the specified edges.

NOTE: The polygon becomes the owner of the segment slice.

func TransformedPolygon

func TransformedPolygon(source Polygon, transform Transform) Polygon

TransformedPolygon creates a new polygon from the specified source polygon by applying the specified transformation.

func (*Polygon) BoundingCircle

func (p *Polygon) BoundingCircle() Circle

BoundingCircle returns a Circle that encompases this polygon.

type PolygonInfo

type PolygonInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Polygon contains the polygon information.
	Polygon Polygon
}

PolygonInfo contains the information needed to create a polygon shape.

type Rectangle

type Rectangle struct {

	// Position holds the position of the rectangle.
	Position dprec.Vec2

	// Rotation holds the rotation of the rectangle.
	Rotation dprec.Angle

	// HalfWidth holds the half-width of the rectangle.
	HalfWidth float64

	// HalfHeight holds the half-height of the rectangle.
	HalfHeight float64
}

Rectnagle represents a 2D rectangle.

func NewRectangle

func NewRectangle(pos dprec.Vec2, rotation dprec.Angle, size dprec.Vec2) Rectangle

NewRectangle returns a rectangle from the specified position, rotation and size.

The size parameter specifies the full width and height of the rectangle. Internally it will be converted to half sizes.

func TransformedRectangle

func TransformedRectangle(source Rectangle, transform Transform) Rectangle

TransformedRectangle creates a new rectangle from the specified source rectangle by applying the specified transformation.

func (*Rectangle) BoundingCircle

func (r *Rectangle) BoundingCircle() Circle

BoundingCircle returns the bounding circle of the rectangle.

func (*Rectangle) Vertices

func (r *Rectangle) Vertices() [4]dprec.Vec2

Vertices returns the four vertices of the rectangle in counter-clockwise order starting from the top-left vertex.

type RectangleInfo

type RectangleInfo[S any] struct {

	// ShapeInfo contains general shape information.
	ShapeInfo[S]

	// Rectangle contains the rectangle information.
	Rectangle Rectangle
}

RectangleInfo contains the information needed to create a rectangle shape.

type Scene

type Scene[T, S any] struct {
	// contains filtered or unexported fields
}

Scene represents a 2D scene where objects made of shapes can be added.

func NewScene

func NewScene[O, S any](settings SceneSettings) *Scene[O, S]

NewScene creates a new scene.

func (*Scene[O, S]) AttachCircle

func (s *Scene[O, S]) AttachCircle(objID ObjectID, info CircleInfo[S]) ShapeID

AttachCircle creates a circle shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) AttachPolygon

func (s *Scene[O, S]) AttachPolygon(objID ObjectID, info PolygonInfo[S]) ShapeID

AttachPolygon creates a polygon shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) AttachRectangle

func (s *Scene[O, S]) AttachRectangle(objID ObjectID, info RectangleInfo[S]) ShapeID

AttachRectangle creates a rectangle shape and attaches it to the object to be used for intersection tests.

func (*Scene[O, S]) CheckCircleIntersection

func (s *Scene[O, S]) CheckCircleIntersection(circle Circle, filter Filter) (ObjectIntersection, bool)

CheckCircleIntersection returns the first intersection of the circle with the scene.

func (*Scene[O, S]) CheckPolygonIntersection

func (s *Scene[O, S]) CheckPolygonIntersection(polygon Polygon, filter Filter) (ObjectIntersection, bool)

CheckPolygonIntersection returns the first intersection of the polygon with the scene.

func (*Scene[O, S]) CheckRectangleIntersection

func (s *Scene[O, S]) CheckRectangleIntersection(rectangle Rectangle, filter Filter) (ObjectIntersection, bool)

CheckRectangleIntersection returns the first intersection of the rectangle with the scene.

func (*Scene[O, S]) CheckSegmentIntersection

func (s *Scene[O, S]) CheckSegmentIntersection(segment Segment, filter Filter) (ObjectIntersection, bool)

CheckSegmentIntersection returns the first intersection of the segment with the scene.

func (*Scene[O, S]) CircleIter

func (s *Scene[O, S]) CircleIter(filter Filter) iter.Seq[Circle]

CircleIter returns an iterator over all circle shapes in the scene that match the given filter.

func (*Scene[O, S]) CollectCircleIntersections

func (s *Scene[O, S]) CollectCircleIntersections(circle Circle, filter Filter, collection ObjectIntersectionCollection)

CollectCircleIntersections collects all intersections of the circle with objects in the scene.

func (*Scene[O, S]) CollectIntersections

func (s *Scene[O, S]) CollectIntersections(collection ObjectIntersectionCollection)

CollectIntersections yields intersections found in this scene.

func (*Scene[O, S]) CollectPolygonIntersections

func (s *Scene[O, S]) CollectPolygonIntersections(polygon Polygon, filter Filter, collection ObjectIntersectionCollection)

CollectPolygonIntersections collects all intersections of the polygon with objects in the scene.

func (*Scene[O, S]) CollectRectangleIntersections

func (s *Scene[O, S]) CollectRectangleIntersections(rectangle Rectangle, filter Filter, collection ObjectIntersectionCollection)

CollectRectangleIntersections collects all intersections of the rectangle with objects in the scene.

func (*Scene[O, S]) CollectSegmentIntersections

func (s *Scene[O, S]) CollectSegmentIntersections(segment Segment, filter Filter, collection ObjectIntersectionCollection)

CollectSegmentIntersections collects all intersections of the segment with objects in the scene.

func (*Scene[O, S]) CreateObject

func (s *Scene[O, S]) CreateObject(info ObjectInfo[O]) ObjectID

CreateObject creates a new object.

func (*Scene[O, S]) DeleteObject

func (s *Scene[O, S]) DeleteObject(objID ObjectID)

DeleteObject deletes an object.

func (*Scene[O, S]) DeleteShape

func (s *Scene[O, S]) DeleteShape(shapeID ShapeID)

DeleteShape deletes a shape from an object. The object is not deleted and continues to exist in the scene.

func (*Scene[O, S]) EachCircle

func (s *Scene[O, S]) EachCircle(filter Filter, yield func(Circle) bool)

EachCircle iterates over all circle shapes in the scene that match the given filter and yields them to the provided callback.

func (*Scene[O, S]) EachPolygon

func (s *Scene[O, S]) EachPolygon(filter Filter, yield func(Polygon) bool)

EachPolygon iterates over all polygon shapes in the scene that match the given filter and yields them to the provided callback.

func (*Scene[O, S]) EachRectangle

func (s *Scene[O, S]) EachRectangle(filter Filter, yield func(Rectangle) bool)

EachRectangle iterates over all rectangle shapes in the scene that match the given filter and yields them to the provided callback.

func (*Scene[O, S]) GC

func (s *Scene[O, S]) GC()

GC cleans up internal data and allows for memory reuse. This should be called once per frame.

func (*Scene[O, S]) GetObjectTransform

func (s *Scene[O, S]) GetObjectTransform(objID ObjectID) Transform

GetObjectTransform returns the given object's transform.

func (*Scene[O, S]) GetObjectUserData

func (s *Scene[O, S]) GetObjectUserData(objID ObjectID) O

GetObjectUserData returns the user data associated with the given object.

func (*Scene[O, S]) GetShapeUserData

func (s *Scene[O, S]) GetShapeUserData(shapeID ShapeID) S

GetShapeUserData returns the user data associated with the given shape.

func (*Scene[O, S]) PolygonIter

func (s *Scene[O, S]) PolygonIter(filter Filter) iter.Seq[Polygon]

PolygonIter returns an iterator over all polygon shapes in the scene that match the given filter.

func (*Scene[O, S]) RectangleIter

func (s *Scene[O, S]) RectangleIter(filter Filter) iter.Seq[Rectangle]

RectangleIter returns an iterator over all rectangle shapes in the scene that match the given filter.

func (*Scene[O, S]) SetObjectTransform

func (s *Scene[O, S]) SetObjectTransform(objID ObjectID, transform Transform)

SetObjectTransform relocates the given object.

func (*Scene[O, S]) SetObjectUserData

func (s *Scene[O, S]) SetObjectUserData(objID ObjectID, userData O)

SetObjectUserData assigns the specified user data to the object.

func (*Scene[O, S]) SetShapeUserData

func (s *Scene[O, S]) SetShapeUserData(shapeID ShapeID, userData S)

SetShapeUserData assigns the specified user data to the shape.

type SceneSettings

type SceneSettings struct {

	// Size specifies the dimension (from side to side) of the scene.
	// Inserting an object outside these bounds has undefined behavior.
	//
	// If not specified, a default size of 4096 units is used.
	Size opt.T[float64]

	// MaxDepth specifies the maximum depth of the internal spatial
	// partitioning structure.
	//
	// If not specified, a default max depth of 8 is used.
	MaxDepth opt.T[uint32]

	// InitialNodeCapacity is a hint as to the number of nodes that will be
	// needed to place all items. Usually one would find that number empirically.
	// This allows the quadtree to preallocate memory and avoid dynamic allocations.
	//
	// By default the initial capacity is 4096.
	InitialNodeCapacity opt.T[uint32]

	// InitialItemCapacity is a hint as to the likely upper bound of items that
	// will be inserted into the quadtree. This allows the quadtree to preallocate
	// memory and avoid dynamic allocations during insertion.
	//
	// By default the initial capacity is 1024.
	InitialItemCapacity opt.T[uint32]
}

SceneSettings contains information needed to create an optimal scene.

type Segment

type Segment struct {

	// A is the start of the segment.
	A dprec.Vec2

	// B is the end of the segment.
	B dprec.Vec2
}

Segment represents a line segment with fixed start and end points.

func NewSegment

func NewSegment(a, b dprec.Vec2) Segment

NewSegment creates a new segment with the specified start and end points.

func TransformedSegment

func TransformedSegment(source Segment, transform Transform) Segment

TransformedSegment creates a new segment from the specified source segment by applying the specified transformation.

func (*Segment) BoundingCircle

func (s *Segment) BoundingCircle() Circle

BoundingCircle returns a Circle that encompasses this segment.

func (*Segment) Center

func (s *Segment) Center() dprec.Vec2

Center returns the center point of the segment.

func (*Segment) Length

func (s *Segment) Length() float64

Length returns the length of the segment.

type ShapeID

type ShapeID shapeRef

ShapeID is a reference to a shape in the scene.

type ShapeInfo

type ShapeInfo[S any] struct {

	// RejectGroup becomes active if a value larger than zero is specified.
	// Shapes that share the same reject group are not checked for intersection.
	RejectGroup uint32

	// SourceMask specifies the layers in which this shape is positioned.
	SourceMask opt.T[uint32]

	// TargetMask specifies the layers with which this shape can intersect.
	TargetMask opt.T[uint32]

	// UserData allows one to attach custom user data to a shape.
	UserData S
}

ShapeInfo contains information needed to create a new shape in the scene.

type SmallestIntersection

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

SmallestIntersection is an implementation of IntersectionCollection that keeps track of the farthest (smallest depth) observed intersection.

func (*SmallestIntersection) AddIntersection

func (i *SmallestIntersection) AddIntersection(intersection Intersection)

AddIntersection tracks the specified intersection.

func (*SmallestIntersection) Intersection

func (i *SmallestIntersection) Intersection() (Intersection, bool)

Intersection returns the best observed intersection and a flag whether there was actually any intersection observed.

func (*SmallestIntersection) Reset

func (i *SmallestIntersection) Reset()

Reset clears any observed intersection.

type SmallestObjectIntersection

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

SmallestObjectIntersection is an implementation of ObjectIntersectionCollection that keeps track of the farthest (smallest depth) observed intersection.

func (*SmallestObjectIntersection) AddIntersection

func (i *SmallestObjectIntersection) AddIntersection(intersection ObjectIntersection)

AddIntersection tracks the specified intersection.

func (*SmallestObjectIntersection) Intersection

func (i *SmallestObjectIntersection) Intersection() (ObjectIntersection, bool)

Intersection returns the worst observed intersection and a flag whether there was actually any intersection observed.

func (*SmallestObjectIntersection) Reset

func (i *SmallestObjectIntersection) Reset()

Reset clears any observed intersection.

type Transform

type Transform struct {

	// Translation specifies the translation that the transformation applies.
	Translation dprec.Vec2

	// Rotation specifies the rotation angle that the transformation applies.
	Rotation dprec.Angle
}

Transform represents a shape transformation.

func ChainedTransform

func ChainedTransform(parent, child Transform) Transform

ChainedTransform returns the Transform that is the result of combining two Transforms together.

func IdentityTransform

func IdentityTransform() Transform

IdentityTransform returns a new Transform that represents the origin.

func RotationTransform

func RotationTransform(rotation dprec.Angle) Transform

RotationTransform returns a new Transform that represents a rotation.

func TRTransform

func TRTransform(translation dprec.Vec2, rotation dprec.Angle) Transform

TRTransform returns a new Transform that represents both a translation and a rotation.

func TranslationTransform

func TranslationTransform(translation dprec.Vec2) Transform

TranslationTransform returns a new Transform that represents a translation.

func (*Transform) Apply

func (t *Transform) Apply(v dprec.Vec2) dprec.Vec2

Apply returns the transformation of the specified vector.

type Triangle

type Triangle struct {

	// A is the first vertex of the triangle.
	A dprec.Vec2

	// B is the second vertex of the triangle.
	B dprec.Vec2

	// C is the third vertex of the triangle.
	C dprec.Vec2
}

Triangle represents a triangle in 2D space defined by three vertices.

The ordering of the vertices is significant and should follow a counter-clockwise direction to ensure correct orientation.

func NewTriangle

func NewTriangle(a, b, c dprec.Vec2) Triangle

NewTriangle creates a new Triangle instance given three vertices a, b, and c.

func (Triangle) Centroid

func (t Triangle) Centroid() dprec.Vec2

Centroid computes and returns the centroid of the triangle.

func (Triangle) ContainsPoint

func (t Triangle) ContainsPoint(p dprec.Vec2) bool

ContainsPoint checks if the given point p lies within the triangle t.

func (Triangle) IsCCW

func (t Triangle) IsCCW() bool

IsCCW checks if the vertices of the triangle are ordered in a counter-clockwise manner.

func (Triangle) LengthAB

func (t Triangle) LengthAB() float64

LengthAB computes and returns the length of the edge between vertices A and B.

func (Triangle) LengthBC

func (t Triangle) LengthBC() float64

LengthBC computes and returns the length of the edge between vertices B and C.

func (Triangle) LengthCA

func (t Triangle) LengthCA() float64

LengthCA computes and returns the length of the edge between vertices C and A.

func (Triangle) SignedArea

func (t Triangle) SignedArea() float64

SignedArea computes and returns the signed area of the triangle. If the triangle is oriented counter-clockwise, the area is positive; otherwise, it is negative.

type VisitorBucket

type VisitorBucket[T any] struct {
	// contains filtered or unexported fields
}

VisitorBucket can be used to store items returned from a query.

func NewVisitorBucket

func NewVisitorBucket[T any](initCapacity int) *VisitorBucket[T]

NewVisitorBucket creates a new VisitorBucket instance with the specified initial capacity, which is only used to preallocate memory (it is allowed to exceed the initial capacity).

func (*VisitorBucket[T]) Add

func (r *VisitorBucket[T]) Add(item T) bool

Add records the passed item into the bucket.

func (*VisitorBucket[T]) Each

func (r *VisitorBucket[T]) Each(yield func(item T) bool)

Each calls the provided closure function for each stored item.

func (*VisitorBucket[T]) Items

func (r *VisitorBucket[T]) Items() []T

Items returns the underlying slice of stored items. The returned slice is valid only until the Reset function is called.

func (*VisitorBucket[T]) Reset

func (r *VisitorBucket[T]) Reset()

Reset clears any stored items.

func (*VisitorBucket[T]) VisitorFunc

func (r *VisitorBucket[T]) VisitorFunc() VisitorFunc[T]

VisitorFunc returns a VisitorFunc that can be passed to query functions.

Make sure to call Reset before reusing the bucket, unless you want to append to the existing items.

type VisitorFunc

type VisitorFunc[T any] func(item T) bool

VisitorFunc is a mechanism to receive items from a query.

Jump to

Keyboard shortcuts

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