collision2d

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2020 License: MIT Imports: 2 Imported by: 3

README

Collision2D

Build Status GoDoc Coverage Status Go Report Card

About

Collision2D is a simple Go library for performing collision detection (and projection-based collision response) of simple 2D shapes. It uses the Separating Axis Theorem. It was based on the JavaScript library SAT-js by jriecken.

It supports detecting collisions between:

  • Circles (using Voronoi Regions.)
  • Convex Polygons (and simple Axis-Aligned Boxes, which are of course, convex polygons.)

It also supports checking whether a point is inside a circle or polygon.

It's released under the MIT license.

To import in your Go project you can use:

`import "github.com/Tarliton/collision2d"`

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PointInCircle

func PointInCircle(point Vector, circle Circle) bool

PointInCircle returns true if the point is inside the circle.

func PointInPolygon

func PointInPolygon(point Vector, polygon Polygon) bool

PointInPolygon returns true if the point is inside a polygon.

Types

type Box

type Box struct {
	Pos  Vector
	W, H float64
}

Box is a simple box with position, width and height.

func NewBox

func NewBox(pos Vector, w, h float64) Box

NewBox create a new box with vector pos as center and width w and height h

func (Box) String

func (box Box) String() string

func (Box) ToPolygon

func (box Box) ToPolygon() Polygon

ToPolygon returns a new polygon whose edges are the edges of the box.

type Circle

type Circle struct {
	Pos Vector
	R   float64
}

Circle is a struct that represents a circle with a position and a raidus.

func NewCircle

func NewCircle(pos Vector, r float64) Circle

NewCircle create a new circle with vector pos as center and radius r

func (Circle) GetAABB

func (circle Circle) GetAABB() Polygon

GetAABB returns the axis-aligned bounding box of the circle.

func (Circle) String

func (circle Circle) String() string

type Polygon

type Polygon struct {
	Pos, Offset                        Vector
	Angle                              float64
	Points, CalcPoints, Edges, Normals []Vector
}

Polygon struct represents a polygon with position and edges in a counter-clockwise fashion.

func NewPolygon

func NewPolygon(pos, offset Vector, angle float64, points []float64) Polygon

NewPolygon creates a new polygon with pos, offset, angle and points. Points is an array of pairs of float64 values, that are mapped into Vectors with X and Y. The first value is X and the second is Y. See test to understand better.

func (Polygon) GetAABB

func (polygon Polygon) GetAABB() Polygon

GetAABB returns the axis-aligned bounding box of the polygon.

func (Polygon) Rotate

func (polygon Polygon) Rotate(angle float64) Polygon

Rotate rotates the polygon by angle in radian.

func (Polygon) SetAngle

func (polygon Polygon) SetAngle(angle float64) Polygon

SetAngle changes the angle of the polygon

func (Polygon) SetOffset

func (polygon Polygon) SetOffset(offset Vector) Polygon

SetOffset changes the offset of the polygon

func (Polygon) SetPoints

func (polygon Polygon) SetPoints(points []Vector) Polygon

SetPoints change the edges of the polygon and recauculate the rest of it's values.

func (Polygon) String

func (polygon Polygon) String() string

func (Polygon) Translate

func (polygon Polygon) Translate(x, y float64) Polygon

Translate the polygon by x and y.

type Response

type Response struct {
	A, B               interface{}
	Overlap            float64
	OverlapN, OverlapV Vector
	AInB, BInA         bool
}

Response contains the information about an collision test.

func NewResponse

func NewResponse() Response

NewResponse is used to create a new response when necessary.

func TestCircleCircle

func TestCircleCircle(circleA, circleB Circle) (isColliding bool, response Response)

TestCircleCircle returns true if the circles collide with each other.

func TestCirclePolygon

func TestCirclePolygon(circle Circle, polygon Polygon) (isColliding bool, response Response)

TestCirclePolygon returns true if the circle collides with the polygon.

func TestPolygonCircle

func TestPolygonCircle(polygon Polygon, circle Circle) (isColliding bool, response Response)

TestPolygonCircle returns true if the polygon collides with the circle.

func TestPolygonPolygon

func TestPolygonPolygon(polygonA, polygonB Polygon) (isColliding bool, response Response)

TestPolygonPolygon returns true if the polygons collide with each other.

func (*Response) NotColliding

func (response *Response) NotColliding() Response

NotColliding is to be used when A and B are not colliding and response should be ignored.

func (Response) String

func (response Response) String() string

type Vector

type Vector struct {
	X, Y float64
}

Vector is a simple 2D vector/point struct.

func NewVector

func NewVector(x, y float64) Vector

NewVector create a new vector with the values of x and y

func (Vector) Add

func (vector Vector) Add(another Vector) Vector

Add returns a new vector with the result of adding another vector to this one.

func (Vector) Clone

func (vector Vector) Clone() Vector

Clone this vector coordinates to a new vector with the same coordinates as this one.

func (Vector) Copy

func (vector Vector) Copy(another Vector) Vector

Copy the value of another vector to a new one.

func (Vector) Dot

func (vector Vector) Dot(another Vector) float64

Dot returns the dot product of this vector and another

func (Vector) Len

func (vector Vector) Len() float64

Len returns the length of this vector

func (Vector) Len2

func (vector Vector) Len2() float64

Len2 returns the squared length of this vector

func (Vector) Normalize

func (vector Vector) Normalize() Vector

Normalize returns a new unit-length vector.

func (Vector) Perp

func (vector Vector) Perp() Vector

Perp returns a new vector perpendicular from this one.

func (Vector) Project

func (vector Vector) Project(another Vector) Vector

Project this vector onto another one

func (Vector) ProjectN

func (vector Vector) ProjectN(another Vector) Vector

ProjectN this vector onto a unit vector

func (Vector) Reflect

func (vector Vector) Reflect(axis Vector) Vector

Reflect this vector on an arbitrary axis vector

func (Vector) ReflectN

func (vector Vector) ReflectN(axis Vector) Vector

ReflectN this vector on an arbitrary axis unit vector

func (Vector) Reverse

func (vector Vector) Reverse() Vector

Reverse returns a new vector that is reversed from this one.

func (Vector) Rotate

func (vector Vector) Rotate(angle float64) Vector

Rotate returns a new vector rotated counter-clockwise by the specified number of radians.

func (Vector) Scale

func (vector Vector) Scale(x float64) Vector

Scale returns a new vector scaled in the direction of X and Y by value x

func (Vector) ScaleDifferent

func (vector Vector) ScaleDifferent(x, y float64) Vector

ScaleDifferent returns a new vector scaled in the direction of X and Y by value x and y respectively

func (Vector) String

func (vector Vector) String() string

func (Vector) Sub

func (vector Vector) Sub(another Vector) Vector

Sub returns a new vector that is the result of subtracting another vector from this one.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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