collider

package module
v0.0.0-...-2c56b94 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 3 Imported by: 0

README

Use this instead if you're using ebiten

Documentation

Overview

Package collider provides simple collision detection and resolution

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrShapeNotFound = errors.New("Couldn't remove shape from SpatialHash; not found")
)

Vars

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Shapes map[Shape]Shape
}

Cell contains shapes

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) GetHash

func (ci *CircleShape) GetHash() *SpatialHash

GetHash gets the hash

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) SetHash

func (ci *CircleShape) SetHash(s *SpatialHash)

SetHash sets the hash

func (*CircleShape) SetParent

func (ci *CircleShape) SetParent(i interface{})

SetParent sets the parent

type CollisionData

type CollisionData struct {
	Other            Shape
	SeparatingVector *vector.Vector
}

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) 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

Jump to

Keyboard shortcuts

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