detect-collisions

2d collision detection for circles and polygons (with SAT and BVH)

Usage no npm install needed!

<script type="module">
  import detectCollisions from 'https://cdn.skypack.dev/detect-collisions';
</script>

README

Introduction

Detect-Collisions is JavaScript library for quickly and accurately detecting collisions between Polygons, Circles, Boxes, and Points. 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.

https://badge.fury.io/js/detect-collisions https://snyk.io/test/github/Prozi/detect-collisions https://circleci.com/gh/Prozi/detect-collisions

Demos

Install

$ yarn add detect-collisions

Usage

1. Creating a System

System extends RBush so it has all of its functionalities.

To start, create a unique collisions system:

const system = new System();

2. Creating, Inserting, Moving, Removing Bodies

Circle, Polygon, Box, Point extend their respective SAT counterparts so they have all of its functionalities.

  • Circle: A shape with infinite sides equidistant from a single point
  • Polygon: A shape made up of line segments
  • Box: A shape like a rectangle (implemented as polygon)
  • Point: A single coordinate (implemented as tiny box)

all bodies have pos property which is a Vector { x, y } containing its position

Create bodies:

const circle = new Circle({ x: 100, y: 100 }, 10);
const polygon = new Polygon({ x: 50, y: 50 }, [
  { x: 0, y: 0 },
  { x: 20, y: 20 },
  { x: -10, y: 10 },
]);
const line = new Polygon({ x: 200, y: 5 }, [
  { x: -30, y: 0 },
  { x: 10, y: 20 },
]);
const point = new Point({ x: 10, y: 10 });

Insert bodies to system:

system.insert(circle);
system.insert(polygon);
system.insert(line);
system.insert(point);

Create and insert to system:

const circle = system.createCircle({ x: 100, y: 100 }, 10);
const polygon = system.createPolygon({ x: 50, y: 50 }, [
  { x: 0, y: 0 },
  { x: 20, y: 20 },
  { x: -10, y: 10 },
]);
const line = system.createPolygon({ x: 200, y: 5 }, [
  { x: -30, y: 0 },
  { x: 10, y: 20 },
]);
const point = system.createPoint({ x: 10, y: 10 });

Moving bodies:

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

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

Remove bodies from system:

system.remove(circle);
system.remove(polygon);
system.remove(line);
system.remove(point);

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.

system.updateBody(body);

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

system.update();

4. Testing for Collisions

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

system.checkAll(handleCollisions);

If you really need to check one body then use:

system.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 = system.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.

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

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

if (system.checkCollision(polygon, line)) {
  console.log("Collision detected!", system.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 system.response object. The SAT.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 (system.checkCollision(player, wall)) {
  const { overlapV } = system.response;

  player.x -= overlapV.x;
  player.y -= overlapV.y;
}

7. Detecting collision after insertion

const collider = system.createCircle({ x: 100, y: 100 }, 10);
const potentials = system.getPotentials(collider);
const obj = { name: "coin", collider };
const collided = potentials.some((body) =>
  system.checkCollision(collider, body)
);

if (collided) {
  system.remove(obj.collider);
  obj.collider = null;
}

Lines

Creating a line is simply a matter of creating a single-sided polygon (i.e. a polygon with only two coordinate pairs).

const line = new Polygon({ x: 200, y: 5 }, [
  { x: -30, y: 0 },
  { x: 10, y: 20 },
]);

Concave Polygons

Detect-Collisions uses the Separating Axis Theorem (SAT) for its narrow-phase collision tests. One caveat to SAT is that it only works properly on convex bodies. However, concave polygons can be "faked" by using a series of Lines. Keep in mind that a polygon drawn using Lines is "hollow".

Handling true concave polygons requires breaking them down into their component convex polygons (Convex Decomposition) and testing them for collisions individually. There are plans to integrate this functionality into the library in the future, but for now, check out poly-decomp.js.

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();

system.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();

system.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 = new Circle({ x: 45, y: 45 }, 20);
const polygon = new Polygon({ x: 50, y: 50 }, [
  { x: 0, y: 0 },
  { x: 20, y: 20 },
  { x: -10, y: 10 },
]);

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

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.