0.1.0 • Published 6 years ago

swift-collider v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

NPM version Known Vulnerabilities npm NPM downloads Gitter

Table of Contents

Prologue

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 swift-collider useful please check out the original repository and give credit to the author.

Install

To install swift-collider, use:

$ npm install swift-collider

Initialization

If you're using swift-collider in a browser environment, you can use the swift-collider.js file found in the root directory or just a reference to the package if you're using webpack.

browser

// Non-webpack environment:
import SwiftCollider from '../node_modules/swift-collider/swift-collider.js';

// Webpack/other bundler environment:
import SwiftCollider from 'swift-collider';

and in a Node environment, you can imply require the module:

node

const SwiftCollider = require('swift-collider');

lastly you just have to make a new instance of SwiftCollider like so:

const sc = new SwiftCollider();

Geometry

swift-collider revolves around points and shapes, more specifically vectors, circles, and polygons.

Vector

A vector is a point in 2D space that has a x and y position.

propertytypedescriptiondefault
xnumberThe x position of the vector.0
ynumberThe y position of the vector.0

example:

const vec1 = sc.vector(10, 25);

To see the full list of properties and methods for circles, check out the vector documentation.

Circle

A circle consists of a center position and a radius.

propertytypedescriptiondefault
positionVectorA vector representing the center of the circle.vector(0, 0)
radiusnumberThe radius of the circle.0

example:

const circle = sc.circle(sc.vector(5, 5), 10);

To see the full list of properties and methods for circles, check out the circles documentation.

Polygon

A polygon consists of a convex shape with any number of points (specified in a counter-clockwise order).

propertytypedescriptiondefault
positionVectorA vector representing the origin of the polygon (all other points are relative to this one).vector(0, 0)
pointsArrayAn array of vectors representing the points of the polygon, in counter-clockwise order.0

example:

const polygon = sc.polygon(sc.vector(0, 0), [
  sc.vector(0, 0),
  sc.vector(40, 0),
  sc.vector(40, 40),
  sc.vector(0, 40)
]);

To see the full list of properties and methods for polygons, check out the polygon documentation.

Box

A box represents an axis-aligned bounding box with a width and a height.

propertytypedescriptiondefault
positionVectorA vector representing the position of this box.vector(0, 0)
widthnumberThe width of this box.0
heightnumberThe height of this box.0

example:

const box = sc.box(sc.vector(5, 10), 100, 250);

To see the full list of properties and methods for boxes, check out the box documentation.

Collisions

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:

propertytypedescriptiondefault
aVector,Circle,PolygonThe first collision object
bVector,Circle,PolygonThe second collision object
overlapNVectorA unit vector representing the direction and magnitude of the overlap.
overlapVVectorA vector representing the minimum change necessary to extract the first object from the second one.
overlapnumberThe amount that is overlapping.
aInBbooleanReturns true if the first collision object is completely in the second collision object.
bInAbooleanReturns 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.

Point Collisions

pointInCircle

Checks to see if a point is inside of a circle.

Returns true if the point is in the circle or false otherwise.

propertytypedescriptiondefault
pointVectorThe point to test.
circleCircleThe circle to test.

example:

const circle = sc.circle(sc.vector(100, 100), 20);

const collision = sc.pointInCircle(sc.vector(110, 110), circle); // true

pointInPolygon

Checks to see if a point is inside of a convex polygon.

Returns true if the point is in the polygon or false otherwise.

propertytypedescriptiondefault
pointVectorThe point to test.
polygonPolygonThe polygon to test.

example:

const triangle = sc.polygon(sc.vector(30, 0), [
  sc.vector(0, 0),
  sc.vector(30, 0),
  sc.vector(0, 30)
]);

const collision = sc.pointInPolygon(sc.vector(35, 5), triangle); // true

Polygon Collisions

testPolygonPolygon

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.

propertytypedescriptiondefault
aPolygonThe first polygon.
bPolygonThe second polygon.
detailsbooleanIf 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 = sc.polygon(sc.vector(0, 0), [
  sc.vector(0, 0),
  sc.vector(40, 0),
  sc.vector(40, 40),
  sc.vector(0, 40)
]);

const polygon2 = sc.polygon(sc.vector(30, 0), [
  sc.vector(0, 0),
  sc.vector(30, 0),
  sc.vector(0, 30)
]);

// No details
const collided = sc.testPolygonPolygon(polygon1, polygon2); // true

// Details
const collidedDetails = sc.testPolygonPolygon(polygon1, polygon2, true);
console.log(collidedDetails.overlap); // 10

testPolygonCircle

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.

propertytypedescriptiondefault
polygonPolygonThe polygon to check.
circleCircleThe circle to check.
detailsbooleanIf 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 = sc.circle(sc.vector(50, 50), 20);

const polygon = sc.polygon(sc.vector(0, 0), [
  sc.vector(0, 0),
  sc.vector(40, 0),
  sc.vector(40, 40),
  sc.vector(0, 40)
]);

// No details
const collided = sc.testPolygonCircle(polygon, circle); // true

// Details
const collidedDetails = sc.testPolygonCircle(polygon, circle, true);
console.log(collidedDetails.overlap.toFixed(2)); // 5.86

Circle Collisions

testCircleCircle

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.

propertytypedescriptiondefault
aCircleThe first circle.
bCircleThe second circle.
detailsbooleanIf 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 = sc.circle(sc.vector(0, 0), 20);
const circle2 = sc.circle(sc.vector(30, 0), 20);

// No details
const collided = sc.testCircleCircle(circle1, circle2); // true

// Details
const collidedDetails = sc.testCircleCircle(circle1, circle2, true);
console.log(collidedDetails.overlap); // 10

testCirclePolygon

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.

propertytypedescriptiondefault
circleCircleThe circle to check.
polygonPolygonThe polygon to check.
detailsbooleanIf 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 = sc.circle(sc.vector(50, 50), 20);

const polygon = sc.polygon(sc.vector(0, 0), [
  sc.vector(0, 0),
  sc.vector(40, 0),
  sc.vector(40, 40),
  sc.vector(0, 40)
]);

// No details
const collided = sc.testCirclePolygon(circle, polygon); // true

// Details
const collidedDetails = sc.testCirclePolygon(circle, polygon, true);

Tests

To run the tests available for swift-collider use:

$ npm run test

License

MIT