Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
detect-collisions
Advanced tools
Points, Lines, Boxes, Polygons (also hollow), Ellipses, Circles. RayCasting, offsets, rotation, scaling, bounding box padding, flags for static and ghost/trigger bodies
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.
npm i detect-collisions --save
For detailed documentation on the library's API, refer to the following link:
Detect-Collisions API Documentation
Initialize a unique collision system using Detect-Collisions:
const { System } = require("detect-collisions");
const system = new System();
Bodies possess various properties:
setPosition(x: number, y: number)
for teleport and move(speed: number)
for moving forward in direction of its angle.setScale(x: number, y: number)
for setting and scale: Vector
for getting scalesetAngle(radians: number)
for setting and angle: number
for getting and deg2rad(degrees: number)
to convert to radians.setOffset(offset: Vector)
for setting and offset: Vector
for getting offset from the body center.aabb: BBox
for inserted or getAABBAsBBox(): BBox
for non inserted bodies to get the bounding box.padding: number
and set to nonzero value to reduce costly reinserts on attributes' change.group: number
for collision filtering, with a range within 0x0 ~ 0x7fffffff.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);
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
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();
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.
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();
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.
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
We welcome contributions! Feel free to open a merge request. When doing so, please adhere to the following code style guidelines:
npm run precommit
script prior to submitting your merge requestany
typeWhile 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.
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
MIT
FAQs
Points, Lines, Boxes, Polygons (also hollow), Ellipses, Circles. RayCasting, offsets, rotation, scaling, bounding box padding, flags for static and ghost/trigger bodies
The npm package detect-collisions receives a total of 3,837 weekly downloads. As such, detect-collisions popularity was classified as popular.
We found that detect-collisions demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.