detect-collisions icon indicating copy to clipboard operation
detect-collisions copied to clipboard

detecting collisions between bodies: Points, Lines, Boxes, Polygons (Concave too), Ellipses and Circles. Also RayCasting. All bodies can have offset, rotation, scale, bounding box padding, can be stat...

Introduction

Detect-Collisions is TypeScript (compiled to JavaScript, fully typed) library for quickly and accurately detecting collisions between Points, Lines, Boxes, Polygons, Ellipses and Circles, also with rotation. It combines the efficiency of a Bounding Volume Hierarchy (BVH) for broad-phase searching and the accuracy of the Separating Axis Theorem (SAT) for narrow-phase collision testing.

npm version npm downloads per week build status

license: MIT typescript vulnerabilities

Demos

Install

$ yarn add detect-collisions

API Documentation

https://prozi.github.io/detect-collisions/modules.html

Usage

1. Creating a System

System extends RBush so it has all of its functionalities.

To start, create a unique collisions system:

const physics: System = new System();

2. Creating, Inserting, Moving, Removing Bodies

  • Circle & Polygon extend their respective SAT counterparts so they have all of its functionalities.
  • all bodies have x & y properties, setting those will update bounding box
  • all bodies have setPosition(x, y), using it will update bounding box
  • all bodies have pos with x & y properties, setting those will not update bounding box
  • all bodies have angle and setAngle() method to rotate (useless for Circle but stores value)
  • all bodies have scale and setScale() method to scale (for Circle takes 1 parameter, x, y for rest)
  • all bodies have center() method for centering anchor (useless for Circle, Ellipse, Point)
  • Box has width & height properties

Circle - Shape with infinite sides equidistant of radius from its center position

Ellipse - Flattened circle (implemented as polygon)

Polygon - Shape made up of finite number of line segments

Box - Rectangle (implemented as polygon)

Line - Line (implemented as 2-point polygon)

Point - A single coordinate (implemented as tiny box)


Create bodies:

Last optional parameter for body creation is always BodyOptions:

const circle: Circle = new Circle(position, radius, options);
const polygon: Polygon = new Polygon(position, points, options);

Insert bodies to system:

physics.insert(circle);
physics.insert(polygon);

Create and insert to system in one step:

Last optional parameter for body creation with insertion is always BodyOptions:

const circle: Circle = physics.createCircle(position, radius, options);
const polygon: Polygon = physics.createPolygon(position, points, options);

Moving bodies:

setPosition: this modifies the element.pos.x and element.pos.y and updates its bounding box in collision physics.

circle.setPosition(x, y);
polygon.setPosition(x, y);

Remove bodies from system:

physics.remove(circle);
physics.remove(polygon);

3. Updating the Collisions System

  • After body moves, its bounding box in collision tree needs to be updated.

  • This is done under-the-hood automatically when you use setPosition().

Collisions systems need to be updated when the bodies within them change. This includes when bodies are inserted, removed, or when their properties change (e.g. position, angle, scaling, etc.). Updating a collision system can be done by calling update() which should typically occur once per frame. Updating the System by after each position change is required for System to detect BVH correctly.

physics.updateBody(body);

Update all bodies (use 0-1 times per frame):

physics.update();

4. Testing for Collisions

The preferred method is once-in-a-gameloop checkAll and then handler:

physics.checkAll(handleCollisions);

If you really need to check one body then use:

physics.checkOne(body, handleCollisions);

When testing for collisions on a body, it is generally recommended that a broad-phase search be performed first by calling getPotentials(body) in order to quickly rule out bodies that are too far away to collide. Detect-Collisions uses a Bounding Volume Hierarchy (BVH) for its broad-phase search. Calling getPotentials(body) on a body traverses the BVH and builds a list of potential collision candidates. Skipping the broad-phase search is not recommended. When testing for collisions against large numbers of bodies, performing a broad-phase search using a BVH is much more efficient.

const potentials: Body[] = physics.getPotentials(body);

Once a list of potential collisions is acquired, loop through them and perform a narrow-phase collision test using checkCollision(). Detect-Collisions uses the Separating Axis Theorem (SAT) for its narrow-phase collision tests.

physics.getPotentials(body).forEach((collider: Body) => {
  if (physics.checkCollision(body, collider)) {
    handleCollisions(physics.response);
  }
});

It is also possible to skip the broad-phase search entirely and call checkCollision() directly on two bodies.

if (physics.checkCollision(polygon, line)) {
  console.log("Collision detected!", physics.response);
}

5. Getting Detailed Collision Information

There is often a need for detailed information about a collision in order to react to it appropriately. This information is stored inside physics.response object. The Response (documentation) object has several properties set on them when a collision occurs:

  • a - The first object in the collision.
  • b - The second object in the collison.
  • overlap - Magnitude of the overlap on the shortest colliding axis.
  • overlapN - The shortest colliding axis (unit-vector)
  • overlapV - The overlap vector (i.e. overlapN.scale(overlap, overlap)). If this vector is subtracted from the position of a, a and b will no longer be colliding.
  • aInB - Whether the first object is completely inside the second.
  • bInA - Whether the second object is completely inside the first.

6. Negating Overlap

A common use-case in collision detection is negating overlap when a collision occurs (such as when a player hits a wall). This can be done using the collision information in a Response object (see Getting Detailed Collision Information).

The three most useful properties on a Response object are overlapV, a, and b. Together, these values describe how much and in what direction the source body is overlapping the target body. More specifically, overlapV.x and overlapV.y describe the scaled direction vector. If this vector is subtracted from the position of a, a and b will no longer be colliding.

These values can be used to "push" one body out of another using the minimum distance required. More simply, subtracting this vector from the source body's position will cause the bodies to no longer collide. Here's an example:

if (physics.checkCollision(player, wall)) {
  const { overlapV }: Response = physics.response;

  player.setPosition(player.x - overlapV.x, player.y - overlapV.y);
}

7. Detecting collision after insertion

// create self-destructing collider
function testCollision(position: Vector, radius: number = 10): boolean {
  const circle: Circle = physics.createCircle(position, radius);
  const potentials: Body[] = physics.getPotentials(circle);
  const collided: boolean = potentials.some((body: Body) =>
    physics.checkCollision(circle, body)
  );

  physics.remove(circle);

  return collided;
}

Concave Polygons

Hollow / non-convex polygons are fully supported since v6.3.0!

Rendering

For debugging, it is often useful to be able to visualize the collision bodies. All of the bodies in a Collision system can be drawn to a <canvas> element by calling draw() and passing in the canvas' 2D context.

const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");

context.strokeStyle = "#FFFFFF";
context.beginPath();

physics.draw(context);

context.stroke();

Bodies can be individually drawn as well.

context.strokeStyle = "#FFFFFF";
context.beginPath();

polygon.draw(context);
circle.draw(context);

context.stroke();

The BVH can also be drawn to help test Bounding Volume Hierarchy.

context.strokeStyle = "#FFFFFF";
context.beginPath();

physics.drawBVH(context);

context.stroke();

Only using SAT

Some projects may only have a need to perform SAT collision tests without broad-phase searching. This can be achieved by avoiding collision systems altogether and only using the checkCollision() function.

const circle: Circle = new Circle(position, radius);
const polygon: Polygon = new Polygon(position, points);

if (physics.checkCollision(polygon, circle)) {
  console.log(physics.result);
}

Raycast

To get raycast information use

const start: Vector = { x: 0, y: 0 };
const end: Vector = { x: 0, y: -10 };
const hit: RaycastResult = physics.raycast(start, end);

if (hit) {
  const { point, collider } = hit;

  console.log({ point, collider });
}
  • point is the Vector { x, y } with coordinates of (closest) intersection
  • collider is the reference to body of the (closest) collider

FAQ

Why shouldn't I just use a physics engine?

Projects requiring physics are encouraged to use one of the several physics engines out there (e.g. Matter.js, Planck.js). However, many projects end up using physics engines solely for collision detection, and developers often find themselves having to work around some of the assumptions that these engines make (gravity, velocity, friction, etc.). Detect-Collisions was created to provide robust collision detection and nothing more. In fact, a physics engine could easily be written with Detect-Collisions at its core.

Sometimes bodies can "squeeze" between two other bodies. What's going on?

This isn't caused by faulty collisions, but rather how a project handles its collision responses. There are several ways to go about responding to collisions, the most common of which is to loop through all bodies, find their potential collisions, and negate any overlaps that are found one at a time. Since the overlaps are negated one at a time, the last negation takes precedence and can cause the body to be pushed into another body.

One workaround is to resolve each collision, update the collision system, and repeat until no collisions are found. Keep in mind that this can potentially lead to infinite loops if the two colliding bodies equally negate each other. Another solution is to collect all overlaps and combine them into a single resultant vector and then push the body out, but this can get rather complicated.

There is no perfect solution. How collisions are handled depends on the project.

Benchmark

$ yarn add detect-collisions
$ cd node_modules/detect-collisions
$ yarn benchmark [miliseconds=1000]

will show you the Stress Demo results without drawing, only using Detect-Collisions and with different N amounts of dynamic, moving bodies. fps cap is at 120.

typical output:

stress test with 1000 items created
{ duration: 5000, frames: 596, fps: 119.2 }
stress test with 2000 items created
{ duration: 5000, frames: 600, fps: 120 }
stress test with 4000 items created
{ duration: 5000, frames: 609, fps: 121.8 }
stress test with 8000 items created
{ duration: 5000, frames: 377, fps: 75.4 }
stress test with 16000 items created
{ duration: 5000, frames: 197, fps: 39.4 }
stress test with 32000 items created
{ duration: 5000, frames: 101, fps: 20.2 }
stress test with 64000 items created
{ duration: 5000, frames: 47, fps: 9.4 }
stress test with 128000 items created
{ duration: 5000, frames: 21, fps: 4.2 }