forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcannon.min.js
23 lines (23 loc) · 43.1 KB
/
cannon.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Copyright (c) 2012 cannon.js Authors
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/(function(){var a=a||{};this.Int32Array||(this.Int32Array=Array,this.Float32Array=Array),a.Broadphase=function(){this.world=null},a.Broadphase.prototype.constructor=a.BroadPhase,a.Broadphase.prototype.collisionPairs=function(a){throw"collisionPairs not implemented for this BroadPhase class!"},a.NaiveBroadphase=function(){this.temp={r:new a.Vec3,normal:new a.Vec3,quat:new a.Quaternion}},a.NaiveBroadphase.prototype=new a.Broadphase,a.NaiveBroadphase.prototype.constructor=a.NaiveBroadphase,a.NaiveBroadphase.prototype.collisionPairs=function(b){var c=[],d=[],e=b.numObjects(),f=b.bodies,g=a.Shape.types,h=g.SPHERE|g.BOX|g.COMPOUND|g.CONVEXPOLYHEDRON,i=g.PLANE,j=a.RigidBody.STATIC|a.RigidBody.KINEMATIC,k=this.temp.r,l=this.temp.normal,m=this.temp.quat;for(var n=0;n<e;n++)for(var o=0;o<n;o++){var p=f[n],q=f[o],r=p.shape.type,s=q.shape.type;if((p.motionstate&j)!==0&&(q.motionstate&j)!==0)continue;if(r&h&&s&h){q.position.vsub(p.position,k);var t=p.shape.boundingSphereRadius()+q.shape.boundingSphereRadius();k.norm2()<t*t&&(c.push(p),d.push(q))}else if(r&h&&s&g.PLANE||s&h&&r&g.PLANE){var u=r===i?n:o,v=r!==i?n:o;f[v].position.vsub(f[u].position,k),f[u].quaternion.vmult(f[u].shape.normal,l);var w=k.dot(l)-f[v].shape.boundingSphereRadius();w<0&&(c.push(p),d.push(q))}}return[c,d]},a.Mat3=function(a){a?this.elements=new Float32Array(a):this.elements=new Float32Array(9)},a.Mat3.prototype.identity=function(){this.elements[0]=1,this.elements[1]=0,this.elements[2]=0,this.elements[3]=0,this.elements[4]=1,this.elements[5]=0,this.elements[6]=0,this.elements[7]=0,this.elements[8]=1},a.Mat3.prototype.vmult=function(b,c){c===undefined&&(c=new a.Vec3);var d=[b.x,b.y,b.z],e=[0,0,0];for(var f=0;f<3;f++)for(var g=0;g<3;g++)e[f]+=this.elements[f+3*g]*d[f];return c.x=e[0],c.y=e[1],c.z=e[2],c},a.Mat3.prototype.smult=function(a){for(var b=0;b<this.elements.length;b++)this.elements[b]*=a},a.Mat3.prototype.mmult=function(b){var c=new a.Mat3;for(var d=0;d<3;d++)for(var e=0;e<3;e++){var f=0;for(var g=0;g<3;g++)f+=this.elements[d+g]*b.elements[g+e*3];c.elements[d+e*3]=f}return c},a.Mat3.prototype.solve=function(b,c){c=c||new a.Vec3;var d=3,e=4,f=new Float32Array(d*e),g,h;for(g=0;g<3;g++)for(h=0;h<3;h++)f[g+e*h]=this.elements[g+3*h];f[3]=b.x,f[7]=b.y,f[11]=b.z;var i=3,j=i,k,l=4,m,n;do{g=j-i;if(f[g+e*g]===0)for(h=g+1;h<j;h++)if(f[g+e*h]!==0){n=[],k=l;do m=l-k,n.push(f[m+e*g]+f[m+e*h]);while(--k);f[g+e*0]=n[0],f[g+e*1]=n[1],f[g+e*2]=n[2];break}if(f[g+e*g]!==0)for(h=g+1;h<j;h++){var o=f[g+e*h]/f[g+e*g];n=[],k=l;do m=l-k,n.push(m<=g?0:f[m+e*h]-f[m+e*g]*o);while(--k);f[h+e*0]=n[0],f[h+e*1]=n[1],f[h+e*2]=n[2]}}while(--i);c.z=f[2*e+3]/f[2*e+2],c.y=(f[1*e+3]-f[1*e+2]*c.z)/f[1*e+1],c.x=(f[0*e+3]-f[0*e+2]*c.z-f[0*e+1]*c.y)/f[0*e+0];if(isNaN(c.x)||isNaN(c.y)||isNaN(c.z)||c.x===Infinity||c.y===Infinity||c.z===Infinity)throw"Could not solve equation! Got x=["+c.toString()+"], b=["+b.toString()+"], A=["+this.toString()+"]";return c},a.Mat3.prototype.e=function(a,b,c){if(c===undefined)return this.elements[a+3*b];this.elements[a+3*b]=c},a.Mat3.prototype.copy=function(b){b=b||new a.Mat3;for(var c=0;c<this.elements.length;c++)b.elements[c]=this.elements[c];return b},a.Mat3.prototype.toString=function(){var a="",b=",";for(var c=0;c<9;c++)a+=this.elements[c]+b;return a},a.Vec3=function(a,b,c){this.x=a||0,this.y=b||0,this.z=c||0},a.Vec3.prototype.cross=function(b,c){c=c||new a.Vec3;var d=[this.x,this.y,this.z],e=[b.x,b.y,b.z];return c.x=d[1]*e[2]-d[2]*e[1],c.y=d[2]*e[0]-d[0]*e[2],c.z=d[0]*e[1]-d[1]*e[0],c},a.Vec3.prototype.set=function(a,b,c){return this.x=a,this.y=b,this.z=c,this},a.Vec3.prototype.vadd=function(b,c){if(!c)return new a.Vec3(this.x+b.x,this.y+b.y,this.z+b.z);c.x=b.x+this.x,c.y=b.y+this.y,c.z=b.z+this.z},a.Vec3.prototype.vsub=function(b,c){if(!c)return new a.Vec3(this.x-b.x,this.y-b.y,this.z-b.z);c.x=this.x-b.x,c.y=this.y-b.y,c.z=this.z-b.z},a.Vec3.prototype.crossmat=function(){return new a.Mat3([0,-this.z,this.y,this.z,0,-this.x,-this.y,this.x,0])},a.Vec3.prototype.normalize=function(){var a=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);return a>0?(this.x/=a,this.y/=a,this.z/=a):(this.x=0,this.y=0,this.z=0),a},a.Vec3.prototype.unit=function(b){b=b||new a.Vec3;var c=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);return c>0?(c=1/c,b.x=this.x*c,b.y=this.y*c,b.z=this.z*c):(b.x=0,b.y=0,b.z=0),b},a.Vec3.prototype.norm=function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},a.Vec3.prototype.norm2=function(){return this.dot(this)},a.Vec3.prototype.mult=function(b,c){return c||(c=new a.Vec3),c.x=b*this.x,c.y=b*this.y,c.z=b*this.z,c},a.Vec3.prototype.dot=function(a){return this.x*a.x+this.y*a.y+this.z*a.z},a.Vec3.prototype.isZero=function(){return this.x===0&&this.y===0&&this.z===0},a.Vec3.prototype.negate=function(b){return b=b||new a.Vec3,b.x=-this.x,b.y=-this.y,b.z=-this.z,b},a.Vec3.prototype.tangents=function(b,c){var d=this.norm();if(d>0){var e=new a.Vec3(this.x/d,this.y/d,this.z/d);if(e.x<.9){var f=Math.random();e.cross((new a.Vec3(f,1e-7,0)).unit(),b)}else e.cross((new a.Vec3(1e-7,f,0)).unit(),b);e.cross(b,c)}else b.set(1,0,0).normalize(),c.set(0,1,0).normalize()},a.Vec3.prototype.toString=function(){return this.x+","+this.y+","+this.z},a.Vec3.prototype.copy=function(b){return b=b||new a.Vec3,b.x=this.x,b.y=this.y,b.z=this.z,b},a.Vec3.prototype.lerp=function(a,b,c){c.x=this.x+(a.x-this.x)*b,c.y=this.y+(a.y-this.y)*b,c.z=this.z+(a.z-this.z)*b},a.Vec3.prototype.almostEquals=function(a,b){return b===undefined&&(b=1e-6),Math.abs(this.x-a.x)>b||Math.abs(this.y-a.y)>b||Math.abs(this.z-a.z)>b?!1:!0},a.Vec3.prototype.almostZero=function(a){return a===undefined&&(a=1e-6),Math.abs(this.x)>a||Math.abs(this.y)>a||Math.abs(this.z)>a?!1:!0},a.Quaternion=function(a,b,c,d){this.x=a!=undefined?a:0,this.y=b!=undefined?b:0,this.z=c!=undefined?c:0,this.w=d!=undefined?d:1},a.Quaternion.prototype.set=function(a,b,c,d){this.x=a,this.y=b,this.z=c,this.w=d},a.Quaternion.prototype.toString=function(){return this.x+","+this.y+","+this.z+","+this.w},a.Quaternion.prototype.setFromAxisAngle=function(a,b){var c=Math.sin(b*.5);this.x=a.x*c,this.y=a.y*c,this.z=a.z*c,this.w=Math.cos(b*.5)},a.Quaternion.prototype.setFromVectors=function(a,b){var c=a.cross(b);this.x=c.x,this.y=c.y,this.z=c.z,this.w=Math.sqrt(Math.pow(a.norm(),2)*Math.pow(b.norm(),2))+a.dot(b),this.normalize()},a.Quaternion.prototype.mult=function(b,c){c==undefined&&(c=new a.Quaternion);var d=new a.Vec3(this.x,this.y,this.z),e=new a.Vec3(b.x,b.y,b.z);return c.w=this.w*b.w-d.dot(e),vaxvb=d.cross(e),c.x=this.w*e.x+b.w*d.x+vaxvb.x,c.y=this.w*e.y+b.w*d.y+vaxvb.y,c.z=this.w*e.z+b.w*d.z+vaxvb.z,c},a.Quaternion.prototype.inverse=function(b){b==undefined&&(b=new a.Quaternion),this.conjugate(b);var c=1/(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);return b.x*=c,b.y*=c,b.z*=c,b.w*=c,b},a.Quaternion.prototype.conjugate=function(b){return b==undefined&&(b=new a.Quaternion),b.x=-this.x,b.y=-this.y,b.z=-this.z,b.w=this.w,b},a.Quaternion.prototype.normalize=function(){var a=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);a===0?(this.x=0,this.y=0,this.z=0,this.w=0):(a=1/a,this.x*=a,this.y*=a,this.z*=a,this.w*=a)},a.Quaternion.prototype.normalizeFast=function(){var a=(3-(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w))/2;a===0?(this.x=0,this.y=0,this.z=0,this.w=0):(this.x*=a,this.y*=a,this.z*=a,this.w*=a)},a.Quaternion.prototype.vmult=function(b,c){c=c||new a.Vec3;if(this.w==0)c.x=b.x,c.y=b.y,c.z=b.z;else{var d=b.x,e=b.y,f=b.z,g=this.x,h=this.y,i=this.z,j=this.w,k=j*d+h*f-i*e,l=j*e+i*d-g*f,m=j*f+g*e-h*d,n=-g*d-h*e-i*f;c.x=k*j+n*-g+l*-i-m*-h,c.y=l*j+n*-h+m*-g-k*-i,c.z=m*j+n*-i+k*-h-l*-g}return c},a.Quaternion.prototype.copy=function(a){a.x=this.x,a.y=this.y,a.z=this.z,a.w=this.w},a.Quaternion.prototype.toEuler=function(a,b){b=b||"YZX";var c,d,e,f=this.x,g=this.y,h=this.z,i=this.w;switch(b){case"YZX":var j=f*g+h*i;j>.499&&(c=2*Math.atan2(f,i),d=Math.PI/2,e=0),j<-0.499&&(c=-2*Math.atan2(f,i),d=-Math.PI/2,e=0);if(isNaN(c)){var k=f*f,l=g*g,m=h*h;c=Math.atan2(2*g*i-2*f*h,1-2*l-2*m),d=Math.asin(2*j),e=Math.atan2(2*f*i-2*g*h,1-2*k-2*m)}break;default:throw new Error("Euler order "+b+" not supported yet.")}a.y=c,a.z=d,a.x=e},a.Shape=function(){this.type=0,this.aabbmin=new a.Vec3,this.aabbmax=new a.Vec3},a.Shape.prototype.constructor=a.Shape,a.Shape.prototype.boundingSphereRadius=function(){throw"boundingSphereRadius() not implemented for shape type "+this.type},a.Shape.prototype.volume=function(){throw"volume() not implemented for shape type "+this.type},a.Shape.prototype.calculateLocalInertia=function(a,b){throw"calculateLocalInertia() not implemented for shape type "+this.type},a.Shape.prototype.calculateTransformedInertia=function(b,c,d){d==undefined&&(d=new a.Vec3),c.normalize();var e=this.calculateLocalInertia(b),f=c.vmult(e);return d.x=Math.abs(f.x),d.y=Math.abs(f.y),d.z=Math.abs(f.z),d},a.Shape.types={SPHERE:1,PLANE:2,BOX:4,COMPOUND:8,CONVEXPOLYHEDRON:16},a.RigidBody=function(b,c,d){this.position=new a.Vec3,this.initPosition=new a.Vec3,this.velocity=new a.Vec3,this.initVelocity=new a.Vec3,this.force=new a.Vec3,this.tau=new a.Vec3,this.quaternion=new a.Quaternion,this.initQuaternion=new a.Quaternion,this.angularVelocity=new a.Vec3,this.initAngularVelocity=new a.Vec3,this.mass=b,this.invMass=b>0?1/b:0,this.shape=c,this.inertia=new a.Vec3,c.calculateLocalInertia(b,this.inertia),this.invInertia=new a.Vec3(this.inertia.x>0?1/this.inertia.x:0,this.inertia.y>0?1/this.inertia.y:0,this.inertia.z>0?1/this.inertia.z:0),this.material=d,this.linearDamping=.01,this.angularDamping=.01,this.motionstate=b<=0?a.RigidBody.STATIC:a.RigidBody.DYNAMIC,this.world=null,this.preStep=null,this.postStep=null},a.RigidBody.DYNAMIC=1,a.RigidBody.STATIC=2,a.RigidBody.KINEMATIC=4,a.Sphere=function(b){a.Shape.call(this),this.radius=b!=undefined?Number(b):1,this.type=a.Shape.types.SPHERE},a.Sphere.prototype=new a.Shape,a.Sphere.prototype.constructor=a.Sphere,a.Sphere.prototype.calculateLocalInertia=function(b,c){c=c||new a.Vec3;var d=2*b*this.radius*this.radius/5;return c.x=d,c.y=d,c.z=d,c},a.Sphere.prototype.volume=function(){return 4*Math.PI*this.radius/3},a.Sphere.prototype.boundingSphereRadius=function(){return this.radius},a.Box=function(b){a.Shape.call(this),this.halfExtents=b,this.type=a.Shape.types.BOX,this.convexPolyhedronRepresentation=null,this.updateConvexPolyhedronRepresentation()},a.Box.prototype=new a.Shape,a.Box.prototype.constructor=a.Box,a.Box.prototype.updateConvexPolyhedronRepresentation=function(){var b=this.halfExtents.x,c=this.halfExtents.y,d=this.halfExtents.z,e=a.Vec3,f=new a.ConvexPolyhedron([new e(-b,-c,-d),new e(b,-c,-d),new e(b,c,-d),new e(-b,c,-d),new e(-b,-c,d),new e(b,-c,d),new e(b,c,d),new e(-b,c,d)],[[0,1,2,3],[4,5,6,7],[0,1,4,5],[2,3,6,7],[0,3,4,7],[1,2,5,6]],[new e(0,0,-1),new e(0,0,1),new e(0,-1,0),new e(0,1,0),new e(-1,0,0),new e(1,0,0)]);this.convexPolyhedronRepresentation=f},a.Box.prototype.calculateLocalInertia=function(b,c){return c=c||new a.Vec3,c.x=1/12*b*(2*this.halfExtents.y*2*this.halfExtents.y+2*this.halfExtents.z*2*this.halfExtents.z),c.y=1/12*b*(2*this.halfExtents.x*2*this.halfExtents.x+2*this.halfExtents.z*2*this.halfExtents.z),c.z=1/12*b*(2*this.halfExtents.y*2*this.halfExtents.y+2*this.halfExtents.x*2*this.halfExtents.x),c},a.Box.prototype.getCorners=function(b){var c=[],d=this.halfExtents;c.push(new a.Vec3(d.x,d.y,d.z)),c.push(new a.Vec3(-d.x,d.y,d.z)),c.push(new a.Vec3(-d.x,-d.y,d.z)),c.push(new a.Vec3(-d.x,-d.y,-d.z)),c.push(new a.Vec3(d.x,-d.y,-d.z)),c.push(new a.Vec3(d.x,d.y,-d.z)),c.push(new a.Vec3(-d.x,d.y,-d.z)),c.push(new a.Vec3(d.x,-d.y,d.z));for(var e=0;b!=undefined&&e<c.length;e++)b.vmult(c[e],c[e]);return c},a.Box.prototype.getSideNormals=function(b,c){var d=[],e=this.halfExtents;d.push(new a.Vec3(e.x,0,0)),d.push(new a.Vec3(0,e.y,0)),d.push(new a.Vec3(0,0,e.z)),b!=undefined&&b&&(d.push(new a.Vec3(-e.x,0,0)),d.push(new a.Vec3(0,-e.y,0)),d.push(new a.Vec3(0,0,-e.z)));for(var f=0;c!=undefined&&f<d.length;f++)c.vmult(d[f],d[f]);return d},a.Box.prototype.volume=function(){return 8*this.halfExtents.x*this.halfExtents.y*this.halfExtents.z},a.Box.prototype.boundingSphereRadius=function(){return this.halfExtents.norm()},a.Plane=function(b){a.Shape.call(this),b.normalize(),this.normal=b,this.type=a.Shape.types.PLANE},a.Plane.prototype=new a.Shape,a.Plane.prototype.constructor=a.Plane,a.Plane.prototype.calculateLocalInertia=function(b,c){return c=c||new a.Vec3,c},a.Plane.prototype.volume=function(){return Infinity},a.Compound=function(){a.Shape.call(this),this.type=a.Shape.types.COMPOUND,this.childShapes=[],this.childOffsets=[],this.childOrientations=[]},a.Compound.prototype=new a.Shape,a.Compound.prototype.constructor=a.Compound,a.Compound.prototype.addChild=function(b,c,d){c=c||new a.Vec3,d=d||new a.Quaternion,this.childShapes.push(b),this.childOffsets.push(c),this.childOrientations.push(d)},a.Compound.prototype.volume=function(){var a=0;for(var b=0;b<this.childShapes.length;b++)a+=this.childShapes[b].volume();return a},a.Compound.prototype.calculateLocalInertia=function(b,c){c=c||new a.Vec3;var d=this.volume();for(var e=0;e<this.childShapes.length;e++){var f=this.childShapes[e],g=this.childOffsets[e],h=this.childOrientations[e],i=f.volume()/d*b,j=f.calculateTransformedInertia(i,h);c.vadd(j,c);var k=new a.Vec3(i*g.x*g.x,i*g.y*g.y,i*g.z*g.z);c.vadd(k,c)}return c},a.Compound.prototype.boundingSphereRadius=function(){var a=0;for(var b=0;b<this.childShapes.length;b++){var c=this.childOffsets[b].norm()+this.childShapes[b].boundingSphereRadius();a<c&&(a=c)}return a},a.ConvexPolyhedron=function(b,c,d){function o(b,c,d,e,f){var g=b.vertices.length,h=null,i=null,j=b.vertices,k=new a.Vec3;for(var l=0;l<g;l++){j[l].copy(k),e.vmult(k,k),k.vadd(d,k);var m=k.dot(c);if(h===null||m>h)h=m;if(i===null||m<i)i=m}if(i>h){var n=i;i=h,h=n}f[0]=h,f[1]=i}function p(b,c){var d=e.vertices[b[0]],f=e.vertices[b[1]],g=e.vertices[b[2]],h=new a.Vec3;s(d,f,g,h);var i=h.dot(d);return h.dot(c)>=i}function q(a,b){var c=e.faces[a],d=e.vertices[c[0]],f=e.vertices[c[1]],g=e.vertices[c[2]];return s(d,f,g,b)}function r(a,b){var c=e.faces[a],d=e.faceNormals[a],f=e.vertices[c[0]],g=-d.dot(f);return g}function s(b,c,d,e){var f=new a.Vec3,g=new a.Vec3;c.vsub(b,g),d.vsub(c,f),f.cross(g,e),e.isZero()||e.normalize()}function t(a){var b=e.faces[a],c="";for(var d=0;d<b.length;d++)c+=" ("+e.vertices[b[d]]+")";return c}function u(a,b){return a[0]===b[1]&&a[1]===b[0]}function v(){return(Math.random()-.5)*2*1e-6}var e=this;a.Shape.call(this),this.type=a.Shape.types.CONVEXPOLYHEDRON,this.vertices=[],this.faces=c,this.faceNormals=d,this.uniqueEdges=[];for(pi in b){var f=b[pi];if(!(f instanceof a.Vec3))throw"Argument 1 must be instance of CANNON.Vec3";this.vertices.push(f)}for(var g=0;g<c.length;g++){var h=c[g].length,i=h;for(var j=0;j<i;j++){var k=(j+1)%h,l=new a.Vec3;this.vertices[c[g][j]].vsub(this.vertices[c[g][k]],l),l.normalize();var m=!1;for(var f=0;f<this.uniqueEdges.length;f++)if(this.uniqueEdges[f].almostEquals(l)||this.uniqueEdges[f].almostEquals(l)){m=!0;break}m||this.uniqueEdges.push(l);if(l)l.face1=g;else{var n;n.m_face0=g,edges.insert(vp,n)}}}this.testSepAxis=function(a,b,c,d,e,f){var g=[],h=[],i=this;o(i,a,c,d,g),o(b,a,e,f,h);var j=g[0],k=g[1],l=h[0],m=h[1];if(j<m||l<k)return!1;var n=j-m,p=l-k;return depth=n<p?n:p,depth},this.findSeparatingAxis=function(b,c,d,e,f,g){var h=Infinity,i=this,j=0,k=i.faces.length,l=new a.Vec3;for(var m=0;m<k;m++){i.faceNormals[m].copy(l),d.vmult(l,l);var n=i.testSepAxis(l,b,c,d,e,f);if(n===!1)return!1;n<h&&(h=n,l.copy(g))}var o=new a.Vec3,p=b.faces.length;for(var m=0;m<p;m++){b.faceNormals[m].copy(o),f.vmult(o,o),j++;var n=i.testSepAxis(o,b,c,d,e,f);if(n===!1)return!1;n<h&&(h=n,o.copy(g))}var q,r,s,t,u=0,v=new a.Vec3,w=new a.Vec3,x=new a.Vec3;for(var y=0;y<i.uniqueEdges.length;y++){i.uniqueEdges[y].copy(v),d.vmult(v,v);for(var z=0;z<b.uniqueEdges.length;z++){b.uniqueEdges[z].copy(w),f.vmult(w,w),v.cross(w,x),u++;if(!x.almostZero()){x.normalize();var A=i.testSepAxis(x,b,c,d,e,f);if(A===!1)return!1;A<h&&(h=A,x.copy(g))}}}var B=new a.Vec3;return e.vsub(c,B),B.dot(g)>0&&g.negate(g),!0},this.clipAgainstHull=function(b,c,d,e,f,g,h,i,j){if(!(b instanceof a.Vec3))throw new Error("posA must be Vec3");if(!(c instanceof a.Quaternion))throw new Error("quatA must be Quaternion");var k=this,l=i,m=-1,n=-Infinity,o=new a.Vec3;for(var p=0;p<d.faces.length;p++){d.faceNormals[p].copy(o),f.vmult(o,o),e.vadd(o,o);var q=o.dot(g);q>n&&(n=q,m=p)}var r=[];polyB=d.faces[m];var s=polyB.length;for(var t=0;t<s;t++){var u=d.vertices[polyB[t]],v=new a.Vec3;u.copy(v),f.vmult(v,v),e.vadd(v,v),r.push(v)}m>=0&&this.clipFaceAgainstHull(g,b,c,r,h,i,j)},this.clipFaceAgainstHull=function(b,c,d,e,f,g,h){if(!(b instanceof a.Vec3))throw new Error("sep normal must be vector");if(!(e instanceof Array))throw new Error("world verts must be array");f=Number(f),g=Number(g);var i=this,j=[],k=e,l=j,m=-1,n=Infinity,o=new a.Vec3;for(var p=0;p<i.faces.length;p++){i.faceNormals[p].copy(o),d.vmult(o,o),c.vadd(o,o);var q=o.dot(b);q<n&&(n=q,m=p)}if(m<0){console.log("--- did not find any closest face... ---");return}var s=i.faces[m];s.connectedFaces=[];for(var t=0;t<i.faces.length;t++)for(var u=0;u<i.faces[t].length;u++)s.indexOf(i.faces[t][u])!==-1&&t!==m&&s.connectedFaces.indexOf(t)===-1&&s.connectedFaces.push(t);var v=k.length,w=s.length,x=new a.Vec3,y=new a.Vec3,z=new a.Vec3,A=new a.Vec3,B=[];for(var C=0;C<w;C++){var D=i.vertices[s[C]],E=i.vertices[s[(C+1)%w]];D.vsub(E,x),x.copy(y),d.vmult(y,y),c.vadd(y,y),this.faceNormals[m].copy(z),d.vmult(z,z),c.vadd(z,z),y.cross(z,A),A.negate(A);var F=new a.Vec3;D.copy(F),d.vmult(F,F),c.vadd(F,F);var G=-F.dot(A),H=s.connectedFaces[C],I=new a.Vec3;this.faceNormals[H].copy(I);var J=r(H),K=new a.Vec3;I.copy(K),d.vmult(K,K);var L=J-K.dot(c);this.clipFaceAgainstPlane(k,l,K,L);while(k.length)k.shift();while(l.length)k.push(l.shift())}var I=new a.Vec3;this.faceNormals[m].copy(I);var J=r(m),K=new a.Vec3;I.copy(K),d.vmult(K,K);var L=J-K.dot(c);for(var t=0;t<k.length;t++){var M=K.dot(k[t])+L;M<=f&&(console.log("clamped: depth="+M+" to minDist="+(f+"")),M=f);if(M<=g){var N=k[t],O={point:N,normal:K,depth:M};h.push(O)}}},this.clipFaceAgainstPlane=function(b,c,d,e){if(d instanceof a.Vec3){if(b instanceof Array){if(c instanceof Array){var f,g,h=b.length;if(h<2)return c;var i=b[b.length-1],j=b[0];f=d.dot(i)+e;for(var k=0;k<h;k++){j=b[k],g=d.dot(j)+e;if(f<0)if(g<0){var l=new a.Vec3;j.copy(l),c.push(l)}else{var l=new a.Vec3;i.lerp(j,f/(f-g),l),c.push(l)}else if(g<0){var l=new a.Vec3;i.lerp(j,f/(f-g),l),c.push(l),c.push(j)}i=j,f=g}return c}throw new Error("outvertices must be Array, "+c+" given")}throw new Error("invertices must be Array, "+b+" given")}throw new Error("planeNormal must be Vec3, "+d+" given")};var e=this;this.calculateLocalInertia=function(a,b){var c=this.aabbmax.x-this.aabbmin.x,d=this.aabbmax.y-this.aabbmin.y,e=this.aabbmax.z-this.aabbmin.z;b.x=1/12*a*(2*d*2*d+2*e*2*e),b.y=1/12*a*(2*c*2*c+2*e*2*e),b.z=1/12*a*(2*d*2*d+2*c*2*c)},this.computeAABB=function(){var a=this.vertices.length,b=this.aabbmin,c=this.aabbmax,d=this.vertices;b.set(Infinity,Infinity,Infinity),c.set(-Infinity,-Infinity,-Infinity);for(var e=0;e<a;e++){var f=d[e];f.x<b.x?b.x=f.x:f.x>c.x&&(c.x=f.x),f.y<b.y?b.y=f.y:f.y>c.y&&(c.y=f.y),f.z<b.z?b.z=f.z:f.z>c.z&&(c.z=f.z)}},this.boundingSphereRadius=function(){var a=0;for(var b=0;b<this.vertices.length;b++){var c=this.vertices[b].norm2();c>a&&(a=c)}return Math.sqrt(a)},this.computeAABB()},a.ConvexPolyhedron.prototype=new a.Shape,a.ConvexPolyhedron.prototype.constructor=a.ConvexPolyhedron,a.Solver=function(a,b,c,d,e,f,g){this.iterations=f||10,this.h=g||1/60,this.a=a,this.b=b,this.eps=c,this.k=d,this.d=e,this.reset(0),this.debug=!1,this.debug&&console.log("a:",a,"b",b,"eps",c,"k",d,"d",e)},a.Solver.prototype.reset=function(a){this.G=[],this.MinvTrace=[],this.Fext=[],this.q=[],this.qdot=[],this.n=0,this.upper=[],this.lower=[],this.hasupper=[],this.haslower=[],this.i=[],this.j=[],this.vxlambda=[],this.vylambda=[],this.vzlambda=[],this.wxlambda=[],this.wylambda=[],this.wzlambda=[];for(var b=0;b<a;b++)this.vxlambda.push(0),this.vylambda.push(0),this.vzlambda.push(0),this.wxlambda.push(0),this.wylambda.push(0),this.wzlambda.push(0)},a.Solver.prototype.addConstraint=function(a,b,c,d,e,f,g,h,i){this.debug&&(console.log("Adding constraint ",this.n," between body ",h," and ",i),console.log("G:",a),console.log("q:",c),console.log("qdot:",d),console.log("Fext:",e),console.log("lower:",f),console.log("upper:",g));for(var j=0;j<12;j++)this.q.push(c[j]),this.qdot.push(d[j]),this.MinvTrace.push(b[j]),this.G.push(a[j]),this.Fext.push(e[j]);return this.upper.push(g),this.hasupper.push(!isNaN(g)),this.lower.push(f),this.haslower.push(!isNaN(f)),this.i.push(h),this.j.push(i),this.n+=1,this.n-1},a.Solver.prototype.addConstraint2=function(a,b,c){a.update();for(var d=0;d<a.equations.length;d++){var e=a.equations[d];this.addConstraint([e.G1.x,e.G1.y,e.G1.z,e.G2.x,e.G2.y,e.G2.z,e.G3.x,e.G3.y,e.G3.z,e.G4.x,e.G4.y,e.G4.z],[e.iM1.x,e.iM1.y,e.iM1.z,e.iM2.x,e.iM2.y,e.iM2.z,e.iM3.x,e.iM3.y,e.iM3.z,e.iM4.x,e.iM4.y,e.iM4.z],[e.g1.x,e.g1.y,e.g1.z,e.g2.x,e.g2.y,e.g2.z,e.g3.x,e.g3.y,e.g3.z,e.g4.x,e.g4.y,e.g4.z],[e.W1.x,e.W1.y,e.W1.z,e.W2.x,e.W2.y,e.W2.z,e.W3.x,e.W3.y,e.W3.z,e.W4.x,e.W4.y,e.W4.z],[e.f1.x,e.f1.y,e.f1.z,e.f2.x,e.f2.y,e.f2.z,e.f3.x,e.f3.y,e.f3.z,e.f4.x,e.f4.y,e.f4.z],e.lambdamin,e.lambdamax,b,c)}},a.Solver.prototype.addNonPenetrationConstraint=function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s){var t=f.cross(e),u=m.vsub(l),v=d.vadd(g).vsub(c.vadd(f)),w=v.dot(e);w<0&&(this.debug&&(console.log("i:",a,"j",b,"xi",c.toString(),"xj",d.toString()),console.log("ni",e.toString(),"ri",f.toString(),"rj",g.toString()),console.log("iMi",h.toString(),"iMj",i.toString(),"iIi",j.toString(),"iIj",k.toString(),"vi",l.toString(),"vj",m.toString(),"wi",n.toString(),"wj",o.toString(),"fi",p.toString(),"fj",q.toString(),"taui",r.toString(),"tauj",s.toString())),this.addConstraint([-e.x,-e.y,-e.z,-t.x,-t.y,-t.z,e.x,e.y,e.z,t.x,t.y,t.z],[h.x,h.y,h.z,j.z,j.y,j.z,i.x,i.y,i.z,k.z,k.y,k.z],[-v.x,-v.y,-v.z,0,0,0,v.x,v.y,v.z,0,0,0],[-u.x,-u.y,-u.z,0,0,0,u.x,u.y,u.z,0,0,0],[p.x,p.y,p.z,r.x,r.y,r.z,q.x,q.y,q.z,s.x,s.y,s.z],0,"inf",a,b))},a.Solver.prototype.solve=function(){var a=this.n,b=[],c=[],d=[],e=[],f=[],g=[],h=this.iterations,i=this.G,j=this.debug,k=this.a,l=this.eps,m=this.lower,n=this.haslower,o=this.upper,p=this.hasupper,q=this.vxlambda,r=this.vylambda,s=this.vzlambda,t=this.wxlambda,u=this.wylambda,v=this.wzlambda,w=this.MinvTrace;for(var x=0;x<a;x++){b.push(0),c.push(0),e.push(0),f.push(0),g.push(0);for(var y=0;y<12;y++)c.push(0)}for(var z=0;z<h;z++)for(var A=0;A<a;A++){var B=this.i[A],C=this.j[A],D=12*A;if(!g[A]){var E=0,F=0,G=0,H=0;for(var x=0;x<12;x++){var I=D+x;E+=i[I]*w[I]*i[I],F+=i[I]*this.q[I],G+=i[I]*this.qdot[I],H+=i[I]*w[I]*this.Fext[I]}f[A]=1/(E+l),e[A]=-k*F-this.b*G-this.h*H,g[A]=1,j&&(console.log("G_Minv_Gt["+A+"]:",E),console.log("Gq["+A+"]:",F),console.log("GW["+A+"]:",G),console.log("GMinvf["+A+"]:",H))}var J=0;B>=0&&(J+=i[0+D]*q[B],J+=i[1+D]*r[B],J+=i[2+D]*s[B],J+=i[3+D]*t[B],J+=i[4+D]*u[B],J+=i[5+D]*v[B],j&&isNaN(J)&&console.log("found NaN Gulambda",q)),C!==-1&&(J+=i[6+D]*q[C],J+=i[7+D]*r[C],J+=i[8+D]*s[C],J+=i[9+D]*t[C],J+=i[10+D]*u[C],J+=i[11+D]*v[C]),c[A]=f[A]*(e[A]-J-l*b[A]),j&&console.log("dlambda["+A+"]=",c[A],"rest = ",f[A],e[A],J,l,b[A],A,B,C),b[A]=b[A]+c[A],n[A]&&b[A]<m[A]&&(j&&console.log("hit lower bound for constraint "+A+", truncating "+b[A]+" to the bound "+m[A]),b[A]=m[A],c[A]=m[A]-b[A]),p&&b[A]>o[A]&&(j&&console.log("hit upper bound for constraint "+A+", truncating "+b[A]+" to the bound "+o[A]),b[A]=o[A],c[A]=o[A]-b[A]),B!==-1&&(q[B]+=c[A]*w[D+0]*i[D+0],r[B]+=c[A]*w[D+1]*i[D+1],s[B]+=c[A]*w[D+2]*i[D+2],t[B]+=c[A]*w[D+3]*i[D+3],u[B]+=c[A]*w[D+4]*i[D+4],v[B]+=c[A]*w[D+5]*i[D+5]),C!==-1&&(q[C]+=c[A]*w[D+6]*i[D+6],r[C]+=c[A]*w[D+7]*i[D+7],s[C]+=c[A]*w[D+8]*i[D+8],t[C]+=c[A]*w[D+9]*i[D+9],u[C]+=c[A]*w[D+10]*i[D+10],v[C]+=c[A]*w[D+11]*i[D+11])}if(j)for(var x=0;x<this.vxlambda.length;x++)console.log("dv["+x+"]=",q[x],r[x],s[x],t[x],u[x],v[x])},a.ObjectPool=function(){this.objects=[],this.type=Object},a.ObjectPool.prototype.release=function(){for(var a in arguments)this.objects.push(arguments[a])},a.ObjectPool.prototype.get=function(){return this.objects.length===0?this.constructObject():this.objects.pop()},a.ObjectPool.prototype.constructObject=function(){throw new Error("constructObject() not implemented in this ObjectPool subclass yet!")},a.Vec3Pool=function(){a.ObjectPool.call(this),this.type=a.Vec3},a.Vec3Pool.prototype=new a.ObjectPool,a.Vec3Pool.prototype.constructObject=function(){return new a.Vec3},a.Material=function(a){this.name=a,this.id=-1},a.ContactMaterial=function(a,b,c,d){this.id=-1,this.materials=[a,b],this.friction=c!=undefined?Number(c):.3,this.restitution=d!=undefined?Number(d):.3},a.World=function(){this.time=0,this.stepnumber=0,this.spook_k=500,this.spook_d=4,this.default_dt=1/60,this.last_dt=this.default_dt,this.nextId=0,this.gravity=new a.Vec3,this.broadphase=null,this.bodies=[];var b=this;this.spook_a=function(a){return 4/(a*(1+4*b.spook_d))},this.spook_b=4*this.spook_d/(1+4*this.spook_d),this.spook_eps=function(a){return 4/(a*a*b.spook_k*(1+4*b.spook_d))},this.solver=new a.Solver(this.spook_a(1/60),this.spook_b,this.spook_eps(1/60)*.1,this.spook_k,this.spook_d,5,1/60),this.constraints=[],this.contactgen=new a.ContactGenerator,this.materials=[],this.contactmaterials=[],this.mats2cmat=[],this.temp={gvec:new a.Vec3,vi:new a.Vec3,vj:new a.Vec3,wi:new a.Vec3,wj:new a.Vec3,t1:new a.Vec3,t2:new a.Vec3,rixn:new a.Vec3,rjxn:new a.Vec3,step_q:new a.Quaternion,step_w:new a.Quaternion,step_wq:new a.Quaternion}},a.World.prototype.getContactMaterial=function(b,c){if(b instanceof a.Material&&c instanceof a.Material){var d=b.id,e=c.id;if(d<e){var f=d;d=e,e=f}return this.contactmaterials[this.mats2cmat[d+e*this.materials.length]]}},a.World.prototype._addImpulse=function(b,c,d,e,f,g,h,i){var j=d.crossmat(),k=e.crossmat(),l=this.inertiax[b]>0?1/this.inertiax[b]:0,m=new a.Mat3([l,0,0,0,l,0,0,0,l]);l=this.inertiax[c]>0?1/this.inertiax[c]:0;var n=new a.Mat3([l,0,0,0,l,0,0,0,l]),o=this.invm[b]+this.invm[c],p=new a.Mat3([o,0,0,0,o,0,0,0,o]),q=j.mmult(m.mmult(j)),r=k.mmult(n.mmult(k)),s=g.mult(-h*f.dot(g)),t=p.solve(s.vsub(f)),i=0;if(i>0){var u=g.mult(t.dot(g)),v=t.vsub(u);if(v.norm()>u.mult(i).norm()){var w=f.vsub(g.mult(f.dot(g))),x=w.mult(1/(w.norm()+1e-4)),y=-(1+h)*f.dot(g)/g.dot(p.vmult(g.vsub(x.mult(i))));t=g.mult(y).vsub(x.mult(i*y))}}var z=this.invm[b],A=this.invm[c];this.vx[b]+=t.x*z-(this.vx[c]-f.x),this.vy[b]+=t.y*z-(this.vy[c]-f.y),this.vz[b]+=t.z*z-(this.vz[c]-f.z),this.vx[c]-=t.x*A+(this.vx[b]+f.x),this.vy[c]-=t.y*A+(this.vy[b]+f.y),this.vz[c]-=t.z*A+(this.vz[b]+f.z);var B=d.cross(t),C=B.mult(1/this.inertiax[b])},a.World.prototype.numObjects=function(){return this.bodies.length},a.World.prototype.clearCollisionState=function(a){var b=this.numObjects(),c=a.id;for(var d=0;d<b;d++){var e=d;c>e?this.collision_matrix[e+c*b]=0:this.collision_matrix[c+e*b]=0}},a.World.prototype.add=function(b){if(b instanceof a.RigidBody){var c=this.numObjects();this.bodies.push(b),b.id=this.id(),b.world=this,b.position.copy(b.initPosition),b.velocity.copy(b.initVelocity),b.angularVelocity.copy(b.initAngularVelocity),b.quaternion.copy(b.initQuaternion),this.collision_matrix=new Int16Array((c+1)*(c+1))}},a.World.prototype.addConstraint=function(b){b instanceof a.Constraint&&(this.constraints.push(b),b.id=this.id())},a.World.prototype.id=function(){return this.nextId++},a.World.prototype.remove=function(b){if(b instanceof a.RigidBody){b.world=null;var c=this.numObjects(),d=this.bodies;for(var e in d)d[e].id==b.id&&d.splice(e,1);this.collision_matrix=new Int16Array((c-1)*(c-1))}},a.World.prototype.addMaterial=function(a){if(a.id==-1){this.materials.push(a),a.id=this.materials.length-1;var b=new Int16Array(this.materials.length*this.materials.length);for(var c=0;c<b.length;c++)b[c]=-1;for(var c=0;c<this.materials.length-1;c++)for(var d=0;d<this.materials.length-1;d++)b[c+this.materials.length*d]=this.mats2cmat[c+(this.materials.length-1)*d];this.mats2cmat=b}},a.World.prototype.addContactMaterial=function(a){this.addMaterial(a.materials[0]),this.addMaterial(a.materials[1]),a.materials[0].id>a.materials[1].id?(i=a.materials[0].id,j=a.materials[1].id):(j=a.materials[0].id,i=a.materials[1].id),this.contactmaterials.push(a),a.id=this.contactmaterials.length-1,this.mats2cmat[i+this.materials.length*j]=a.id},a.World.prototype._id2index=function(a){for(var b=0;b<this.bodies.length;b++)if(this.bodies[b].id===a)return b;return-1},a.World.prototype.step=function(b){function r(a,b,c,e){if(c==0&&a<b||c==-1&&a>b){var f=b;b=a,a=f}if(e===undefined)return d.collision_matrix[a+b*d.numObjects()];d.collision_matrix[a+b*d.numObjects()]=parseInt(e)}var c=this,d=this,e=this.numObjects(),f=this.bodies;b==undefined&&(this.last_dt?b=this.last_dt:b=this.default_dt);for(var g in f){var h=f[g];if(h.motionstate&a.RigidBody.DYNAMIC){var i=f[g].force,j=f[g].mass;i.x+=c.gravity.x*j,i.y+=c.gravity.y*j,i.z+=c.gravity.z*j}}var k=this.broadphase.collisionPairs(this),l=k[0],m=k[1],n=a.Shape.types.SPHERE,o=a.Shape.types.PLANE,p=a.Shape.types.BOX,q=a.Shape.types.COMPOUND;for(var g in f)for(var s=0;s<g;s++)r(g,s,-1,r(g,s,0)),r(g,s,0,0);this.solver.reset(e);var t=this.contacts;this.contacts=[],this.contactgen.getContacts(l,m,this,this.contacts,t);var u=this.temp;for(var v=0;v<this.contacts.length;v++){var w=this.contacts[v],h=w.bi,x=w.bj,g=this._id2index(h.id),s=this._id2index(x.id),y=r(g,s,-1),z=.3,A=.2,B=this.getContactMaterial(h.material,x.material);B&&(z=B.friction,A=B.restitution);var C=u.gvec;C.set(x.position.x+w.rj.x-h.position.x-w.ri.x,x.position.y+w.rj.y-h.position.y-w.ri.y,x.position.z+w.rj.z-h.position.z-w.ri.z);var D=C.dot(w.ni);if(D<0){var E=h.velocity,F=h.angularVelocity,G=x.velocity,H=x.angularVelocity,I=w.ni,J=[u.t1,u.t2];I.tangents(J[0],J[1]);var K=E.vadd(F.cross(w.ri)),L=G.vadd(H.cross(w.rj)),M=L.vsub(K),N=H.cross(w.rj).vsub(F.cross(w.ri)),O=G.vsub(E),P=w.rj.cross(H).vsub(w.ri.cross(F));O.vsub(P,O);var Q=h.invMass,R=x.invMass,S=h.invInertia.x,T=h.invInertia.y,U=h.invInertia.z,V=x.invInertia.x,W=x.invInertia.y,X=x.invInertia.z,Y=u.rixn,Z=u.rjxn;w.ri.cross(I,Y),w.rj.cross(I,Z);var $=I.mult(M.dot(I)*.5),_=Y.unit().mult(N.dot(Y.unit())),ab=Z.unit().mult(-N.dot(Z.unit())),bb=w.ni.mult(D);this.solver.addConstraint([-I.x,-I.y,-I.z,-Y.x,-Y.y,-Y.z,I.x,I.y,I.z,Z.x,Z.y,Z.z],[Q,Q,Q,S,T,U,R,R,R,V,W,X],[-bb.x,-bb.y,-bb.z,0,0,0,bb.x,bb.y,bb.z,0,0,0],[-$.x,-$.y,-$.z,0,0,0,$.x,$.y,$.z,0,0,0],[h.force.x,h.force.y,h.force.z,h.tau.x,h.tau.y,h.tau.z,-x.force.x,-x.force.y,-x.force.z,-x.tau.x,-x.tau.y,-x.tau.z],0,"inf",g,s);if(z>0){var D=d.gravity.norm();for(var cb=0;cb<J.length;cb++){var db=J[cb],eb=w.ri.cross(db),fb=w.rj.cross(db),gb=db.mult(M.dot(db)),hb=eb.unit().mult(M.dot(eb.unit())),ib=fb.unit().mult(-M.dot(fb.unit()));this.solver.addConstraint([-db.x,-db.y,-db.z,-eb.x,-eb.y,-eb.z,db.x,db.y,db.z,fb.x,fb.y,fb.z],[Q,Q,Q,S,T,U,R,R,R,V,W,X],[0,0,0,0,0,0,0,0,0,0,0,0],[-gb.x,-gb.y,-gb.z,0,0,0,gb.x,gb.y,gb.z,0,0,0],[h.force.x,h.force.y,h.force.z,h.tau.x,h.tau.y,h.tau.z,x.force.x,x.force.y,x.force.z,x.tau.x,x.tau.y,x.tau.z],-z*100*(h.mass+x.mass),z*100*(h.mass+x.mass),g,s)}}}}for(var g=0;g<this.constraints.length;g++){var x=-1,h=-1;for(var s=0;s<this.bodies.length;s++)this.bodies[s].id===this.constraints[g].body_i.id?h=s:this.bodies[s].id===this.constraints[g].body_j.id&&(x=s);this.solver.addConstraint2(this.constraints[g],h,x)}var h;if(this.solver.n){this.solver.solve();for(
var g in f){h=f[g];if(h.motionstate&a.RigidBody.DYNAMIC){var jb=f[g];jb.velocity.x+=this.solver.vxlambda[g],jb.velocity.y+=this.solver.vylambda[g],jb.velocity.z+=this.solver.vzlambda[g],jb.angularVelocity.x+=this.solver.wxlambda[g],jb.angularVelocity.y+=this.solver.wylambda[g],jb.angularVelocity.z+=this.solver.wzlambda[g]}}}for(var g in f){h=f[g];if(h.motionstate&a.RigidBody.DYNAMIC){var kb=1-h.linearDamping,lb=1-h.angularDamping;h.velocity.mult(kb,h.velocity),h.angularVelocity.mult(lb,h.angularVelocity)}}for(var g in f){var h=f[g];h.preStep&&h.preStep.call(h)}var mb=u.step_q,nb=u.step_w,ob=u.step_wq,pb=a.RigidBody.DYNAMIC|a.RigidBody.KINEMATIC;for(var g in f){var jb=f[g];jb.motionstate&pb&&(jb.velocity.x+=jb.force.x*jb.invMass*b,jb.velocity.y+=jb.force.y*jb.invMass*b,jb.velocity.z+=jb.force.z*jb.invMass*b,jb.angularVelocity.x+=jb.tau.x*jb.invInertia.x*b,jb.angularVelocity.y+=jb.tau.y*jb.invInertia.y*b,jb.angularVelocity.z+=jb.tau.z*jb.invInertia.z*b,jb.position.x+=jb.velocity.x*b,jb.position.y+=jb.velocity.y*b,jb.position.z+=jb.velocity.z*b,nb.set(jb.angularVelocity.x,jb.angularVelocity.y,jb.angularVelocity.z,0),nb.mult(jb.quaternion,ob),jb.quaternion.x+=b*.5*ob.x,jb.quaternion.y+=b*.5*ob.y,jb.quaternion.z+=b*.5*ob.z,jb.quaternion.w+=b*.5*ob.w,c.stepnumber%3===0&&jb.quaternion.normalizeFast()),jb.force.set(0,0,0),jb.tau.set(0,0,0)}c.time+=b,c.stepnumber+=1;for(var g in f){var h=f[g];h.postStep&&h.postStep.call(h)}},a.ContactPoint=function(b,c,d,e,f){if(!(b instanceof a.RigidBody&&c instanceof a.RigidBody))throw new Error("Arguments 1 and 2 must be instances of CANNON.RigidBody.");this.ri=new a.Vec3,this.rj=new a.Vec3,this.ni=new a.Vec3,d&&d.copy(this.ri),e&&e.copy(this.rj),f&&f.copy(this.ni),this.bi=b,this.bj=c},a.ContactGenerator=function(){function d(e,f,g,h,i,j,k,l,m){function p(c,d){if(b.length){var e=b.pop();return e.bi=c,e.bj=d,e}return new a.ContactPoint(c,d)}function q(a){var b;b=a.ri,a.ri=a.rj,a.rj=b,a.ni.negate(a.ni),b=a.bi,a.bi=a.bj,a.bj=b}function r(a,b,c,e,f,g,h,i,j){for(var k=0;k<c.childShapes.length;k++){var l=[];d(l,b,c.childShapes[k],e,f.vadd(h.vmult(c.childOffsets[k])),g,h.mult(c.childOrientations[k]),i,j);for(var m=0;m<l.length;m++)l[m].rj.vadd(h.vmult(c.childOffsets[k]),l[m].rj),a.push(l[m])}}var n=!1;if(f.type>g.type){var o;o=g,g=f,f=o,o=i,i=h,h=o,o=k,k=j,j=o,o=m,m=l,l=o,n=!0}if(f.type==a.Shape.types.SPHERE){if(g.type==a.Shape.types.SPHERE){var s=p(l,m);i.vsub(h,s.ni),s.ni.normalize(),s.ni.copy(s.ri),s.ni.copy(s.rj),s.ri.mult(f.radius,s.ri),s.rj.mult(-g.radius,s.rj),e.push(s)}else if(g.type==a.Shape.types.PLANE){var s=p(l,m);g.normal.copy(s.ni),k.vmult(s.ni,s.ni),s.ni.negate(s.ni),s.ni.normalize(),s.ni.mult(f.radius,s.ri);var t=h.vsub(i),u=s.ni.mult(s.ni.dot(t));s.rj=t.vsub(u),u.norm()<=f.radius&&e.push(s)}else if(g.type==a.Shape.types.BOX){var v=h.vsub(i),w=g.getSideNormals(!0,k),x=f.radius,y=[],z=!1;for(var A=0;A<w.length&&!z;A++){var B=w[A].copy(),C=B.norm();B.normalize();var D=v.dot(B);if(D<C+x&&D>0){var E=w[(A+1)%3].copy(),F=w[(A+2)%3].copy(),G=E.norm(),H=F.norm();E.normalize(),F.normalize();var I=v.dot(E),J=v.dot(F);if(I<G&&I>-G&&J<H&&J>-H){z=!0;var s=p(l,m);B.mult(-x,s.ri),B.copy(s.ni),s.ni.negate(s.ni),B.mult(C).vadd(E.mult(I)).vadd(F.mult(J),s.rj),e.push(s)}}}var K=c.get();for(var L=0;L<2&&!z;L++)for(var M=0;M<2&&!z;M++)for(var N=0;N<2&&!z;N++){K.set(0,0,0),L?K.vadd(w[0],K):K.vsub(w[0],K),M?K.vadd(w[1],K):K.vsub(w[1],K),N?K.vadd(w[2],K):K.vsub(w[2],K);var O=i.vadd(K).vsub(h);if(O.norm()<x){z=!0;var s=p(l,m);O.copy(s.ri),s.ri.normalize(),s.ri.copy(s.ni),s.ri.mult(x,s.ri),K.copy(s.rj),e.push(s)}}c.release(K),K=null;var P=new a.Vec3,Q=new a.Vec3,s=new a.Vec3,R=new a.Vec3,S=new a.Vec3;for(var L=0;L<w.length&&!z;L++)for(var M=0;M<w.length&&!z;M++)if(L%3!=M%3){w[M].cross(w[L],P),P.normalize(),w[L].vadd(w[M],Q),h.copy(s),s.vsub(Q,s),s.vsub(i,s);var T=s.dot(P);P.mult(T,R);var N=0;while(N==L%3||N==M%3)N++;h.copy(S),S.vsub(R,S),S.vsub(Q,S),S.vsub(i,S);var U=Math.abs(T),V=S.norm();if(U<w[N].norm()&&V<x){z=!0;var W=p(l,m);Q.vadd(R,W.rj),W.rj.copy(W.rj),S.negate(W.ni),W.ni.normalize(),W.rj.copy(W.ri),W.ri.vadd(i,W.ri),W.ri.vsub(h,W.ri),W.ri.normalize(),W.ri.mult(x,W.ri),e.push(W)}}}else if(g.type==a.Shape.types.COMPOUND)r(e,f,g,h,i,j,k,l,m);else if(g.type==a.Shape.types.CONVEXPOLYHEDRON)throw new Error("sphere/convexpolyhedron contacts not implemented yet.")}else if(f.type==a.Shape.types.PLANE){if(g.type==a.Shape.types.PLANE)throw"Plane-plane collision... wait, you did WHAT?";if(g.type==a.Shape.types.BOX){var X=f.normal.copy(),Y=0,Z=g.getCorners(k);for(var A=0;A<Z.length&&Y<=4;A++){var s=p(l,m),$=Z[A].vadd(i);Z[A].copy(s.rj);var _=$.vsub(h),ab=X.dot(_);if(ab<=0){Y++;var bb=X.mult(ab);_.vsub(bb,s.ri),X.copy(s.ni),e.push(s)}}}else if(g.type==a.Shape.types.COMPOUND)r(e,f,g,h,i,j,k,l,m);else if(g.type==a.Shape.types.CONVEXPOLYHEDRON){console.log(c.objects.length);var cb=c.get(),db=c.get();f.normal.tangents(cb,db),cb.mult(1e5,cb),db.mult(1e5,db);var X=c.get();f.normal.copy(X);var eb=[new a.Vec3(-cb.x-db.x-X.x,-cb.y-db.y-X.y,-cb.z-db.z-X.z),new a.Vec3(cb.x-db.x+0*X.x,cb.y-db.y+0*X.y,cb.z-db.z+0*X.z),new a.Vec3(cb.x+db.x-X.x,cb.y+db.y-X.y,cb.z+db.z-X.z),new a.Vec3(-cb.x+db.x-X.x,-cb.y+db.y-X.y,-cb.z+db.z-X.z),new a.Vec3(-cb.x-db.x+0*X.x,-cb.y-db.y+0*X.y,-cb.z-db.z+0*X.z),new a.Vec3(+cb.x-db.x+0*X.x,cb.y-db.y+0*X.y,cb.z-db.z+0*X.z),new a.Vec3(+cb.x+db.x+0*X.x,+cb.y+db.y+0*X.y,cb.z+db.z+0*X.z),new a.Vec3(-cb.x+db.x+0*X.x,-cb.y+db.y+0*X.y,-cb.z+db.z+0*X.z)];cb.normalize(),db.normalize();var fb=new a.ConvexPolyhedron(eb,[[0,1,2,3],[4,5,6,7],[0,1,4,5],[2,3,6,7],[0,3,4,7],[1,2,5,6]],[new a.Vec3(-X.x,-X.y,-X.z),new a.Vec3(X.x,X.y,X.z),new a.Vec3(-db.x,-db.y,-db.z),new a.Vec3(db.x,db.y,db.z),new a.Vec3(-cb.x,-cb.y,-cb.z),new a.Vec3(cb.x,cb.y,cb.z)]),gb=c.get();X.negate(gb);var hb=c.get();if(g.testSepAxis(gb,fb,i,k,h,j)!==!1){var W=[];fb.clipAgainstHull(h,j,g,i,k,gb,-100,100,W);for(var L=0;L<W.length;L++){var s=p(l,m);gb.negate(s.ni),W[L].normal.negate(hb),hb.mult(W[L].depth,hb),s.ri.set(W[L].point.x+hb.x,W[L].point.y+hb.y,W[L].point.z+hb.z),s.rj.set(W[L].point.x,W[L].point.y,W[L].point.z),s.rj.vsub(i,s.rj),s.ri.vsub(h,s.ri),e.push(s)}}c.release(hb,cb,db,gb,X)}}else if(f.type==a.Shape.types.BOX)g.type==a.Shape.types.BOX?d(e,f.convexPolyhedronRepresentation,g.convexPolyhedronRepresentation,h,i,j,k,l,m):g.type==a.Shape.types.COMPOUND?r(e,f,g,h,i,j,k,l,m):g.type==a.Shape.types.CONVEXPOLYHEDRON&&d(e,f.convexPolyhedronRepresentation,g,h,i,j,k,l,m);else if(f.type==a.Shape.types.COMPOUND)g.type==a.Shape.types.COMPOUND?r(e,f,g,h,i,j,k,l,m):g.type==a.Shape.types.CONVEXPOLYHEDRON&&r(e,g,f,i,h,k,j,m,l);else if(f.type==a.Shape.types.CONVEXPOLYHEDRON&&g.type==a.Shape.types.CONVEXPOLYHEDRON){var gb=new a.Vec3;if(f.findSeparatingAxis(g,h,j,i,k,gb)){var W=[];f.clipAgainstHull(h,j,g,i,k,gb,-100,100,W);for(var L=0;L<W.length;L++){var s=p(l,m);gb.negate(s.ni);var hb=new a.Vec3;W[L].normal.negate(hb),hb.mult(W[L].depth,hb),s.ri.set(W[L].point.x+hb.x,W[L].point.y+hb.y,W[L].point.z+hb.z),s.rj.set(W[L].point.x,W[L].point.y,W[L].point.z),s.rj.vsub(i,s.rj),s.ri.vsub(h,s.ri),e.push(s)}}}for(var ib=0;n&&ib<e.length;ib++)q(e[ib])}this.contactReduction=!0;var b=[],c=new a.Vec3Pool;this.reduceContacts=function(a){},this.getContacts=function(a,c,e,f,g){for(var h=0;g&&h<g.length;h++)b.push(g[h]);for(var i=0;i<a.length;i++){var j=a[i],k=c[i];d(f,j.shape,k.shape,j.position,k.position,j.quaternion,k.quaternion,j,k)}}},a.Constraint=function(){this.equations=[],this.id=-1},a.Constraint.prototype.constructor=a.Constraint,a.Constraint.prototype.update=function(){throw"update() not implemented in this Constraint subclass!"},a.ContactConstraint=function(b,c,d){a.Constraint.call(this),this.body_i=b,this.body_j=c,this.contact=contact,this.slipForce=d,this.unused_equations=[],this.temp={rixn:new a.Vec3,rjxn:new a.Vec3,t1:new a.Vec3,t2:new a.Vec3}},a.ContactConstraint.prototype=new a.Constraint,a.ContactConstraint.prototype.constructor=a.ContactConstraint,a.ContactConstraint.prototype.update=function(){var a=this.body_i,b=this.body_j,d=a.velocity,e=a.angularVelocity,f=b.velocity,h=b.angularVelocity,i=[this.temp.t1,this.temp.t2];for(var j in a.contacts)for(var k in b.contacts)if(a.contacts[j].to.id==b.id&&b.contacts[k].to.id==a.id){var l=a.contacts[j].r,m=b.contacts[k].r,o=a.contacts[j].n;n.tangents(i[0],i[1]);var p=d.vadd(e.cross(c.ri)),q=f.vadd(h.cross(c.rj)),r=q.vsub(p),s=h.cross(c.rj).vsub(e.cross(c.ri)),t=f.vsub(d),u=c.rj.cross(h).vsub(c.ri.cross(e));t.vsub(u,t);var v=a.invMass,w=b.invMass,x=a.invInertia.x,y=a.invInertia.y,z=a.invInertia.z,A=b.invInertia.x,B=b.invInertia.y,C=b.invInertia.z,D=this.temp.rixn,E=this.temp.rjxn;c.ri.cross(n,D),c.rj.cross(n,E);var F=n.mult(r.dot(n)),G=D.unit().mult(s.dot(D.unit())),H=E.unit().mult(-s.dot(E.unit())),I=c.ni.mult(g);n.negate(eq.G1),D.negate(eq.G2),n.copy(eq.G3),E.copy(eq.G4),eq.setDefaultMassProps(),I.negate(eq.g1),I.copy(eq.g3),F.negate(eq.W1),F.copy(eq.W3),a.force.copy(eq.f1),a.tau.copy(eq.f2),b.force.copy(eq.f3),b.tau.copy(eq.f4),eq.lambdamin=0,eq.lambdamax="inf"}},a.DistanceConstraint=function(b,c,d){a.Constraint.call(this),this.body_i=b,this.body_j=c,this.distance=Number(d);var e=new a.Equation(b,c instanceof a.RigidBody?c:null);this.equations.push(e)},a.DistanceConstraint.prototype=new a.Constraint,a.DistanceConstraint.prototype.constructor=a.DistanceConstraint,a.DistanceConstraint.prototype.update=function(){var b=this.equations[0],c=this.body_i,d=this.body_j,e=d instanceof a.RigidBody;e?d.position.vsub(c.position,b.G1):c.position.vsub(d,b.G1),b.G1.normalize(),b.G1.isZero()&&b.G1.set(1,0,0),b.G1.negate(b.G3),b.setDefaultMassProps(),b.setDefaultForce(),b.g1.set((e?d.position.x:d.x)-c.position.x-b.G1.x*this.distance,(e?d.position.y:d.y)-c.position.y-b.G1.y*this.distance,(e?d.position.z:d.z)-c.position.z-b.G1.z*this.distance),b.g1.negate(b.g1),b.g1.negate(b.g3)},a.DistanceConstraint.prototype.setMaxForce=function(a){this.equations[0].lambdamax=Math.abs(a),this.equations[0].lambdamin=-this.equations[0].lambdamax},a.Equation=function(b,c){this.G1=new a.Vec3,this.G2=new a.Vec3,this.G3=new a.Vec3,this.G4=new a.Vec3,this.iM1=new a.Vec3,this.iM2=new a.Vec3,this.iM3=new a.Vec3,this.iM4=new a.Vec3,this.g1=new a.Vec3,this.g2=new a.Vec3,this.g3=new a.Vec3,this.g4=new a.Vec3,this.W1=new a.Vec3,this.W2=new a.Vec3,this.W3=new a.Vec3,this.W4=new a.Vec3,this.f1=new a.Vec3,this.f2=new a.Vec3,this.f3=new a.Vec3,this.f4=new a.Vec3,this.lambdamax=1e6,this.lambdamin=-1e6,this.body_i=b,this.body_j=c},a.Equation.prototype.setDefaultMassProps=function(){var a=this.body_i,b=this.body_j;a&&(this.iM1.set(a.invMass,a.invMass,a.invMass),a.invInertia.copy(this.iM2)),b&&(this.iM3.set(b.invMass,b.invMass,b.invMass),b.invInertia.copy(this.iM4))},a.Equation.prototype.setDefaultForce=function(){var a=this.body_i,b=this.body_j;a&&(a.force.copy(this.f1),a.tau.copy(this.f2)),b&&(b.force.copy(this.f3),b.tau.copy(this.f4))},a.PointToPointConstraint=function(b,c,d,e){a.Constraint.call(this),this.body_i=b,this.body_j=d,this.pivot_i=c,this.pivot_j=e;for(var f=0;f<3;f++)this.equations.push(new Equation(b,d))},a.PointToPointConstraint.prototype=new a.Constraint,a.PointToPointConstraint.prototype.constructor=a.PointToPointConstraint,a.PointToPointConstraint.prototype.update=function(){},typeof module!="undefined"?module.exports=a:this.CANNON=a}).apply(this);