9.24.0 • Published 1 month ago

detect-collisions v9.24.0

Weekly downloads
113
License
MIT
Repository
github
Last release
1 month ago

Detect-Collisions

Introduction

Detect-Collisions 💫 is a lightning-fast ⚡️ TypeScript library built to detect collisions between diverse shapes like Points, Lines, Boxes, Polygons (including concave), Ellipses, and Circles. Utilizing Bounding Volume Hierarchy (BVH) and the Separating Axis Theorem (SAT), it offers rapid and accurate collision detection. The library supports RayCasting, offsets, rotation, scaling, and optimizations for: bounding box, flags for non-moving and ghost/trigger bodies and collision groups filtering - making it an ideal choice for high-speed applications in gaming and simulations.

Demos

Installation

npm i detect-collisions --save

API Documentation

For detailed documentation on the library's API, refer to the following link:

Detect-Collisions API Documentation

Usage

Step 1: Initialize Collision System

Initialize a unique collision system using Detect-Collisions:

const { System } = require("detect-collisions");
const system = new System();

Step 2: Understand Body Attributes

Bodies possess various properties:

  • Position: Use setPosition(x: number, y: number) for teleport and move(speed: number) for moving forward in direction of its angle.
  • Scale: Use setScale(x: number, y: number) for setting and scale: Vector for getting scale
  • Rotation: Use setAngle(radians: number) for setting and angle: number for getting and deg2rad(degrees: number) to convert to radians.
  • Offset: Use setOffset(offset: Vector) for setting and offset: Vector for getting offset from the body center.
  • AABB Bounding Box: Use aabb: BBox for inserted or getAABBAsBBox(): BBox for non inserted bodies to get the bounding box.
  • Padding: Use padding: number and set to nonzero value to reduce costly reinserts on attributes' change.
  • Collision Filtering: Use group: number for collision filtering, with a range within 0x0 ~ 0x7fffffff.
  • Body Options: Read more in BodyOptions documentation

Step 3: Create and Manage Bodies

Create bodies of various types and manage them:

const {
  Box,
  Circle,
  Ellipse,
  Line,
  Point,
  Polygon,
} = require("detect-collisions");

// Example: Create and insert box1 body
const box1 = system.createBox(position, width, height, options);
// Example: Create box2 body
const box2 = new Box(position, width, height, options);
// Example: Insert box2 body
system.insert(box2);

Step 4: Manipulate Bodies

Manipulate body attributes and update the collision system:

// if omitted updateNow is true
const updateNow = false;
// this should be time scaled, 1 for example
const speed = 1;

// teleport
box.setPosition(x, y, updateNow);
box.setScale(scaleX, scaleY, updateNow);
box.setAngle(angle, updateNow);
box.move(speed, updateNow);
box.setOffset({ x, y }, updateNow);
console.log(box.dirty); // true

box.updateBody(); // Update the body once, when all manipulations are done
console.log(box.dirty); // false

box.group = group; // Immediate effect, no body/system update needed
console.log(box.dirty); // false

Step 5: Collision Detection and Resolution

Detect collisions and respond accordingly:

if (system.checkAll(callback)) {
  // Do something yourself
}

if (system.checkOne(body, callback)) {
  // Do something yourself
}

// Or separate bodies based on isStatic/isTrigger
system.separate();

Step 6: Cleaning Up

Remove bodies when they're no longer needed:

system.remove(body);

And that's it! You're now ready to utilize the Detect-Collisions library in your project.

Visual Debugging with Rendering

To facilitate debugging, Detect-Collisions allows you to visually represent the collision bodies. By invoking the draw() method and supplying a 2D context of a <canvas> element, you can draw all the bodies within a collision system. You can also opt to draw individual bodies.

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

context.strokeStyle = "#FFFFFF";
context.beginPath();
// draw specific body
body.draw(context);
// draw whole system
system.draw(context);
context.stroke();

To assess the Bounding Volume Hierarchy, you can draw the BVH.

context.strokeStyle = "#FFFFFF";
context.beginPath();
// draw specific body bounding box
body.drawBVH(context);
// draw bounding volume hierarchy of the system
system.drawBVH(context);
context.stroke();

Raycasting

Detect-Collisions provides the functionality to gather raycast data. Here's how:

const start = { x: 0, y: 0 };
const end = { x: 0, y: -10 };
const hit = system.raycast(start, end, (body, ray) => {
  // if you don't want the body to be hit by raycast return false
  return true;
});

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

  console.log({ point, body });
}

In this example, point is a Vector with the coordinates of the nearest intersection, and body is a reference to the closest body.

Usage in Browsers

Just do what I did here, import from proper cdn as module, and v'oila:

https://code.pietal.dev/#/boilerplate/detect-collisions?pans=html,console

Contributing to the Project

We welcome contributions! Feel free to open a merge request. When doing so, please adhere to the following code style guidelines:

  • Execute the npm run precommit script prior to submitting your merge request
  • Follow the conventional commits standard
  • Refrain from using the any type

Additional Considerations

Why not use a physics engine?

While physics engines like Matter-js or Planck.js are recommended for projects that need comprehensive physics simulation, not all projects require such complexity. In fact, using a physics engine solely for collision detection can lead to unnecessary overhead and complications due to built-in assumptions (gravity, velocity, friction, etc.). Detect-Collisions is purpose-built for efficient and robust collision detection, making it an excellent choice for projects that primarily require this functionality. It can also serve as the foundation for a custom physics engine.

Benchmark

This will provide you with the results of both the insertion test benchmark and a headless Stress Demo benchmark, featuring moving bodies, with increasing amounts in each step.

git clone https://github.com/Prozi/detect-collisions.git
cd detect-collisions
npm i && npm run build # will build & run tests & run benchmarks

License

MIT

You can buy me a coffee

https://paypal.me/jacekpietal

9.24.0

1 month ago

9.20.0

2 months ago

9.20.1

2 months ago

9.23.0

2 months ago

9.23.1

2 months ago

9.23.2

2 months ago

9.19.0

2 months ago

9.22.0

2 months ago

9.19.1

2 months ago

9.18.0

2 months ago

9.18.1

2 months ago

9.21.0

2 months ago

9.16.0

3 months ago

9.15.1

3 months ago

9.15.2

3 months ago

9.15.3

3 months ago

9.15.0

4 months ago

9.17.0

3 months ago

9.14.0

4 months ago

9.14.1

4 months ago

9.13.0

4 months ago

9.12.0

4 months ago

9.9.0

5 months ago

9.9.1

5 months ago

9.9.4

5 months ago

9.9.2

5 months ago

9.9.3

5 months ago

9.11.1

5 months ago

9.11.2

5 months ago

9.11.0

5 months ago

9.10.0

5 months ago

9.8.0

7 months ago

9.5.7

7 months ago

9.5.6

7 months ago

9.6.0

7 months ago

9.7.2

7 months ago

9.7.1

7 months ago

9.7.0

7 months ago

9.5.5

8 months ago

9.5.4

8 months ago

9.5.3

9 months ago

9.5.2

9 months ago

9.5.1

9 months ago

9.5.0

9 months ago

9.4.4

9 months ago

9.4.3

9 months ago

9.4.2

9 months ago

9.4.1

9 months ago

9.4.0

10 months ago

9.3.3

10 months ago

9.3.2

10 months ago

9.2.7

10 months ago

9.2.6

10 months ago

9.2.5

10 months ago

9.3.1

10 months ago

9.3.0

10 months ago

9.2.4

10 months ago

9.2.3

12 months ago

9.2.2

1 year ago

9.2.1

1 year ago

8.1.0

1 year ago

9.0.0

1 year ago

9.1.1

1 year ago

9.1.0

1 year ago

8.0.5

2 years ago

8.0.4

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

7.0.5

2 years ago

7.0.4

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.8.1

2 years ago

6.8.0

2 years ago

6.6.9

2 years ago

6.7.0

2 years ago

6.7.2

2 years ago

6.7.1

2 years ago

6.7.3

2 years ago

6.6.11

2 years ago

6.6.12

2 years ago

6.6.10

2 years ago

6.5.0

2 years ago

6.6.1

2 years ago

6.6.0

2 years ago

6.6.3

2 years ago

6.6.2

2 years ago

6.6.5

2 years ago

6.6.4

2 years ago

6.6.7

2 years ago

6.6.6

2 years ago

6.6.8

2 years ago

6.5.2

2 years ago

6.5.1

2 years ago

6.5.4

2 years ago

6.5.3

2 years ago

6.5.6

2 years ago

6.5.5

2 years ago

6.5.8

2 years ago

6.5.7

2 years ago

6.1.0

2 years ago

6.3.0

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.4.2

2 years ago

6.3.10

2 years ago

6.3.11

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.3.14

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.0.2

2 years ago

6.4.1

2 years ago

6.3.12

2 years ago

6.4.0

2 years ago

6.3.13

2 years ago

6.2.2

2 years ago

6.3.4

2 years ago

6.3.3

2 years ago

6.3.6

2 years ago

6.3.5

2 years ago

6.3.8

2 years ago

6.3.7

2 years ago

6.3.9

2 years ago

5.0.1

3 years ago

5.0.0

3 years ago

3.4.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.2

3 years ago

3.3.0

3 years ago

3.5.0

3 years ago

3.2.7

3 years ago

3.2.6

3 years ago

3.2.2

3 years ago

3.1.3

3 years ago

3.2.1

3 years ago

3.1.2

3 years ago

3.2.0

3 years ago

3.2.5

3 years ago

3.2.4

3 years ago

3.2.3

3 years ago

2.7.0

3 years ago

3.0.9

3 years ago

3.0.12

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.1.1

3 years ago

3.0.10

3 years ago

3.0.2

3 years ago

3.1.0

3 years ago

3.0.11

3 years ago

3.0.1

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

2.6.0

3 years ago

2.5.6

3 years ago

2.5.5

3 years ago

2.5.4

4 years ago

2.5.3

4 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.4

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago