This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
von-physics3.min.js
1 lines (1 loc) · 29.1 KB
/
von-physics3.min.js
1
(function(){var t=this,i={Boundary:{BOUNDARY_DISABLE:"disable",BOUNDARY_BOUNCE:"bounce",BOUNDARY_WRAP:"wrap"},Type:{AABB2:"AABB2",AABB3:"AABB3",CIRCLE:"Circle",SPHERE:"Sphere"}};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=i),exports.vgp=i):"undefined"!=typeof define&&define.amd?define(i):t.vgp=i}).call(this),vgp.utils={PI:Math.PI,TAU:2*Math.PI,DEG_TO_RAD:.0174532925,RAD_TO_DEG:57.2957795,clamp:function(t,i,s){return Math.max(i,Math.min(s,t))},sign:function(t){return t&&t/Math.abs(t)},random:function(t,i){return 1===arguments.length?Math.random()*t-.5*t:Math.random()*(i-t)+t},randomInt:function(t,i){return 1===arguments.length?Math.random()*t-.5*t|0:Math.random()*(i-t+1)+t|0},normalize:function(t,i,s){return(t-i)/(s-i)},getShortRotation:function(t){return t%=this.TAU,t>this.PI?t-=this.TAU:t<-this.PI&&(t+=this.TAU),t},generateID:function(){return Math.random().toString(36).slice(2)+Date.now()},isPlainObject:function(t){if("object"!=typeof t||t.nodeType||t===t.window)return!1;try{if(t.constructor&&!Object.prototype.hasOwnProperty.call(t.constructor.prototype,"isPrototypeOf"))return!1}catch(i){return!1}return!0},merge:function(){var t,i,s,n,e,o,h=arguments[0]||{},r=1,a=arguments.length,l=!1;for("boolean"==typeof h&&(l=h,h=arguments[1]||{},r=2);a>r;r++)if(null!==(t=arguments[r]))for(i in t)s=h[i],n=t[i],h!==n&&(l&&n&&(this.isPlainObject(n)||(e=Array.isArray(n)))?(e?(e=!1,o=s&&Array.isArray(s)?s:[]):o=s&&this.isPlainObject(s)?s:{},h[i]=this.merge(l,o,n)):void 0!==n&&(h[i]=n));return h},now:function(){return window.nwf?window.nwf.system.Performance.elapsedTime:window.performance.now()},empty:function(t){for(;t.lastChild;)t.removeChild(t.lastChild)},getJSON:function(t,i,s){var n;if("undefined"!=typeof XMLHttpRequest)n=new XMLHttpRequest;else for(var e=["MSXML2.XmlHttp.5.0","MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp.2.0","Microsoft.XmlHttp"],o=0,h=e.length;h>o;o++)try{n=new ActiveXObject(e[o]);break}catch(r){}n.onreadystatechange=function(){return this.readyState<4||200!==this.status?void console.warn("[Tools] Error - "+this.statusText+" - loading "+t):void i.call(s||this,JSON.parse(this.responseText))},n.open("GET",t,!0),n.send("")}},vgp.Vec=function(t,i,s){this.x=t||0,this.y=i||0,this.z=s||0},vgp.Vec.prototype={constructor:vgp.Vec,set:function(t,i,s){return this.x=t,this.y=i,this.z=s,this},setX:function(t){return this.x=t,this},setY:function(t){return this.y=t,this},setZ:function(t){return this.z=t,this},setComponent:function(t,i){switch(t){case 0:this.x=i;break;case 1:this.y=i;break;case 2:this.z=i;break;default:throw new Error("index is out of range: "+t)}},getComponent:function(t){switch(t){case 0:return this.x;case 1:return this.y;case 2:return this.z;default:throw new Error("index is out of range: "+t)}},copy:function(t){return this.x=t.x,this.y=t.y,this.z=t.z,this},add:function(t,i){return void 0!==i?(console.warn("vgp.Vec: .add() now only accepts one argument. Use .addVectors( a, b ) instead."),this.addVectors(t,i)):(this.x+=t.x,this.y+=t.y,this.z+=t.z,this)},addScalar:function(t){return this.x+=t,this.y+=t,this.z+=t,this},addVectors:function(t,i){return this.x=t.x+i.x,this.y=t.y+i.y,this.z=t.z+i.z,this},sub:function(t,i){return void 0!==i?(console.warn("vgp.Vec: .sub() now only accepts one argument. Use .subVectors( a, b ) instead."),this.subVectors(t,i)):(this.x-=t.x,this.y-=t.y,this.z-=t.z,this)},subScalar:function(t){return this.x-=t,this.y-=t,this.z-=t,this},subVectors:function(t,i){return this.x=t.x-i.x,this.y=t.y-i.y,this.z=t.z-i.z,this},multiply:function(t,i){return void 0!==i?(console.warn("vgp.Vec: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead."),this.multiplyVectors(t,i)):(this.x*=t.x,this.y*=t.y,this.z*=t.z,this)},multiplyScalar:function(t){return this.x*=t,this.y*=t,this.z*=t,this},multiplyVectors:function(t,i){return this.x=t.x*i.x,this.y=t.y*i.y,this.z=t.z*i.z,this},applyEuler:function(){var t;return function(i){return i instanceof THREE.Euler==!1&&console.error("vgp.Vec: .applyEuler() now expects a Euler rotation rather than a Vector3 and order."),void 0===t&&(t=new THREE.Quaternion),this.applyQuaternion(t.setFromEuler(i)),this}}(),applyAxisAngle:function(){var t;return function(i,s){return void 0===t&&(t=new THREE.Quaternion),this.applyQuaternion(t.setFromAxisAngle(i,s)),this}}(),applyMatrix3:function(t){var i=this.x,s=this.y,n=this.z,e=t.elements;return this.x=e[0]*i+e[3]*s+e[6]*n,this.y=e[1]*i+e[4]*s+e[7]*n,this.z=e[2]*i+e[5]*s+e[8]*n,this},applyMatrix4:function(t){var i=this.x,s=this.y,n=this.z,e=t.elements;return this.x=e[0]*i+e[4]*s+e[8]*n+e[12],this.y=e[1]*i+e[5]*s+e[9]*n+e[13],this.z=e[2]*i+e[6]*s+e[10]*n+e[14],this},applyProjection:function(t){var i=this.x,s=this.y,n=this.z,e=t.elements,o=1/(e[3]*i+e[7]*s+e[11]*n+e[15]);return this.x=(e[0]*i+e[4]*s+e[8]*n+e[12])*o,this.y=(e[1]*i+e[5]*s+e[9]*n+e[13])*o,this.z=(e[2]*i+e[6]*s+e[10]*n+e[14])*o,this},applyQuaternion:function(t){var i=this.x,s=this.y,n=this.z,e=t.x,o=t.y,h=t.z,r=t.w,a=r*i+o*n-h*s,l=r*s+h*i-e*n,c=r*n+e*s-o*i,u=-e*i-o*s-h*n;return this.x=a*r+u*-e+l*-h-c*-o,this.y=l*r+u*-o+c*-e-a*-h,this.z=c*r+u*-h+a*-o-l*-e,this},project:function(){var t;return function(i){return void 0===t&&(t=new THREE.Matrix4),t.multiplyMatrices(i.projectionMatrix,t.getInverse(i.matrixWorld)),this.applyProjection(t)}}(),unproject:function(){var t;return function(i){return void 0===t&&(t=new THREE.Matrix4),t.multiplyMatrices(i.matrixWorld,t.getInverse(i.projectionMatrix)),this.applyProjection(t)}}(),transformDirection:function(t){var i=this.x,s=this.y,n=this.z,e=t.elements;return this.x=e[0]*i+e[4]*s+e[8]*n,this.y=e[1]*i+e[5]*s+e[9]*n,this.z=e[2]*i+e[6]*s+e[10]*n,this.normalize(),this},divide:function(t){return this.x/=t.x,this.y/=t.y,this.z/=t.z,this},divideScalar:function(t){if(0!==t){var i=1/t;this.x*=i,this.y*=i,this.z*=i}else this.x=0,this.y=0,this.z=0;return this},min:function(t){return this.x>t.x&&(this.x=t.x),this.y>t.y&&(this.y=t.y),this.z>t.z&&(this.z=t.z),this},max:function(t){return this.x<t.x&&(this.x=t.x),this.y<t.y&&(this.y=t.y),this.z<t.z&&(this.z=t.z),this},clamp:function(t,i){return this.x<t.x?this.x=t.x:this.x>i.x&&(this.x=i.x),this.y<t.y?this.y=t.y:this.y>i.y&&(this.y=i.y),this.z<t.z?this.z=t.z:this.z>i.z&&(this.z=i.z),this},clampScalar:function(){var t,i;return function(s,n){return void 0===t&&(t=new vgp.Vec,i=new vgp.Vec),t.set(s,s,s),i.set(n,n,n),this.clamp(t,i)}}(),floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this.z=Math.floor(this.z),this},ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this.z=Math.ceil(this.z),this},round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this.z=Math.round(this.z),this},roundToZero:function(){return this.x=this.x<0?Math.ceil(this.x):Math.floor(this.x),this.y=this.y<0?Math.ceil(this.y):Math.floor(this.y),this.z=this.z<0?Math.ceil(this.z):Math.floor(this.z),this},negate:function(){return this.x=-this.x,this.y=-this.y,this.z=-this.z,this},dot:function(t){return this.x*t.x+this.y*t.y+this.z*t.z},lengthSq:function(){return this.x*this.x+this.y*this.y+this.z*this.z},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},lengthManhattan:function(){return Math.abs(this.x)+Math.abs(this.y)+Math.abs(this.z)},normalize:function(){return this.divideScalar(this.length())},setLength:function(t){var i=this.length();return 0!==i&&t!==i&&this.multiplyScalar(t/i),this},lerp:function(t,i){return this.x+=(t.x-this.x)*i,this.y+=(t.y-this.y)*i,this.z+=(t.z-this.z)*i,this},cross:function(t,i){if(void 0!==i)return console.warn("vgp.Vec: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead."),this.crossVectors(t,i);var s=this.x,n=this.y,e=this.z;return this.x=n*t.z-e*t.y,this.y=e*t.x-s*t.z,this.z=s*t.y-n*t.x,this},crossVectors:function(t,i){var s=t.x,n=t.y,e=t.z,o=i.x,h=i.y,r=i.z;return this.x=n*r-e*h,this.y=e*o-s*r,this.z=s*h-n*o,this},projectOnVector:function(){var t,i;return function(s){return void 0===t&&(t=new vgp.Vec),t.copy(s).normalize(),i=this.dot(t),this.copy(t).multiplyScalar(i)}}(),projectOnPlane:function(){var t;return function(i){return void 0===t&&(t=new vgp.Vec),t.copy(this).projectOnVector(i),this.sub(t)}}(),reflect:function(){var t;return function(i){return void 0===t&&(t=new vgp.Vec),this.sub(t.copy(i).multiplyScalar(2*this.dot(i)))}}(),angleTo:function(t){var i=this.dot(t)/(this.length()*t.length());return Math.acos(THREE.Math.clamp(i,-1,1))},distanceTo:function(t){return Math.sqrt(this.distanceToSquared(t))},distanceToSquared:function(t){var i=this.x-t.x,s=this.y-t.y,n=this.z-t.z;return i*i+s*s+n*n},setEulerFromRotationMatrix:function(){console.error("vgp.Vec: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.")},setEulerFromQuaternion:function(){console.error("vgp.Vec: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.")},getPositionFromMatrix:function(t){return console.warn("vgp.Vec: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition()."),this.setFromMatrixPosition(t)},getScaleFromMatrix:function(t){return console.warn("vgp.Vec: .getScaleFromMatrix() has been renamed to .setFromMatrixScale()."),this.setFromMatrixScale(t)},getColumnFromMatrix:function(t,i){return console.warn("vgp.Vec: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn()."),this.setFromMatrixColumn(t,i)},setFromMatrixPosition:function(t){return this.x=t.elements[12],this.y=t.elements[13],this.z=t.elements[14],this},setFromMatrixScale:function(t){var i=this.set(t.elements[0],t.elements[1],t.elements[2]).length(),s=this.set(t.elements[4],t.elements[5],t.elements[6]).length(),n=this.set(t.elements[8],t.elements[9],t.elements[10]).length();return this.x=i,this.y=s,this.z=n,this},setFromMatrixColumn:function(t,i){var s=4*t,n=i.elements;return this.x=n[s],this.y=n[s+1],this.z=n[s+2],this},equals:function(t){return t.x===this.x&&t.y===this.y&&t.z===this.z},fromArray:function(t,i){return void 0===i&&(i=0),this.x=t[i],this.y=t[i+1],this.z=t[i+2],this},toArray:function(t,i){return void 0===t&&(t=[]),void 0===i&&(i=0),t[i]=this.x,t[i+1]=this.y,t[i+2]=this.z,t},fromAttribute:function(t,i,s){return void 0===s&&(s=0),i=i*t.itemSize+s,this.x=t.array[i],this.y=t.array[i+1],this.z=t.array[i+2],this},clone:function(){return new vgp.Vec(this.x,this.y,this.z)}},vgp.LinkedListNode=function(){this.obj=null,this.next=null,this.prev=null,this.free=!0},vgp.LinkedList=function(){this.first=null,this.last=null,this.length=0,this.objToNodeMap={},this.uniqueID=Date.now()+""+Math.floor(1e3*Math.random()),this.sortArray=[]},vgp.LinkedList.prototype={constructor:vgp.LinkedList,getNode:function(t){return this.objToNodeMap[t.uniqueID]},addNode:function(t){var i=new vgp.LinkedListNode;if(!t.uniqueID)try{t.uniqueID=LinkedList.generateID(),console.log("New ID: "+t.uniqueID)}catch(s){return console.error("[LinkedList.addNode] obj passed is immutable: cannot attach necessary identifier"),null}return i.obj=t,i.free=!1,this.objToNodeMap[t.uniqueID]=i,i},swapObjects:function(t,i){this.objToNodeMap[t.obj.uniqueID]=null,this.objToNodeMap[i.uniqueID]=t,t.obj=i},add:function(t){var i=this.objToNodeMap[t.uniqueID];if(i){if(i.free===!1)return;i.obj=t,i.free=!1,i.next=null,i.prev=null}else i=this.addNode(t);if(this.first){if(!this.last)throw new Error("[LinkedList.add] No last in the list -- that shouldn't happen here");this.last.next=i,i.prev=this.last,this.last=i,i.next=null}else this.first=i,this.last=i,i.next=null,i.prev=null;this.length++,this.showDebug&&this.dump("after add")},has:function(t){return!!this.objToNodeMap[t.uniqueID]},moveUp:function(t){this.dump("before move up");var i=this.getNode(t);if(!i)throw"Oops, trying to move an object that isn't in the list";if(i.prev){var s=i.prev,n=s.prev;i==this.last&&(this.last=s);var e=i.next;n&&(n.next=i),i.next=s,i.prev=s.prev,s.next=e,s.prev=i,this.first==s&&(this.first=i)}},moveDown:function(t){var i=this.getNode(t);if(!i)throw"Oops, trying to move an object that isn't in the list";if(i.next){var s=i.next;this.moveUp(s.obj),this.last==s&&(this.last=i)}},sort:function(t){var i,s,n=this.sortArray,e=this.first;for(n.length=0;e;)n.push(e.obj),e=e.next;for(this.clear(),n.sort(t),s=n.length,i=0;s>i;i++)this.add(n[i])},remove:function(t){var i=this.getNode(t);return!i||i.free?!1:(i.prev&&(i.prev.next=i.next),i.next&&(i.next.prev=i.prev),i.prev||(this.first=i.next),i.next||(this.last=i.prev),i.free=!0,i.prev=null,i.next=null,this.length--,!0)},shift:function(){var t=this.first;return 0===this.length?null:(t.prev&&(t.prev.next=t.next),t.next&&(t.next.prev=t.prev),this.first=t.next,t.next||(this.last=null),t.free=!0,t.prev=null,t.next=null,this.length--,t.obj)},pop:function(){var t=this.last;return 0===this.length?null:(t.prev&&(t.prev.next=t.next),t.next&&(t.next.prev=t.prev),this.last=t.prev,t.prev||(this.first=null),t.free=!0,t.prev=null,t.next=null,this.length--,t.obj)},concat:function(t){for(var i=t.first;i;)this.add(i.obj),i=i.next},clear:function(){for(var t=this.first;t;)t.free=!0,t.prev&&(t.prev.next=t.next),t.next&&(t.next.prev=t.prev),t=t.next;this.first=null,this.length=0},dispose:function(){for(var t=this.first;t;)t.obj=null,t=t.next;this.first=null,this.objToNodeMap=null},dump:function(t){console.log("===================="+t+"=====================");for(var i=this.first;i;)console.log("{"+i.obj.toString()+"} previous="+(i.prev?i.prev.obj:"NULL")),i=i.next();console.log("==================================="),console.log("Last: {"+(this.last?this.last.obj:"NULL")+"} First: {"+(this.first?this.first.obj:"NULL")+"}")}},vgp.LinkedList.generateID=function(){return Math.random().toString(36).slice(2)+Date.now()},function(){var t=function(t,i,s,n,e){this._listener=i,this._isOnce=s,this.context=n,this._signal=t,this._priority=e||0};t.prototype={active:!0,params:null,execute:function(t){var i,s;return this.active&&this._listener&&(s=this.params?this.params.concat(t):t,i=this._listener.apply(this.context,s),this._isOnce&&this.detach()),i},detach:function(){return this.isBound()?this._signal.remove(this._listener,this.context):null},isBound:function(){return!!this._signal&&!!this._listener},isOnce:function(){return this._isOnce},getListener:function(){return this._listener},getSignal:function(){return this._signal},_destroy:function(){delete this._signal,delete this._listener,delete this.context},toString:function(){return"[SignalBinding isOnce:"+this._isOnce+", isBound:"+this.isBound()+", active:"+this.active+"]"}},t.prototype.constructor=t;var i=function(){this._bindings=[],this._prevParams=null;var t=this;this.dispatch=function(){i.prototype.dispatch.apply(t,arguments)}};i.prototype={memorize:!1,_shouldPropagate:!0,active:!0,validateListener:function(t,i){if("function"!=typeof t)throw new Error("Signal: listener is a required param of {fn}() and should be a Function.".replace("{fn}",i))},_registerListener:function(i,s,n,e){var o,h=this._indexOfListener(i,n);if(-1!==h){if(o=this._bindings[h],o.isOnce()!==s)throw new Error("You cannot add"+(s?"":"Once")+"() then add"+(s?"Once":"")+"() the same listener without removing the relationship first.")}else o=new t(this,i,s,n,e),this._addBinding(o);return this.memorize&&this._prevParams&&o.execute(this._prevParams),o},_addBinding:function(t){var i=this._bindings.length;do i--;while(this._bindings[i]&&t._priority<=this._bindings[i]._priority);this._bindings.splice(i+1,0,t)},_indexOfListener:function(t,i){for(var s,n=this._bindings.length;n--;)if(s=this._bindings[n],s._listener===t&&s.context===i)return n;return-1},has:function(t,i){return-1!==this._indexOfListener(t,i)},add:function(t,i,s){return this.validateListener(t,"add"),this._registerListener(t,!1,i,s)},addOnce:function(t,i,s){return this.validateListener(t,"addOnce"),this._registerListener(t,!0,i,s)},remove:function(t,i){this.validateListener(t,"remove");var s=this._indexOfListener(t,i);return-1!==s&&(this._bindings[s]._destroy(),this._bindings.splice(s,1)),t},removeAll:function(t){"undefined"==typeof t&&(t=null);for(var i=this._bindings.length;i--;)t?this._bindings[i].context===t&&(this._bindings[i]._destroy(),this._bindings.splice(i,1)):this._bindings[i]._destroy();t||(this._bindings.length=0)},getNumListeners:function(){return this._bindings.length},halt:function(){this._shouldPropagate=!1},dispatch:function(){if(this.active){var t,i=Array.prototype.slice.call(arguments),s=this._bindings.length;if(this.memorize&&(this._prevParams=i),s){t=this._bindings.slice(),this._shouldPropagate=!0;do s--;while(t[s]&&this._shouldPropagate&&t[s].execute(i)!==!1)}}},forget:function(){this._prevParams=null},dispose:function(){this.removeAll(),delete this._bindings,delete this._prevParams},toString:function(){return"[Signal active:"+this.active+" numListeners:"+this.getNumListeners()+"]"}},i.prototype.constructor=i,vgp.Signal=i}.call(this),vgp.Manifold=function(){this.a=null,this.b=null,this.penetration=0,this.normal=new vgp.Vec},vgp.physics={_scratch:new vgp.Vec,_scratch2:new vgp.Vec,_normal:new vgp.Vec,_impulse:new vgp.Vec,_manifold:new vgp.Manifold,_separateStr:"separate",_testStr:"test",elapsed:.01666,resolve:function(t,i,s){this._scratch.set(i.velocity.x-t.velocity.x,i.velocity.y-t.velocity.y,i.velocity.z-t.velocity.z);var n=this._scratch.dot(s.normal);if(!(n>0)){var e=Math.min(t.restitution,i.restitution),o=-(1+e)*n;o/=t.invmass+i.invmass,this._impulse.set(s.normal.x*o,s.normal.y*o,s.normal.z*o),t.velocity.x-=t.invmass*this._impulse.x,t.velocity.y-=t.invmass*this._impulse.y,t.velocity.z-=t.invmass*this._impulse.z,i.velocity.x+=i.invmass*this._impulse.x,i.velocity.y+=i.invmass*this._impulse.y,i.velocity.z+=i.invmass*this._impulse.z}},test:function(t,i){if(t.type===i.type)return this[this._testStr+t.type+i.type](t,i);throw new Error("Sorry, mixed collider types are not supported")},separate:function(t,i){if(t.type===i.type)return this[this._separateStr+t.type+i.type](t,i);throw new Error("Sorry, mixed collider types are not yet supported")},testAABB3AABB3:function(t,i){return t.max.x<i.min.x||t.min.x>i.max.x?!1:t.max.y<i.min.y||t.min.y>i.max.y?!1:t.max.z<i.min.z||t.min.z>i.max.z?!1:!0},separateAABB3AABB3:function(t,i){if(t.max.x<i.min.x||t.min.x>i.max.x)return null;if(t.max.y<i.min.y||t.min.y>i.max.y)return null;if(t.max.z<i.min.z||t.min.z>i.max.z)return null;this._normal.set(i.position.x-t.position.x,i.position.y-t.position.y,i.position.z-t.position.z);var s=.5*(t.max.x-t.min.x),n=.5*(i.max.x-i.min.x),e=s+n-Math.abs(this._normal.x);s=.5*(t.max.y-t.min.y),n=.5*(i.max.y-i.min.y);var o=s+n-Math.abs(this._normal.y);s=.5*(t.max.z-t.min.z),n=.5*(i.max.z-i.min.z);var h=s+n-Math.abs(this._normal.z);o>e?e>h?(this._normal.z<0?this._manifold.normal.set(0,0,-1):this._manifold.normal.set(0,0,1),this._manifold.penetration=h):(this._normal.x<0?this._manifold.normal.set(-1,0,0):this._manifold.normal.set(1,0,0),this._manifold.penetration=e):o>h?(this._normal.z<0?this._manifold.normal.set(0,0,-1):this._manifold.normal.set(0,0,1),this._manifold.penetration=h):(this._normal.y<0?this._manifold.normal.set(0,-1,0):this._manifold.normal.set(0,1,0),this._manifold.penetration=o),this._scratch.copy(this._manifold.normal).multiplyScalar(this._manifold.penetration),this._scratch2.copy(this._scratch);var r=t.invmass+i.invmass;return this._scratch.multiplyScalar(t.invmass/r),t.position.sub(this._scratch),this._scratch2.multiplyScalar(i.invmass/r),i.position.add(this._scratch2),this._manifold},testSphereSphere:function(t,i){var s=i.position.x-t.position.x,n=i.position.y-t.position.y,e=i.position.z-t.position.z,o=s*s+n*n+e*e,h=t.radius+i.radius;return h*h>o?!0:!1},separateSphereSphere:function(t,i){this._scratch.copy(i.position).sub(t.position);var s=t.radius+i.radius,n=s*s,e=this._scratch.dot(this._scratch);if(n>e){e=Math.sqrt(e),0===e?(e=s-1,this._scratch.set(s,s,s),this._manifold.penetration=t.radius,this._manifold.normal.set(1,0,0)):(this._manifold.penetration=s-e,this._manifold.normal.copy(this._scratch).normalize());var o=(s-e)/e,h=t.invmass+i.invmass;return this._scratch.multiplyScalar(o),this._scratch2.copy(this._scratch),this._scratch.multiplyScalar(t.invmass/h),t.position.sub(this._scratch),this._scratch2.multiplyScalar(i.invmass/h),i.position.add(this._scratch2),this._manifold}if(!t.continuous&&!i.continuous)return null;if(0>e-n)return null;this._scratch2.copy(i.velocity).sub(t.velocity).multiplyScalar(this.elapsed),this._impulse.copy(t.velocity).multiplyScalar(this.elapsed);var r=this._impulse.dot(this._impulse);if(this._impulse.copy(i.velocity).multiplyScalar(this.elapsed),r+this._impulse.dot(this._impulse)+n<e)return null;var a=this._scratch2.dot(this._scratch2),l=this._scratch2.dot(this._scratch);if(l>=0)return null;var c=l*l-a*(e-n);if(0>c)return null;var u=(-l-Math.sqrt(c))/a;return this._scratch2.copy(t.velocity).multiplyScalar(this.elapsed).multiplyScalar(u),t.position.add(this._scratch2),this._scratch2.copy(i.velocity).multiplyScalar(this.elapsed).multiplyScalar(u),i.position.add(this._scratch2),this._manifold.penetration=0,this._manifold.normal.copy(this._scratch).normalize(),this._manifold}},vgp.AABB3=function(t,i){this.active=!1,this.entity=t||null,this.uniqueID=vgp.utils.generateID(),this.type=vgp.Type.AABB3,this.solid=!0,this.width=50,this.height=50,this.depth=50,this.maxSpeed=10,this.mass=50,this.invmass=0,this.restitution=.8,this.autoAdd=!0,this.boundaryBehavior=vgp.Boundary.BOUNDARY_BOUNCE,this.collisionID=this.uniqueID,this.collisionGroup=null,this.onCollision=new vgp.Signal,vgp.utils.merge(this,i),this.position=t&&t.position?t.position:new vgp.Vec,this.velocity=t&&t.velocity?t.velocity:new vgp.Vec,this.accel=t&&t.accel?t.accel:new vgp.Vec,this.min=new vgp.Vec,this.max=new vgp.Vec,this.half=new vgp.Vec(this.width/2,this.height/2,this.depth/2),this._v=new vgp.Vec,this.setMass(this.mass),this.update()},vgp.AABB3.prototype={constructor:vgp.AABB3,activate:function(){this.active=!0,this.autoAdd&&game.world.add(this),this.position=this.entity.position},disable:function(){this.velocity.set(),this.accel.set(),this.active=!1,game.world.remove(this)},setMass:function(t){this.mass=t,this.invmass=0>=t?0:1/t},reset:function(){this.setMass(this.mass)},update:function(){var t=game.world,i=this.accel.length();if(0!==i&&i>this.maxSpeed&&(this.accel.divideScalar(i),this.accel.multiplyScalar(this.maxSpeed)),this.accel.add(t.gravity),this.velocity.multiplyScalar(t.friction),this.velocity.add(this.accel),this._v.copy(this.velocity).multiplyScalar(t.elapsed),this.position.add(this._v),this.min.copy(this.position).sub(this.half),this.max.copy(this.position).add(this.half),t.bounded){switch(this.boundaryBehavior){case vgp.Boundary.BOUNDARY_DISABLE:(this.min.x<t.min.x||this.max.x>t.max.x||this.min.y<t.min.y||this.max.y>t.max.y||this.min.z<t.min.z||this.max.z>t.max.z)&&(this.onCollision.dispatch(vgp.Boundary),this.disable());break;case vgp.Boundary.BOUNDARY_BOUNCE:this.min.x<t.min.x?(this.position.x=t.min.x+this.half.x,this.velocity.x=-this.velocity.x*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.x>t.max.x&&(this.position.x=t.max.x-this.half.x,this.velocity.x=-this.velocity.x*this.restitution,this.onCollision.dispatch(vgp.Boundary)),this.min.y<t.min.y?(this.position.y=t.min.y+this.half.y,this.velocity.y=-this.velocity.y*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.y>t.max.y&&(this.position.y=t.max.y-this.half.y,this.velocity.y=-this.velocity.y*this.restitution,this.onCollision.dispatch(vgp.Boundary)),this.min.z<t.min.z?(this.position.z=t.min.z+this.half.z,this.velocity.z=-this.velocity.z*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.z>t.max.z&&(this.position.z=t.max.z-this.half.z,this.velocity.z=-this.velocity.z*this.restitution,this.onCollision.dispatch(vgp.Boundary));break;case vgp.Boundary.BOUNDARY_WRAP:this.min.x<t.min.x?this.position.x+=t.max.x-this.half.x:this.max.x>t.max.x&&(this.position.x-=t.max.x-this.half.x),this.min.y<t.min.y?this.position.y+=t.max.y-this.half.y:this.max.y>t.max.y&&(this.position.y-=t.max.y-this.half.y),this.min.z<t.min.z?this.position.z+=t.max.z-this.half.z:this.max.z>t.max.z&&(this.position.z-=t.max.z-this.half.z)}this.min.copy(this.position).sub(this.half),this.max.copy(this.position).add(this.half)}else this.min.max(t.min),this.max.min(t.max)},dispose:function(){this.onCollision.dispose(),this.onCollision=null,this.entity=null,this.position=null,this.velocity=null,this.accel=null,this.min=null,this.max=null}},vgp.Radial3=function(t,i){this.active=!1,this.entity=t||null,this.uniqueID=vgp.utils.generateID(),this.type=vgp.Type.SPHERE,this.solid=!0,this.radius=10,this.maxSpeed=20,this.continuous=!1,this.mass=50,this.restitution=.8,this.boundaryBehavior=vgp.Boundary.BOUNDARY_BOUNCE,this.collisionID=this.uniqueID,this.collisionGroup=null,this.onCollision=new vgp.Signal,vgp.utils.merge(this,i),this.invmass=0,this.position=t&&t.position?t.position:new vgp.Vec,this.velocity=t&&t.velocity?t.velocity:new vgp.Vec,this.accel=t&&t.accel?t.accel:new vgp.Vec,this.min=new vgp.Vec,this.max=new vgp.Vec,this._v=new vgp.Vec,this.setMass(this.mass),this.update()},vgp.Radial3.prototype={constructor:vgp.Radial3,activate:function(){this.active=!0,game.world.add(this),this.position=this.entity.position},disable:function(){this.velocity.set(),this.accel.set(),this.active=!1,game.world.remove(this)},setMass:function(t){this.mass=t,this.invmass=0>=t?0:1/t},reset:function(){this.setMass(this.mass)},update:function(){var t=game.world,i=this.accel.length();if(0!==i&&i>this.maxSpeed&&(this.accel.divideScalar(i),this.accel.multiplyScalar(this.maxSpeed)),this.accel.add(t.gravity),this.velocity.multiplyScalar(t.friction),this.velocity.add(this.accel),this._v.copy(this.velocity).multiplyScalar(t.elapsed),this.position.add(this._v),this.min.copy(this.position).subScalar(this.radius),this.max.copy(this.position).addScalar(this.radius),t.bounded)switch(this.boundaryBehavior){case vgp.Boundary.BOUNDARY_DISABLE:(this.min.x<t.min.x||this.max.x>t.max.x||this.min.y<t.min.y||this.max.y>t.max.y||this.min.z<t.min.z||this.max.z>t.max.z)&&(this.onCollision.dispatch(vgp.Boundary),this.disable());break;case vgp.Boundary.BOUNDARY_BOUNCE:this.min.x<t.min.x?(this.position.x=t.min.x+this.radius,this.velocity.x=-this.velocity.x*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.x>t.max.x&&(this.position.x=t.max.x-this.radius,this.velocity.x=-this.velocity.x*this.restitution,this.onCollision.dispatch(vgp.Boundary)),this.min.y<t.min.y?(this.position.y=t.min.y+this.radius,this.velocity.y=-this.velocity.y*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.y>t.max.y&&(this.position.y=t.max.y-this.radius,this.velocity.y=-this.velocity.y*this.restitution,this.onCollision.dispatch(vgp.Boundary)),this.min.z<t.min.z?(this.position.z=t.min.z+this.radius,this.velocity.z=-this.velocity.z*this.restitution,this.onCollision.dispatch(vgp.Boundary)):this.max.z>t.max.z&&(this.position.z=t.max.z-this.radius,this.velocity.z=-this.velocity.z*this.restitution,this.onCollision.dispatch(vgp.Boundary));break;case vgp.Boundary.BOUNDARY_WRAP:this.min.x<t.min.x?this.position.x+=t.max.x+this.radius:this.max.x>t.max.x&&(this.position.x-=t.max.x-this.radius),this.min.y<t.min.y?this.position.y+=t.max.y+this.radius:this.max.y>t.max.y&&(this.position.y-=t.max.y-this.radius),this.min.z<t.min.z?this.position.z+=t.max.z+this.radius:this.max.z>t.max.z&&(this.position.z-=t.max.z-this.radius)}else this.min.max(t.min),this.max.min(t.max)},dispose:function(){this.onCollision.dispose(),this.onCollision=null,this.entity=null,this.position=null,this.velocity=null,this.accel=null,this.min=null,this.max=null}},vgp.ObjectPool=function(t,i,s){this.free=new vgp.LinkedList,this.busy=new vgp.LinkedList,this.size=s,this._Class=t,this._settings=i||{},this._settings.pool=this;for(var n=0;n<this.size;n++)this.free.add(new this._Class(this._settings))},vgp.ObjectPool.prototype={constructor:vgp.ObjectPool,get:function(){var t;return this.free.length?(t=this.free.pop(),this.busy.add(t),t):(t=new this._Class(this._settings),this.busy.add(t),this.size++,t)},recycle:function(t){this.busy.has(t)&&(this.busy.remove(t),this.free.add(t))},freeAll:function(){for(var t,i=this.busy.first;i;)t=i.obj,i=i.next,this.busy.remove(t),this.free.add(t)},dispose:function(){for(var t=this.busy.first;t;)t.obj.dispose(),t=t.next;for(t=this.free.first;t;)t.obj.dispose(),t=t.next;this.free.dispose(),this.busy.dispose(),this.free=null,this.busy=null,this._Class=null,this._settings=null},toString:function(){return"[ObjectPool size: "+this.size+", free: "+this.free.length+", busy: "+this.busy.length+"]"}},vgp.World=function(){this.active=!0,this.bounded=!1,this.friction=.9,this.gravity=new vgp.Vec,this.elapsed=.0166,this.min=new vgp.Vec,this.max=new vgp.Vec(100,100,100),this.objects=new vgp.LinkedList},vgp.World.prototype={constructor:vgp.World,add:function(t){return t=t.body||t,t.onCollision?void this.objects.add(t):(console.warn("[vgp.World.add] Ignoring object; must be an entity or physics component"),void console.dir(t))},remove:function(t){t=t.body||t,this.objects.remove(t)},setBounds:function(t,i,s){this.bounded="boolean"==typeof t?t:!0,this.min.set(0,0,0),this.bounded&&this.max.set(t,i,s)},update:function(){var t,i,s,n,e;for(s=this.objects.first;s;)if(i=s.obj,i.solid&&i.active){for(e=s.next;e;)n=e.obj,e=e.next,n.solid&&n.active&&n.collisionID!==i.collisionID&&(t=vgp.physics.separate(i,n),t&&(vgp.physics.resolve(i,n,t),i.onCollision.dispatch(n,t),n.onCollision.dispatch(i,t)));s=s.next}else s=s.next},disable:function(){this.active=!1},dispose:function(){this.objects.dispose(),this.objects=null,this.grid=null,this.min=null,this.max=null,this._emptyCell=null}};