collision2d

package
v0.0.0-...-e7326f8 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

README

Here is fork of https://github.com/Tarliton/collision2d

Original code by Tarliton. All credits to this code author.

WARNING

Some library functions isn't work (TestPolygonCircle(), TestCirclePolygon()). Due to this, method Circle.ToPolygon() has been created as replacement. If you are is author of this library, please, correct these functions.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PointInCircle

func PointInCircle(point *vector2.Vector2, circle *Circle) bool

WARN: Returns incorrect result, use PointInPolygon(point, circle.ToPolygon()) instead of. This functions is not under maintance, but if you have solution you can create PR or issue.

PointInCircle returns true if the point is inside the circle.

func PointInPolygon

func PointInPolygon(point *vector2.Vector2, polygon *Polygon) bool

PointInPolygon returns true if the point is inside a polygon.

Types

type Box

type Box struct {
	Pos  *vector2.Vector2
	W, H float64
}

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

func NewBox

func NewBox(pos *vector2.Vector2, 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        *vector2.Vector2
	R          float64
	SizeScalar *vector2.Vector2
	Resolution int
}

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

func NewCircle

func NewCircle(pos *vector2.Vector2, r float64, sizeScalar *vector2.Vector2) *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

func (*Circle) ToPolygon

func (circle *Circle) ToPolygon() *Polygon

type Polygon

type Polygon struct {
	Pos, Offset                        *vector2.Vector2
	Angle                              float64
	Points, CalcPoints, Edges, Normals []*vector2.Vector2
	// contains filtered or unexported fields
}

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

func NewPolygon

func NewPolygon(pos, offset *vector2.Vector2, 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) Clone

func (polygon *Polygon) Clone(pos *vector2.Vector2, angle float64) *Polygon

func (*Polygon) GetAABB

func (polygon *Polygon) GetAABB() *Polygon

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

func (*Polygon) GetOriginal

func (polygon *Polygon) GetOriginal() *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 *vector2.Vector2) *Polygon

SetOffset changes the offset of the polygon

func (*Polygon) SetPoints

func (polygon *Polygon) SetPoints(points []*vector2.Vector2) *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 *vector2.Vector2
	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)

WARN: Returns incorrect result, use TestPolygonPolygon(polygon, circle.ToPolygon()) instead of. This functions is not under maintance, but if you have solution you can create PR or issue.

TestCirclePolygon returns true if the circle collides with the polygon.

func TestPolygonCircle

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

WARN: Returns incorrect result, use TestPolygonPolygon(polygon, circle.ToPolygon()) instead of. This functions is not under maintance, but if you have solution you can create PR or issue.

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

Jump to

Keyboard shortcuts

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