diff --git a/README.md b/README.md index 90ee91d..0a75b76 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Crash ===== -v.1.0.0 +v.1.1.0 Crash performs optimized 2D collisions, powered by [RBush] and [SAT.js], written in javascript. It's most obvious use-case is in game engines, but it's flexible enough to be used anywhere. @@ -33,8 +33,8 @@ Crash is perfectly happy in the browser and on Node.js. ## Installation At the moment, package managers are not yet set up. Just download one of the following files from this repo and load it in your project. -1. [crash.js]: full source, with comments and all (12.8kB). -2. [crash.min.js]: minified, ready to be used in production (5.3kB or 1.7kB gzipped). +1. [crash.js]: full source, with comments and all (13.6kB). +2. [crash.min.js]: minified, ready to be used in production (5.6kB or 1.8kB gzipped). When you have installed Crash, head over to the [Getting Started section][getting-started]. diff --git a/crash.js b/crash.js index 17429dd..7b0f463 100644 --- a/crash.js +++ b/crash.js @@ -1,5 +1,5 @@ // Crash -// Version 1.0.0 - Copyright 2015 - Tuur Dutoit +// Version 1.1.0 - Copyright 2015 - Tuur Dutoit // // Released under the MIT License - https://github.com/TuurDutoit/crash // diff --git a/crash.min.js b/crash.min.js index 48255d2..292d761 100644 --- a/crash.min.js +++ b/crash.min.js @@ -1 +1 @@ -!function(a){"use strict";"function"==typeof define&&define.amd?define(["RBush","SAT"],a):"object"==typeof exports?module.exports=a(require("rbush"),require("sat")):window.Crash=a(window.rbush,window.SAT)}(function(a,b){"use strict";var c={RBush:a,SAT:b,Vector:b.Vector,V:b.Vector,Response:b.Response,maxChecks:100,rbush:null,RESPONSE:new b.Response,BREAK:!1,__listeners:[],__notYetInserted:[],__moved:[]};c.extend=function(a,b){a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})},c.reset=function(a){return this.clear(),this.__listeners=[],this.BREAK=!1,this.maxChecks=100,this.RESPONSE.clear(),this.init(a),this},c.onCollision=function(a){return this.__listeners.push(a),this},c.offCollision=function(a){var b=this.__listeners.indexOf(a);return b>-1&&this.__listeners.splice(b,1),this},c.__onCollision=function(a,b,c){for(var d=0,e=this.__listeners.length;e>d;d++)this.__listeners[d](a,b,c,this.cancel);return this},c.cancel=function(){return this.BREAK=!0,!1},c.getTestString=function(a,b){return"circle"===a?"circle"===b?"testCircleCircle":"testCirclePolygon":"circle"===b?"testPolygonCircle":"testPolygonPolygon"},c.init=function(b){this.rbush=new a(b||9,[".aabb.x1",".aabb.y1",".aabb.x2",".aabb.y2"]);for(var c=0,d=this.__notYetInserted.length;d>c;c++)this.rbush.insert(this.__notYetInserted.pop());return this},c.insert=function(a){return this.rbush?this.rbush.insert(a):this.__notYetInserted.push(a),this},c.remove=function(a){if(this.rbush)this.rbush.remove(a);else{var b=this.__notYetInserted.indexOf(a);b>-1&&this.__notYetInserted.splice(b,1)}return this},c.all=function(){return this.rbush?this.rbush.all():this.__notYetInserted},c.search=function(a){if(this.rbush){var b=this.rbush.search([a.aabb.x1,a.aabb.y1,a.aabb.x2,a.aabb.y2]),c=b.indexOf(a);return c>-1&&b.splice(c,1),b}return[]},c.clear=function(){return this.rbush&&this.rbush.clear(),this.__moved=[],this.__notYetInserted=[],this},c.moved=function(a){return-1===this.__moved.indexOf(a)&&this.__moved.push(a),this},c.update=function(a){return this.updateAABB(a),this.remove(a),this.insert(a),this},c.updateAABB=function(a){switch(a.type){case"polygon":return c.updateAABBPolygon(a);case"box":return c.updateAABBBox(a);case"circle":return c.updateAABBCircle(a);case"point":return c.updateAABBPoint(a)}},c.updateAABBPolygon=function(a){for(var b=a.aabb,c=a.sat.pos,d=a.sat.calcPoints,e=d.length,f=d[0].x,g=d[0].y,h=d[0].x,i=d[0].y,j=1;e>j;j++){var k=d[j];k.xh&&(h=k.x),k.yi&&(i=k.y)}b.x1=c.x+f,b.y1=c.y+g,b.x2=c.x+h,b.y2=c.y+i},c.updateAABBBox=function(a){var b=a.sat.calcPoints,c=a.aabb;c.x1=b[0].x,c.y1=b[0].y,c.x2=b[2].x,c.y2=b[2].y},c.updateAABBCircle=function(a){var b=a.aabb,c=a.sat.r,d=a.sat.pos;b.x1=d.x-c,b.y1=d.y-c,b.x2=d.x+c,b.y2=d.y+c},c.updateAABBPoint=function(a){var b=a.aabb,c=a.sat.pos;b.x1=b.x2=c.x,b.y1=b.y2=c.x},c.test=function(a,c,d){var d=d||this.RESPONSE,e=this.getTestString(a.type,c.type);return d.clear(),b[e](a.sat,c.sat,d)},c.testAll=function(a,c){var c=c||this.RESPONSE;this.update(a);var d=this.search(a);a:for(var e=0,f=d.length;f>e;e++){var g=d[e],h=this.getTestString(a.type,g.type);if(c.clear(),b[h](a.sat,g.sat,c)&&Math.abs(c.overlap)>.5&&(this.__onCollision(a,g,c),this.BREAK))break a}return a.lastPos.x=a.sat.pos.x,a.lastPos.y=a.sat.pos.y,this.BREAK=!1,this},c.check=function(a){for(var b=0;this.__moved.length&&bc;c++)this.testAll(b[c],a);return this.check(a),this};var d=c.Collider=function(a,b,d,e){return this.type=a,this.sat=b,this.data=e,this.pos=this.sat.pos,this.lastPos=this.pos.clone(),this.lastCheckedPos=this.pos.clone(),this.aabb={},c.updateAABB(this),d&&c.insert(this),this};d.prototype.insert=function(){return c.insert(this),this},d.prototype.remove=function(){return c.remove(this),this},d.prototype.update=function(){return c.update(this),this},d.prototype.updateAABB=function(){return c.updateAABB(this),this},d.prototype.moved=function(){return c.moved(this),this},d.prototype.search=function(){return c.search(this)},d.prototype.setData=function(a){return this.data=a,this},d.prototype.getData=function(){return this.data},d.prototype.moveTo=function(a,b){return this.sat.pos.x=a,this.sat.pos.y=b,this.moved(),this},d.prototype.moveBy=d.prototype.move=function(a,b){return this.sat.pos.x+=a,this.sat.pos.y+=b,this.moved(),this};var e=c.Polygon=function(a,c,e,f){var g=new b.Polygon(a,c);return d.call(this,"polygon",g,e,f),this};c.extend(e,d),e.prototype.setPoints=function(a){return this.sat.setPoints(a),this.moved(),this},e.prototype.setAngle=function(a){return this.sat.setAngle(a),this.moved(),this},e.prototype.setOffset=function(a){return this.sat.setOffset(a),this.moved(),this},e.prototype.rotate=function(a){return this.sat.rotate(a),this.moved(),this};var f=c.Circle=function(a,c,e,f){var g=new b.Circle(a,c);return d.call(this,"circle",g,e,f),this};c.extend(f,d);var g=c.Point=function(a,c,e){var f=new b.Box(a,1,1).toPolygon();return d.call(this,"point",f,c,e),this};c.extend(g,d);var h=c.Box=function(a,c,e,f,g){var h=new b.Box(a,c,e).toPolygon();return d.call(this,"box",h,f,g),this};return c.extend(h,d),c}); \ No newline at end of file +!function(a){"use strict";"function"==typeof define&&define.amd?define(["RBush","SAT"],a):"object"==typeof exports?module.exports=a(require("rbush"),require("sat")):window.Crash=a(window.rbush,window.SAT)}(function(a,b){"use strict";var c={RBush:a,SAT:b,Vector:b.Vector,V:b.Vector,Response:b.Response,rbush:null,RESPONSE:new b.Response,BREAK:!1,MAX_CHECKS:100,OVERLAP_LIMIT:.5,__listeners:[],__notYetInserted:[],__moved:[]};c.extend=function(a,b){a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})},c.reset=function(a){return this.clear(),this.__listeners=[],this.BREAK=!1,this.MAX_CHECKS=100,this.OVERLAP_LIMIT=.5,this.RESPONSE.clear(),this.init(a),this},c.onCollision=function(a){return this.__listeners.push(a),this},c.offCollision=function(a){var b=this.__listeners.indexOf(a);return b>-1&&this.__listeners.splice(b,1),this},c.__onCollision=function(a,b,c){for(var d=0,e=this.__listeners.length;e>d;d++)this.__listeners[d].call(this,a,b,c,this.cancel);return this},c.cancel=function(){return this.BREAK=!0,!1},c.getTestString=function(a,b){return"circle"===a?"circle"===b?"testCircleCircle":"testCirclePolygon":"circle"===b?"testPolygonCircle":"testPolygonPolygon"},c.init=function(b){this.rbush=new a(b||9,[".aabb.x1",".aabb.y1",".aabb.x2",".aabb.y2"]);for(var c=0,d=this.__notYetInserted.length;d>c;c++)this.rbush.insert(this.__notYetInserted.pop());return this},c.insert=function(a){return this.rbush?this.rbush.insert(a):this.__notYetInserted.push(a),this},c.remove=function(a){if(this.rbush)this.rbush.remove(a);else{var b=this.__notYetInserted.indexOf(a);b>-1&&this.__notYetInserted.splice(b,1)}return this},c.all=function(){return this.rbush?this.rbush.all():this.__notYetInserted};var d=[];c.search=function(a){if(this.rbush){d[0]=a.aabb.x1,d[1]=a.aabb.y1,d[2]=a.aabb.x2,d[3]=a.aabb.y2;var b=this.rbush.search(d),c=b.indexOf(a);return c>-1&&b.splice(c,1),b}return[]},c.clear=function(){return this.rbush&&this.rbush.clear(),this.__moved=[],this.__notYetInserted=[],this},c.addToMoved=function(a){return-1===this.__moved.indexOf(a)&&this.__moved.push(a),this},c.update=function(a){return this.updateAABB(a),this.remove(a),this.insert(a),this},c.moved=function(a){return this.update(a),this.addToMoved(a),this},c.updateAABB=function(a){switch(a.type){case"polygon":return c.updateAABBPolygon(a);case"box":return c.updateAABBBox(a);case"circle":return c.updateAABBCircle(a);case"point":return c.updateAABBPoint(a)}},c.updateAABBPolygon=function(a){for(var b=a.aabb,c=a.sat.pos,d=a.sat.calcPoints,e=d.length,f=d[0].x,g=d[0].y,h=d[0].x,i=d[0].y,j=1;e>j;j++){var k=d[j];k.xh&&(h=k.x),k.yi&&(i=k.y)}b.x1=c.x+f,b.y1=c.y+g,b.x2=c.x+h,b.y2=c.y+i},c.updateAABBBox=function(a){var b=a.sat.calcPoints,c=a.aabb;c.x1=b[0].x,c.y1=b[0].y,c.x2=b[2].x,c.y2=b[2].y},c.updateAABBCircle=function(a){var b=a.aabb,c=a.sat.r,d=a.sat.pos;b.x1=d.x-c,b.y1=d.y-c,b.x2=d.x+c,b.y2=d.y+c},c.updateAABBPoint=function(a){var b=a.aabb,c=a.sat.pos;b.x1=b.x2=c.x,b.y1=b.y2=c.x},c.test=function(a,c,d){var d=d||this.RESPONSE,e=this.getTestString(a.type,c.type);return d.clear(),b[e](a.sat,c.sat,d)},c.testAll=function(a,c){var c=c||this.RESPONSE,d=this.search(a);a:for(var e=0,f=d.length;f>e;e++){var g=d[e],h=this.getTestString(a.type,g.type);if(c.clear(),b[h](a.sat,g.sat,c)&&(this.OVERLAP_LIMIT&&Math.abs(c.overlap)>this.OVERLAP_LIMIT||!this.OVERLAP_LIMIT)&&(this.__onCollision(a,g,c),this.BREAK))break a}a.lastPos.copy(a.pos);var i=this.BREAK;return this.BREAK=!1,!i};var e=[];c.check=function(a){for(var b=0;this.__moved.length&&bb;b++)e[b].lastCheckedPos.copy(e[b].pos);return e.splice(0,e.length),this},c.checkAll=function(a){for(var b=this.all(),c=0,d=b.length;d>c;c++)this.testAll(b[c],a);return this.check(a),this};var f=c.Collider=function(a,b,d,e){return this.type=a,this.sat=b,this.data=e,this.pos=this.sat.pos,this.lastPos=this.pos.clone(),this.lastCheckedPos=this.pos.clone(),this.aabb={},c.updateAABB(this),d&&c.insert(this),this};f.prototype.insert=function(){return c.insert(this),this},f.prototype.remove=function(){return c.remove(this),this},f.prototype.update=function(){return c.update(this),this},f.prototype.updateAABB=function(){return c.updateAABB(this),this},f.prototype.moved=function(){return c.moved(this),this},f.prototype.search=function(){return c.search(this)},f.prototype.setData=function(a){return this.data=a,this},f.prototype.getData=function(){return this.data},f.prototype.moveTo=function(a,b){return this.sat.pos.x=a,this.sat.pos.y=b,this.moved(),this},f.prototype.moveBy=f.prototype.move=function(a,b){return this.sat.pos.x+=a,this.sat.pos.y+=b,this.moved(),this};var g=c.Polygon=function(a,c,d,e){var g=new b.Polygon(a,c);return f.call(this,"polygon",g,d,e),this};c.extend(g,f),g.prototype.setPoints=function(a){return this.sat.setPoints(a),this.moved(),this},g.prototype.setAngle=function(a){return this.sat.setAngle(a),this.moved(),this},g.prototype.setOffset=function(a){return this.sat.setOffset(a),this.moved(),this},g.prototype.rotate=function(a){return this.sat.rotate(a),this.moved(),this};var h=c.Circle=function(a,c,d,e){var g=new b.Circle(a,c);return f.call(this,"circle",g,d,e),this};c.extend(h,f);var i=c.Point=function(a,c,d){var e=new b.Box(a,1,1).toPolygon();return f.call(this,"point",e,c,d),this};c.extend(i,f);var j=c.Box=function(a,c,d,e,g){var h=new b.Box(a,c,d).toPolygon();return f.call(this,"box",h,e,g),this};return c.extend(j,f),c}); \ No newline at end of file diff --git a/package.json b/package.json index 5b2fe20..695d3ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crash-colliders", - "version": "1.0.0", + "version": "1.1.0", "description": "Crash performs optimized 2D collisions, powered by RBush and SAT.js, written in javascript.", "main": "crash.js", "dependencies": {