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.
A 2D collision checker for modern JavaScript games.
Table of Contents
Note, this project uses https://github.com/jriecken/sat-js as a foundation and builds on it. I will actively send updates that I find useful to the original repository as I find it to be a great resource still.
If you find collider2d useful please check out the original repository and give credit to the author.
To install collider2d, use:
$ npm install collider2d
If you're using collider2d in a browser environment, you can use the collider2d.js
file found in the root directory or just a reference to the package if you're using webpack.
browser
// Non-webpack environment:
import Collider2D from '../node_modules/collider2d/collider2d.js';
// Webpack/other bundler environment:
import Collider2D from 'collider2d';
and in a Node environment, you can imply require the module:
node
const Collider2D = require('collider2d');
lastly you just have to make a new instance of Collider2D like so:
const c2d = new Collider2D();
collider2d revolves around points and shapes, more specifically vectors, circles, and polygons.
A vector is a point in 2D space that has a x and y position.
property | type | description | default |
---|---|---|---|
x | number | The x position of the vector. | 0 |
y | number | The y position of the vector. | 0 |
example:
const vec1 = c2d.vector(10, 25);
To see the full list of properties and methods for circles, check out the vector documentation.
A circle consists of a center position and a radius.
property | type | description | default |
---|---|---|---|
position | Vector | A vector representing the center of the circle. | vector(0, 0) |
radius | number | The radius of the circle. | 0 |
example:
const circle = c2d.circle(c2d.vector(5, 5), 10);
To see the full list of properties and methods for circles, check out the circles documentation.
A polygon consists of a convex shape with any number of points (specified in a counter-clockwise order).
property | type | description | default |
---|---|---|---|
position | Vector | A vector representing the origin of the polygon (all other points are relative to this one). | vector(0, 0) |
points | Array | An array of vectors representing the points of the polygon, in counter-clockwise order. | 0 |
example:
const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
]);
To see the full list of properties and methods for polygons, check out the polygon documentation.
A box represents an axis-aligned bounding box with a width and a height.
property | type | description | default |
---|---|---|---|
position | Vector | A vector representing the position of this box. | vector(0, 0) |
width | number | The width of this box. | 0 |
height | number | The height of this box. | 0 |
example:
const box = c2d.box(c2d.vector(5, 10), 100, 250);
To see the full list of properties and methods for boxes, check out the box documentation.
There are three different types of collisions: point, circle, and polygon collisions.
Some collisions (testCircleCircle, testPolygonPolygon, testPolygonCircle, testCirclePolygon) have a details
parameter that can be used to return extra details about the collision, if there is one.
The structure of the collision details object looks like:
property | type | description | default |
---|---|---|---|
a | Vector,Circle,Polygon | The first collision object | |
b | Vector,Circle,Polygon | The second collision object | |
overlapN | Vector | A unit vector representing the direction and magnitude of the overlap. | |
overlapV | Vector | A vector representing the minimum change necessary to extract the first object from the second one. | |
overlap | number | The amount that is overlapping. | |
aInB | boolean | Returns true if the first collision object is completely in the second collision object. | |
bInA | boolean | Returns true if the second collision object is completely in the first collision object. |
Note that if you pass in true for the detail parameter and there is a collision, there will not be a boolean returned but the collision details object instead. If there is no collision then it will still return false.
Checks to see if a point is inside of a circle.
Returns true if the point is in the circle or false otherwise.
property | type | description | default |
---|---|---|---|
point | Vector | The point to test. | |
circle | Circle | The circle to test. |
example:
const circle = c2d.circle(c2d.vector(100, 100), 20);
const collision = c2d.pointInCircle(c2d.vector(110, 110), circle); // true
Checks to see if a point is inside of a convex polygon.
Returns true if the point is in the polygon or false otherwise.
property | type | description | default |
---|---|---|---|
point | Vector | The point to test. | |
polygon | Polygon | The polygon to test. |
example:
const triangle = c2d.polygon(c2d.vector(30, 0), [
c2d.vector(0, 0),
c2d.vector(30, 0),
c2d.vector(0, 30)
]);
const collision = c2d.pointInPolygon(c2d.vector(35, 5), triangle); // true
Checks whether polygons collide.
Returns true if the circles collide or false otherwise. If details is set to true and a collision occurs then a collision details object will be returned instead of true.
property | type | description | default |
---|---|---|---|
a | Polygon | The first polygon. | |
b | Polygon | The second polygon. | |
details | boolean | If set to true and there is a collision, an object highlighting details about the collision will be returned instead of just returning true |
example:
const polygon1 = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
]);
const polygon2 = c2d.polygon(c2d.vector(30, 0), [
c2d.vector(0, 0),
c2d.vector(30, 0),
c2d.vector(0, 30)
]);
// No details
const collided = c2d.testPolygonPolygon(polygon1, polygon2); // true
// Details
const collidedDetails = c2d.testPolygonPolygon(polygon1, polygon2, true);
console.log(collidedDetails.overlap); // 10
Check if a polygon and a circle collide.
Returns true if the circles collide or false otherwise. If details is set to true and a collision occurs then a collision details object will be returned instead of true.
property | type | description | default |
---|---|---|---|
polygon | Polygon | The polygon to check. | |
circle | Circle | The circle to check. | |
details | boolean | If set to true and there is a collision, an object highlighting details about the collision will be returned instead of just returning true |
example:
const circle = c2d.circle(c2d.vector(50, 50), 20);
const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
]);
// No details
const collided = c2d.testPolygonCircle(polygon, circle); // true
// Details
const collidedDetails = c2d.testPolygonCircle(polygon, circle, true);
console.log(collidedDetails.overlap.toFixed(2)); // 5.86
Check if two circles collide.
Returns true if the circles collide or false otherwise. If details is set to true and a collision occurs then a collision details object will be returned instead of true.
property | type | description | default |
---|---|---|---|
a | Circle | The first circle. | |
b | Circle | The second circle. | |
details | boolean | If set to true and there is a collision, an object highlighting details about the collision will be returned instead of just returning true |
example:
const circle1 = c2d.circle(c2d.vector(0, 0), 20);
const circle2 = c2d.circle(c2d.vector(30, 0), 20);
// No details
const collided = c2d.testCircleCircle(circle1, circle2); // true
// Details
const collidedDetails = c2d.testCircleCircle(circle1, circle2, true);
console.log(collidedDetails.overlap); // 10
Check if a circle and a polygon collide.
NOTE: This is slightly less efficient than polygonCircle as it just runs polygonCircle and reverses everything at the end.
Returns true if the circles collide or false otherwise. If details is set to true and a collision occurs then a collision details object will be returned instead of true.
property | type | description | default |
---|---|---|---|
circle | Circle | The circle to check. | |
polygon | Polygon | The polygon to check. | |
details | boolean | If set to true and there is a collision, an object highlighting details about the collision will be returned instead of just returning true |
example:
const circle = c2d.circle(c2d.vector(50, 50), 20);
const polygon = c2d.polygon(c2d.vector(0, 0), [
c2d.vector(0, 0),
c2d.vector(40, 0),
c2d.vector(40, 40),
c2d.vector(0, 40)
]);
// No details
const collided = c2d.testCirclePolygon(circle, polygon); // true
// Details
const collidedDetails = c2d.testCirclePolygon(circle, polygon, true);
To run the tests available for collider2d use:
$ npm run test
MIT
0.1.0 / 2020-01-16
FAQs
A 2D collision checker for modern JavaScript games.
The npm package collider2d receives a total of 87 weekly downloads. As such, collider2d popularity was classified as not popular.
We found that collider2d demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.