Home Reference Source
import {Collisions} from 'collisions'
public class | source

Collisions

A collision system used to track bodies in order to improve collision detection performance

Static Method Summary

Static Public Methods
public static

Creates a Result used to collect the detailed results of a collision test

Constructor Summary

Public Constructor
public

Method Summary

Public Methods
public

collides(target: Circle | Polygon | Point, result: Result, aabb: Boolean): Boolean

Determines if two bodies are colliding

public

createCircle(x: Number, y: Number, radius: Number, scale: Number, padding: Number): Circle

Creates a Circle and inserts it into the collision system

public

createPoint(x: Number, y: Number, padding: Number): Point

Creates a Point and inserts it into the collision system

public

createPolygon(x: Number, y: Number, points: Array<Number[]>, angle: Number, scale_x: Number, scale_y: Number, padding: Number): Polygon

Creates a Polygon and inserts it into the collision system

public

Creates a Result used to collect the detailed results of a collision test

public

draw(context: CanvasRenderingContext2D)

Draws the bodies within the system to a CanvasRenderingContext2D's current path

public

drawBVH(context: CanvasRenderingContext2D)

Draws the system's BVH to a CanvasRenderingContext2D's current path.

public

insert(bodies: ...Circle | ...Polygon | ...Point)

Inserts bodies into the collision system

public

Returns a list of potential collisions for a body

public

remove(bodies: ...Circle | ...Polygon | ...Point)

Removes bodies from the collision system

public

update()

Updates the collision system.

Static Public Methods

public static createResult() source

Creates a Result used to collect the detailed results of a collision test

Public Constructors

public constructor() source

Public Methods

public collides(target: Circle | Polygon | Point, result: Result, aabb: Boolean): Boolean source

Determines if two bodies are colliding

Params:

NameTypeAttributeDescription
target Circle | Polygon | Point

The target body to test against

result Result
  • optional
  • default: null

A Result object on which to store information about the collision

aabb Boolean
  • optional
  • default: true

Set to false to skip the AABB test (useful if you use your own potential collision heuristic)

Return:

Boolean

public createCircle(x: Number, y: Number, radius: Number, scale: Number, padding: Number): Circle source

Creates a Circle and inserts it into the collision system

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 0

The starting X coordinate

y Number
  • optional
  • default: 0

The starting Y coordinate

radius Number
  • optional
  • default: 0

The radius

scale Number
  • optional
  • default: 1

The scale

padding Number
  • optional
  • default: 0

The amount to pad the bounding volume when testing for potential collisions

Return:

Circle

public createPoint(x: Number, y: Number, padding: Number): Point source

Creates a Point and inserts it into the collision system

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 0

The starting X coordinate

y Number
  • optional
  • default: 0

The starting Y coordinate

padding Number
  • optional
  • default: 0

The amount to pad the bounding volume when testing for potential collisions

Return:

Point

public createPolygon(x: Number, y: Number, points: Array<Number[]>, angle: Number, scale_x: Number, scale_y: Number, padding: Number): Polygon source

Creates a Polygon and inserts it into the collision system

Params:

NameTypeAttributeDescription
x Number
  • optional
  • default: 0

The starting X coordinate

y Number
  • optional
  • default: 0

The starting Y coordinate

points Array<Number[]>
  • optional
  • default: []

An array of coordinate pairs making up the polygon - [[x1, y1], [x2, y2], ...]

angle Number
  • optional
  • default: 0

The starting rotation in radians

scale_x Number
  • optional
  • default: 1

The starting scale along the X axis

scale_y Number
  • optional
  • default: 1

The starting scale long the Y axis

padding Number
  • optional
  • default: 0

The amount to pad the bounding volume when testing for potential collisions

Return:

Polygon

public createResult() source

Creates a Result used to collect the detailed results of a collision test

public draw(context: CanvasRenderingContext2D) source

Draws the bodies within the system to a CanvasRenderingContext2D's current path

Params:

NameTypeAttributeDescription
context CanvasRenderingContext2D

The context to draw to

public drawBVH(context: CanvasRenderingContext2D) source

Draws the system's BVH to a CanvasRenderingContext2D's current path. This is useful for testing out different padding values for bodies.

Params:

NameTypeAttributeDescription
context CanvasRenderingContext2D

The context to draw to

public insert(bodies: ...Circle | ...Polygon | ...Point) source

Inserts bodies into the collision system

Params:

NameTypeAttributeDescription
bodies ...Circle | ...Polygon | ...Point

public potentials(body: Circle | Polygon | Point): Array<Body> source

Returns a list of potential collisions for a body

Params:

NameTypeAttributeDescription
body Circle | Polygon | Point

The body to test for potential collisions against

Return:

Array<Body>

public remove(bodies: ...Circle | ...Polygon | ...Point) source

Removes bodies from the collision system

Params:

NameTypeAttributeDescription
bodies ...Circle | ...Polygon | ...Point

public update() source

Updates the collision system. This should be called before any collisions are tested.