JS: SAT-js
more at:
https://github.com/jriecken/sat-js
HTML
<canvas id='canvas' />
CSS
html,body{margin:0; padding:0}
canvas {display:block}
JavaScript
/** @preserve @author Jim Riecken - released under the MIT License. */
/**
* A simple library for determining intersections of circles and
* polygons using the Separating Axis Theorem.
*/
/*jshint shadow:true, sub:true, forin:true, noarg:true, noempty:true,
eqeqeq:true, bitwise:true, strict:true, undef:true,
curly:true, browser:true */
(function (window, SAT) {
// Math caching
var abs = Math.abs;
var sqrt = Math.sqrt;
/**
* Represents a vector in two dimensions.
*
* @param {Number} x The x position.
* @param {Number} y The y position.
* @constructor
*/
var Vector = function (x, y) {
this.x = x || 0;
this.y = y || x || 0;
};
SAT.Vector = Vector;
/**
* Copy the values of another Vector into this one.
*
* @param {Vector} other The other Vector.
* @return {Vector} This for chaining.
*/
Vector.prototype.copy = function (other) {
this.x = other.x;
this.y = other.y;
return this;
};
/**
* Rotate this vector by 90 degrees
*
* @return {Vector} This for chaining.
*/
Vector.prototype.perp = function () {
var x = this.x;
this.x = this.y;
this.y = -x;
return this;
};
/**
* Reverse this vector.
*
* @return {Vector} This for chaining.
*/
Vector.prototype.reverse = function () {
this.x = -this.x;
this.y = -this.y;
return this;
};
/**
* Normalize (make unit length) this vector.
*
* @return {Vector} This for chaining.
*/
Vector.prototype.normalize = function () {
var len = this.len();
if (len > 0) {
this.x = this.x / len;
this.y = this.y / len;
}
return this;
};
/**
* Add another vector to this one.
*
* @param {Vector} other The other Vector.
* @return {Vector} This for chaining.
*/
...