Documentation ¶
Overview ¶
Package collider provides simple collision detection and resolution
Index ¶
- Variables
- type Cell
- type CellCoord
- type CircleShape
- func (ci *CircleShape) GetBounds() (float64, float64, float64, float64)
- func (ci *CircleShape) GetHash() *SpatialHash
- func (ci *CircleShape) GetParent() interface{}
- func (ci *CircleShape) GetPosition() *vector.Vector
- func (ci *CircleShape) Move(x, y float64)
- func (ci *CircleShape) MoveTo(x, y float64)
- func (ci *CircleShape) SetHash(s *SpatialHash)
- func (ci *CircleShape) SetParent(i interface{})
- type CollisionData
- type PointShape
- type RectangleShape
- func (re *RectangleShape) GetBounds() (float64, float64, float64, float64)
- func (re *RectangleShape) GetHash() *SpatialHash
- func (re *RectangleShape) GetParent() interface{}
- func (re *RectangleShape) GetPosition() *vector.Vector
- func (re *RectangleShape) Move(x, y float64)
- func (re *RectangleShape) MoveTo(x, y float64)
- func (re *RectangleShape) SetHash(s *SpatialHash)
- func (re *RectangleShape) SetParent(i interface{})
- type Shape
- type SpatialHash
- func (s *SpatialHash) Add(shape Shape)
- func (s *SpatialHash) CheckCollisions(shape Shape) []CollisionData
- func (s *SpatialHash) Draw(surface *ebiten.Image)
- func (s *SpatialHash) GetCollisionCandidates(shape Shape) []Shape
- func (s *SpatialHash) NewCircleShape(x, y, r float64) *CircleShape
- func (s *SpatialHash) NewPointShape(x, y float64) *PointShape
- func (s *SpatialHash) NewRectangleShape(x, y, w, h float64) *RectangleShape
- func (s *SpatialHash) Remove(shape Shape) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrShapeNotFound = errors.New("Couldn't remove shape from SpatialHash; not found")
)
Vars
Functions ¶
This section is empty.
Types ¶
type CellCoord ¶
type CellCoord struct {
X, Y int
}
CellCoord is a coordinate used to index the hash
type CircleShape ¶
type CircleShape struct { // Center point Pos *vector.Vector Radius float64 SpatialHash *SpatialHash Parent interface{} }
CircleShape shape
func (*CircleShape) GetBounds ¶
func (ci *CircleShape) GetBounds() (float64, float64, float64, float64)
GetBounds returns the Bounds of the CircleShape
func (*CircleShape) GetParent ¶
func (ci *CircleShape) GetParent() interface{}
GetParent gets the parent
func (*CircleShape) GetPosition ¶
func (ci *CircleShape) GetPosition() *vector.Vector
GetPosition returns the Point of the CircleShape
func (*CircleShape) Move ¶
func (ci *CircleShape) Move(x, y float64)
Move moves the CircleShape by x and y
func (*CircleShape) MoveTo ¶
func (ci *CircleShape) MoveTo(x, y float64)
MoveTo moves the CircleShape to x and y
func (*CircleShape) SetParent ¶
func (ci *CircleShape) SetParent(i interface{})
SetParent sets the parent
type CollisionData ¶
CollisionData contains information about the collision
type PointShape ¶
type PointShape struct{ *RectangleShape }
PointShape is a RectangleShape but with 0 width and height
type RectangleShape ¶
type RectangleShape struct { // Center point Pos *vector.Vector Width, Height float64 SpatialHash *SpatialHash Parent interface{} }
RectangleShape shape
func (*RectangleShape) GetBounds ¶
func (re *RectangleShape) GetBounds() (float64, float64, float64, float64)
GetBounds returns the Bounds of the RectangleShape
func (*RectangleShape) GetHash ¶
func (re *RectangleShape) GetHash() *SpatialHash
GetHash gets the hash
func (*RectangleShape) GetParent ¶
func (re *RectangleShape) GetParent() interface{}
GetParent gets the parent
func (*RectangleShape) GetPosition ¶
func (re *RectangleShape) GetPosition() *vector.Vector
GetPosition returns the Point of the RectangleShape
func (*RectangleShape) Move ¶
func (re *RectangleShape) Move(x, y float64)
Move moves the RectangleShape by x and y
func (*RectangleShape) MoveTo ¶
func (re *RectangleShape) MoveTo(x, y float64)
MoveTo moves the RectangleShape to x and y
func (*RectangleShape) SetHash ¶
func (re *RectangleShape) SetHash(s *SpatialHash)
SetHash sets the hash
func (*RectangleShape) SetParent ¶
func (re *RectangleShape) SetParent(i interface{})
SetParent sets the parent
type Shape ¶
type Shape interface { GetPosition() *vector.Vector // get the position GetBounds() (float64, float64, float64, float64) Move(x, y float64) // move by amount MoveTo(x, y float64) // move to position SetHash(s *SpatialHash) // sets ref to hash GetHash() *SpatialHash // gets ref to hash SetParent(i interface{}) GetParent() interface{} }
Shape interface. It's probably not needed but it keeps code more readable.
type SpatialHash ¶
type SpatialHash struct { // Size of the grid/cell/partition CellSize int // Store shapes in a cell depending on their bounds Hash map[CellCoord]*Cell // Backref for shapes to find its containing cells Backref map[Shape][]*Cell }
SpatialHash contains cells
func NewSpatialHash ¶
func NewSpatialHash(cellSize int) *SpatialHash
NewSpatialHash returns a new *SpatialHash
func (*SpatialHash) Add ¶
func (s *SpatialHash) Add(shape Shape)
Add adds a shape to the spatial hash
func (*SpatialHash) CheckCollisions ¶
func (s *SpatialHash) CheckCollisions(shape Shape) []CollisionData
CheckCollisions returns a list of all shapes and their separating vector.vector
func (*SpatialHash) Draw ¶
func (s *SpatialHash) Draw(surface *ebiten.Image)
Draw is a debug function. It draws a rectangle for every cell which has had a shape in it at some point.
func (*SpatialHash) GetCollisionCandidates ¶
func (s *SpatialHash) GetCollisionCandidates(shape Shape) []Shape
GetCollisionCandidates returns a list of all shapes in the same cells as shape
func (*SpatialHash) NewCircleShape ¶
func (s *SpatialHash) NewCircleShape(x, y, r float64) *CircleShape
NewCircleShape creates, then adds a new CircleShape to the hash before returning it
func (*SpatialHash) NewPointShape ¶
func (s *SpatialHash) NewPointShape(x, y float64) *PointShape
NewPointShape creates, then adds a new RectangleShape to the hash before returning it
func (*SpatialHash) NewRectangleShape ¶
func (s *SpatialHash) NewRectangleShape(x, y, w, h float64) *RectangleShape
NewRectangleShape creates, then adds a new RectangleShape to the hash before returning it
func (*SpatialHash) Remove ¶
func (s *SpatialHash) Remove(shape Shape) error
Remove removes a shape from the spatial hash