collider

package module
v0.0.0-...-71ba582 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2022 License: MIT Imports: 7 Imported by: 0

README

ebiten-collider

Basic collision detection optimized with the use of spatial partitioning.

It can detect and resolve the following collision pairs:

  • Rectangle + Rectangle
  • Rectangle + Circle
  • Rectangle + Point
  • Circle + Circle
  • Circle + Point
  • Point + Point

Usage

📖 Docs
Look at the example to see how to use the library.

// Create the world
hash = collider.NewSpatialHash(128)

// Create a shape
wall = hash.NewRectangleShape(
    0
    16,
    128,
    128,
)

// Set arbitrary data. Useful for linking a shape to another struct.
wall.SetParent("I'm a wall")
// or...
wall.SetParent(Tile{Type:"Wall"})
// GetParent during a collision, check it's type and then change how the collision is handled. 
// If it returns data which represents a wall, then you should move by the collision.SeparatingVector, but if it's a 
// floor tile, then it can be ignored.
log.Println(wall.GetParent())

// Check for collisions
collisions := hash.CheckCollisions(player)
for _, collision := range collisions {
    sep := collision.SeparatingVector
    // Move a shape by the overlap
    player.Move(sep.X, sep.Y)

    // Or move both shapes equally
    player.Move(sep.X/2, sep.Y/2)
    collision.Other.Move(-sep.X/2, -sep.Y/2)
}

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

Directories

Path Synopsis
examples
moveAndCollide
Package main 👍
Package main 👍

Jump to

Keyboard shortcuts

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