collisions

package module
v0.0.0-...-d1a0a59 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: MIT Imports: 4 Imported by: 4

README

ebiten-collisions

A simple AABB collision lib for ebiten

Usage

import collisions "github.com/tducasse/ebiten-collisions"

world := collisions.MakeWorld()

// pass the world to your entities, then do something like:
p.World = opt.World
p.CollisionShape = collisions.MakeBox(p.X, p.Y, p.W, p.H)
// you can add data to the box, useful to filter collisions or to keep references to the entity
p.CollisionShape.AddData("player")
p.World.Add(p.CollisionShape)

// this is a function that returns true or false, used to filter collisions
func filter(self *collisions.Box, other *Box) bool {
  entityType := other.Data.(string)
  return entityType == "enemy"
}



// now when you want to move, do this instead
x, y, collisions := p.World.Move(p.CollisionShape, dx, dy, filter)
// where:
// dx is the amount you want to move the entity on the x-axis
// dy is the amount you want to move the entity on the y-axis
// filter is a function which filters collisions; return true to collide 

// collisions is a []*Collision (len == 0 if it is not colliding with anything)
type Collision struct {
	Other      *Box
	CollidingX bool
	CollidingY bool
}

// there's a few other functions
world.Remove(p.CollisionShape)
world.AddBox(collisions.MakeBox(0, 0, 3, 4))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box struct {
	Data interface{}
	W    float64
	H    float64
	X    float64
	Y    float64
}

func MakeBox

func MakeBox(x float64, y float64, w float64, h float64) *Box

func (*Box) AddData

func (b *Box) AddData(data interface{})

func (*Box) Collides

func (b *Box) Collides(o *Box, filter func(self *Box, other *Box) bool) bool

type Collision

type Collision struct {
	Other      *Box
	CollidingX bool
	CollidingY bool
}

type World

type World struct {
	Items []*Box
}

func MakeWorld

func MakeWorld() *World

func (*World) Add

func (w *World) Add(box *Box)

func (*World) AddNewBox

func (world *World) AddNewBox(x float64, y float64, w float64, h float64)

func (*World) Debug

func (w *World) Debug(screen *ebiten.Image)

func (*World) Move

func (w *World) Move(box *Box, dx float64, dy float64, filterFunc func(self *Box, other *Box) bool) (float64, float64, []*Collision)

func (*World) Remove

func (w *World) Remove(box *Box)

Jump to

Keyboard shortcuts

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