Skip to content

It is a fast TypeScript library for detecting collisions between bodies: Points, Lines, Boxes, Polygons, Ellipses and Circles, and for Raycasting. All bodies can have offset, rotation, scale, bounding box padding, be static (non moving) or be trigger bodies (non colliding). Compatible with colyseus/schema

License

Notifications You must be signed in to change notification settings

gunsurvival/detect-collisions

Β 
Β 

Repository files navigation

Detect-Collisions

npm version npm downloads per week vulnerabilities build status license: MIT

Introduction

It is a fast TypeScript library for detecting collisions between bodies: Points, Lines, Boxes, Polygons, Ellipses and Circles, and for Raycasting. All bodies can have offset, rotation, scale, bounding box padding, be static (non moving) or be trigger bodies (non colliding).

This library combines:

Demos

Installation

$ npm install detect-collisions

API Documentation

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

Usage

1. Create System

System extends RBush so it has all of its functionalities.

To start, create a unique collisions system:

const physics = new System();

2. Body Information

πŸ‘‰ each body has:

  • setPosition(x, y) method - calling it updates bounding box
  • x & y properties - setting any of those updates bounding box
  • pos.x, pos.y properties - setting those doesn't update bounding box
  • angle property and setAngle() method - to rotate
  • scale property and setScale() method - to scale (for Circle takes 1 parameter, x, y for rest)
  • offset and setOffset() method - for offset from center of body
  • getAABBAsBBox() method - for getting bbox even on non inserted bodies
  • center() method - for centering anchor (useless but available for Circle, Ellipse)
  • isStatic property - if true body doesn't move
  • isTrigger property - if true body doesn't trigger collisions
  • isCentered property - if true the body is centered (true for Circle, Ellipse)
  • isConvex property - if true the body is convex (may be false only for Polygon)
  • padding property - ignores costly tree update until outside bbox with padding

πŸ‘‰ some bodies:

  • Box has width & height properties

πŸ‘‰ each body after inserted to system have:

  • bbox = { minX, minY, maxX, maxY } property - without padding
  • minX, minY, maxX, maxY properties - bbox plus padding
  • system property - to use body.system.updateBody(body) internally

πŸ‘‰ body types:

  • 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)

3. Create and insert Body

Last optional parameter for body creation is always BodyOptions

const options = {
  angle: 0,
  center: false,
  isStatic: false,
  isTrigger: false,
  padding: 0;
}

πŸ‘‰ Only create Body

// create with options, without insert
const circle = new Circle(position, radius, options);
const polygon = new Polygon(position, points, options);

πŸ‘‰ Only insert Body

// insert, without create
physics.insert(circle);
physics.insert(polygon);

πŸ‘‰ Create and insert Body

// create with options, and insert
const circle = physics.createCircle(position, radius, options);
const polygon = physics.createPolygon(position, points, options);

4. Move Body

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

5. Remove Body

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

6. Update Body or 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.

// update one body, use anytime
physics.updateBody(body);
// update all bodies (use 0-1 times per frame):
physics.update();

7. Collision Detection

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

It is 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);
}

πŸ‘‰ 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.

πŸ‘‰ 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 } = physics.response;

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

Detecting collision after insertion

// create self-destructing collider
const testCollision = ({ x, y }, radius = 10) => {
  const circle = physics.createCircle({ x, y }, radius);
  const potentials = physics.getPotentials(circle);
  const collided = potentials.some((body) =>
    physics.checkCollision(circle, body)
  );

  physics.remove(circle);

  return collided;
};

Concave Polygons

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

the System.response.aInB and System.response.bInA is currently because of complexity and speed reasons only checking the bounding boxes of compared bodies. for more informations relate to this issue on github and this merged pull request

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. Note that unless a use-case really requires this, I strongly advise to use the normal flow.

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

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

Raycast

To get raycast information use

const start = { x: 0, y: 0 };
const end = { x: 0, y: -10 };
const hit = 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

$ git clone https://github.com/Prozi/detect-collisions.git
$ cd detect-collisions
$ yarn
$ yarn benchmark [milliseconds=1000]

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

typical output:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ value β”‚        name         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚  317  β”‚     'Total FPS'     β”‚
β”‚    1    β”‚  124  β”‚ 'FPS / 1000 items'  β”‚
β”‚    2    β”‚  57   β”‚ 'FPS / 2000 items'  β”‚
β”‚    3    β”‚  34   β”‚ 'FPS / 3000 items'  β”‚
β”‚    4    β”‚  24   β”‚ 'FPS / 4000 items'  β”‚
β”‚    5    β”‚  22   β”‚ 'FPS / 5000 items'  β”‚
β”‚    6    β”‚  15   β”‚ 'FPS / 6000 items'  β”‚
β”‚    7    β”‚  13   β”‚ 'FPS / 7000 items'  β”‚
β”‚    8    β”‚  10   β”‚ 'FPS / 8000 items'  β”‚
β”‚    9    β”‚   9   β”‚ 'FPS / 9000 items'  β”‚
β”‚   10    β”‚   9   β”‚ 'FPS / 10000 items' β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Done in 14.58s.

About

It is a fast TypeScript library for detecting collisions between bodies: Points, Lines, Boxes, Polygons, Ellipses and Circles, and for Raycasting. All bodies can have offset, rotation, scale, bounding box padding, be static (non moving) or be trigger bodies (non colliding). Compatible with colyseus/schema

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 50.3%
  • JavaScript 47.6%
  • HTML 1.2%
  • Other 0.9%