Vinage is a collision library usable in both the browser and Node.js. It supports wrap-around.
Your JavaScript implementation needs to support Proxy
for it to work. Firefox 18+, Chrome 49+ and Node.js 6+ work.
$ npm install vinage --save
var vinage = require("vinage");
var whatsThePoint = new vinage.Point(0, 0); // use it!
<!-- add this to your html file -->
<script src="/vinage.js"></script>
// you're all set
var whatsThePoint = new vinage.Point(0, 0);
To use it, you must define instances of the classes listed below, which represent geometric objects in a 2D space. You can modify some properties, as indicated. Other properties are calculated when you read them, then cached. When accessed again, they are either retrieved or recalculated, depending on the modifications you made to other properties of the object.
var myPoint = new vinage.Point(12, 45);
console.log(myPoint.x, myPoint.y); // 12, 45
x
: the point's x-coordinate (abscissa). Writabley
: the point's y-coordinate (ordinate). Writable
An euclidean vector.
var myVectorOne = new vinage.Vector(25, 87),
myVectorTwo = new vinage.Vector(myPointOne, myPointTwo);
console.log(myVectorOne.x, myVectorOne.y, myVectorOne.orthogonalVector, myVectorOne.length); // 25, 87, Vector, 90.52071586106685
x
: the vector's x-component. Writabley
: the vector's y-component. WritableorthogonalVector
: a vector which is orthogonal to the vectorlength
: the length of the vector
dotProduct(vector)
: returns the dot product of the vector and the other vector passed as a parameternormalize()
: make the vector into a unit vectorapply(point)
: adds the vector topoint
's coordinates
var myCircle = new vinage.Circle(myPoint, 45);
center
: a point, the center of the circleradius
: the circle's radius. Writable
var myRectangleOne = new vinage.Rectangle(myPoint, 45, 87, Math.PI/7),
myRectangleTwo = new vinage.Rectangle(myPoint, 97, 54);
center
: a point, the center of the rectanglewidth
: the width of the rectangle. Writableheight
: the height of the rectangle. Writableangle
: the angle defining the orientation of the rectangle. Writablevertices
: an array of points, which are the vertices of the rectangle
project(vector)
: returns the projection of the rectangle against an axisforget()
: vinage may prevent rectangles from being garbage collected1. If you don't want to use a rectangle anymore, for example before usingdelete
on it or replacing it with something else, please use this method. Do not use an object this method has been used on.
If you want to check for collision, you must first define a universe where collisions happen. Universes are mere instances of Rectangle.
Most of the time you will want them to happen in an Cartesian plane (i.e. an infinite universe). You can create it like this:
var myUniverse = new vinage.Rectangle(new Point(0, 0), Infinity, Infinity);
However, if you want to implement wrap-around, you will need a bounded and finite universe:
var myUniverse = new vinage.Rectangle(new Point(0, 0), 1000, 1000);
If your universe is finite and one of your geometric object overlap the "edge" of the universe, it will be as if this part of it was actually on the other side of the universe. Note that your geometric objects can be beyond the limits of the universe; tests are internally remapped inside the universe.
var myRectangle = new vinage.Rectangle(238, 153, 50, 50),
myCircle = new vinage.Circle(new Point(-56, 27), 12),
myUniverse = new vinage.Rectangle(new Point(-25, 0), 100, 100);
// it's valid to check whether myRectangle and myCircle collide under myUniverse
You can check for collisions under your universe like this:
myUniverse.collide(myRectangle, myCircle); // returns true or false
Vinage relies on the Proxy object (introduced in ES6) to cache data. Every instance of the above classes is actually a proxy to a regular object. When a modification is made to a writable property, the proxy object searches for read-only properties relying on it, and keep a reminder that they must be calculated again. The next time one of those read-only properties will be retrieved, it will be recalculated and recached.
var vec = new Vector(25, 87); // vec is actually a proxy
console.log(vec.length); // vec.length is caclulated based on vec.x and vec.y, then stored, and finally returned.
var someVar = 1 + vec.length; // vec.length is up-to-date, it is only read from the cache
vec.x = 64; // the vec.length and vec.orthogonalVector depend on the value of vec.x, they are both set to be refreshed
console.log(vec.length); // vec.length is set to be refreshed, therefore it is recaculated, then stored, and finally returned
Every instance has a _upToDate
property, where it is stored in an object (used as a map) whether properties must be calculated (a boolean is used).
Every instance has a _cache
property, an object (used as a map) where calculated values are stored.
Vector
and Rectangle
instances have a _proxyMap
property, inherited from their prototype, an object (used as a map) which contains for each modifiable variable, the list of the calculated variable which will have to be refreshed.
Point
instances have a _proxyMap
property, inherited from their prototype, an object (used as a map) which contains for each modifiable variable, the list of the calculated variable which will have to be refreshed on the parent. That list takes into account the parent's type.
// from Vinage's code
Vector.prototype._proxyMap = {
x: ['orthogonalVector', 'length'], // if myVec.x is modified, myVec._upToDate.orthogonalVector and myVec._upToDate.length will both be set to false
y: ['orthogonalVector', 'length'] // if myVec.y is modified, myVec._upToDate.orthogonalVector and myVec._upToDate.length will both be set to false
};
You can open graphical-tester.svg
in your browser and move around elements to check if collisions work as intended.
When the library reports a collision the colliding elements are red, otherwise they are green.
1: This happens when an object is child of multiple parents (for example a point used as the center of differents rectangles), because the child keeps a reference to each parent.