collide

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

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

Go to latest
Published: Feb 21, 2022 License: MIT Imports: 1 Imported by: 0

README

go-collide

godocs.io

Package collide implements 2D collision detection in Go.

This package implements collision detection only. Resolving collisions is not implemented.

Usage

import "git.sr.ht/~adnano/go-collide"

Examples

There are a few examples provided in the examples directory. To run an example:

go run examples/distance.go

Contributing

Send patches and questions to ~adnano/go-collide-devel.

Subscribe to release announcements on ~adnano/go-collide-announce.

Credits

This library is based on:

The following resources explain the various algorithms used in this library.

Documentation

Overview

Package collide implements 2D collision detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cross

func Cross(p, q Point) float64

Cross returns the cross product of p and q.

func Distance

func Distance(a Shape, xfa Transform, b Shape, xfb Transform) float64

Distance returns the distance between a and b.

func Dot

func Dot(p, q Point) float64

Dot returns the dot product of p and q.

func TimeOfImpact

func TimeOfImpact(simplex *Simplex, a Shape, sweepA Sweep, b Shape, sweepB Sweep) float64

CCD via the local separating axis method. This seeks progression by computing the largest time at which separation is maintained.

Types

type Circle

type Circle struct {
	Center Point
	Radius float64
}

Circle represents a circle shape.

type Collision

type Collision struct {
	Normal Point
	Depth  float64
}

Collision represents a collision.

func Collide

func Collide(a Shape, xfa Transform, b Shape, xfb Transform) *Collision

Collide calculates a collision for two shapes.

func CollideCircleAndPolygon

func CollideCircleAndPolygon(a *Circle, xfa Transform, b *Polygon, xfb Transform) *Collision

func CollideCircles

func CollideCircles(a *Circle, xfa Transform, b *Circle, xfb Transform) *Collision

func CollidePolygonAndCircle

func CollidePolygonAndCircle(a *Polygon, xfa Transform, b *Circle, xfb Transform) *Collision

func CollidePolygons

func CollidePolygons(a *Polygon, xfa Transform, b *Polygon, xfb Transform) *Collision

type Point

type Point struct {
	X, Y float64
}

A Point is an X, Y coordinate pair.

func CrossPS

func CrossPS(p Point, s float64) Point

func CrossSP

func CrossSP(s float64, p Point) Point

func Pt

func Pt(X, Y float64) Point

Pt is shorthand for Point{X, Y}.

func (Point) Add

func (p Point) Add(q Point) Point

Add returns the vector p+q.

func (Point) Div

func (p Point) Div(k float64) Point

Div returns the vector p/k.

func (Point) IsZero

func (p Point) IsZero() bool

IsZero reports whether the point has zero length.

func (Point) Length

func (p Point) Length() float64

Length returns the length of the vector p.

func (Point) LengthSquared

func (p Point) LengthSquared() float64

LengthSquared returns the squared length of the vector p.

func (Point) Mul

func (p Point) Mul(k float64) Point

Mul returns the vector p*k.

func (Point) Neg

func (p Point) Neg() Point

Neg returns the vector -p.

func (Point) Normalize

func (p Point) Normalize() Point

Normalize returns the unit vector in the direction of p.

func (Point) Sub

func (p Point) Sub(q Point) Point

Sub returns the vector p-q.

type Polygon

type Polygon struct {
	Points  []Point
	Normals []Point
}

Polygon represents a collection of points.

func NewPolygon

func NewPolygon(points ...Point) *Polygon

NewPolygon returns a polygon with the given points specified in clockwise order.

func Rect

func Rect(x, y, w, h float64) *Polygon

Rect is shorthand for Rectangle(Point{x, y}, Point{w/2, h/2}).

func Rectangle

func Rectangle(center, extents Point) *Polygon

Rectangle returns a rectangular polygon shape with the given center and half extents.

type Rotation

type Rotation struct {
	Sin, Cos float64
}

Rotation represents a rotation.

func NewRotation

func NewRotation(theta float64) Rotation

NewRotation returns a new rotation of theta radians.

func (Rotation) Mul

func (r Rotation) Mul(p Point) Point

Mul returns the vector p rotated.

func (Rotation) MulT

func (r Rotation) MulT(p Point) Point

MulT returns the vector p inverse rotated.

type Shape

type Shape interface {
	// contains filtered or unexported methods
}

Shape represents a shape.

type Simplex

type Simplex struct {
	// contains filtered or unexported fields
}

Simplex represents a simplex.

func (*Simplex) ClosestPoint

func (s *Simplex) ClosestPoint() Point

ClosestPoint returns the closest point on the simplex to the origin.

func (*Simplex) GJK

func (s *Simplex) GJK(a Shape, xfa Transform, b Shape, xfb Transform)

GJK implements the Gilbert-Johnson-Keerthi distance algorithm.

func (*Simplex) Metric

func (s *Simplex) Metric() float64

Metric returns a metric that identifies this simplex.

func (*Simplex) ReadCache

func (s *Simplex) ReadCache(cache *SimplexCache, a Shape, xfa Transform, b Shape, xfb Transform)

ReadCache populates the simplex from the cache.

func (*Simplex) WitnessPoints

func (s *Simplex) WitnessPoints() (Point, Point)

WitnessPoints returns the witness points on the original shapes.

func (*Simplex) WriteCache

func (s *Simplex) WriteCache(cache *SimplexCache)

WriteCache populates the cache.

type SimplexCache

type SimplexCache struct {
	// contains filtered or unexported fields
}

SimplexCache is used to speed up repeated calls to distance functions.

type Sweep

type Sweep struct {
	P0, P1 Point   // position
	R0, R1 float64 // rotation
}

A Sweep interpolates between two positions and orientations.

func (Sweep) Advance

func (s Sweep) Advance(t float64) Sweep

Advance advances the position and rotation to time t.

func (Sweep) GetTransform

func (s Sweep) GetTransform(t float64) Transform

GetTransform returns the transform at time t.

type Transform

type Transform struct {
	Position Point
	Rotation Rotation
}

A Transform represents translation and rotation.

func NewTransform

func NewTransform(position Point, rotation float64) Transform

NewTransform returns a Transform with the given position and rotation.

func (Transform) Mul

func (t Transform) Mul(p Point) Point

Mul returns the transformation of the vector p.

func (Transform) MulT

func (t Transform) MulT(p Point) Point

MulT returns the inverse transformation of the vector p.

Jump to

Keyboard shortcuts

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