topical media & game development
mobile-query-three-plugins-cannonjs-vendor-cannon.js-build-cannon.min.js / js
/*
* 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,relpos:new a.Vec3}},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.Body.STATIC|a.Body.KINEMATIC,k=this.temp,l=k.r,m=k.normal,n=k.quat,o=k.relpos;for(var p=0;p<e;p++)for(var q=0;q<p;q++){var r=f[p],s=f[q];if(!((r.motionstate&j)===0&&!r.isSleeping()||(s.motionstate&j)===0&&!s.isSleeping()))continue;var t=r.shape,u=s.shape;if(t&&u){var v=t.type,w=u.type;if(v&h&&w&h){s.position.vsub(r.position,l);var x=t.boundingSphereRadius()+u.boundingSphereRadius();l.norm2()<x*x&&(c.push(r),d.push(s))}else if(v&h&&w&g.PLANE||w&h&&v&g.PLANE){var y=v===i?p:q,z=v!==i?p:q;f[z].position.vsub(f[y].position,l),m.set(0,0,1),f[y].quaternion.vmult(m,m);var A=l.dot(m)-f[z].shape.boundingSphereRadius();A<0&&(c.push(r),d.push(s))}}else if(!!t||!!u){var B=t?s:r,C=t?r:s,D=C.shape,E=D.type;if(E&h)E===g.SPHERE&&(B.position.vsub(C.position,o),Math.pow(D.radius,2)>=o.norm2()&&(c.push(B),d.push(C)));else if(E&g.PLANE){var F=C;F.quaternion.vmult(m,m),B.position.vsub(F.position,o),m.dot(o)<=0&&(c.push(B),d.push(C))}}}return[c,d]},a.Ray=function(b,c){function q(a,b,c){return c.vsub(a,m),o=m.dot(b),b.mult(o,n),n.vadd(a,n),p=c.distanceTo(n),p}function B(a,b,c,d){return d.vsub(b,m),c.vsub(b,z),a.vsub(b,A),r=m.dot(m),s=m.dot(z),t=m.dot(A),u=z.dot(z),v=z.dot(A),w=1/(r*u-s*s),x=(u*t-s*v)*w,y=(r*v-s*t)*w,x>=0&&y>=0&&x+y<1}this.origin=b||new a.Vec3,this.direction=c||new a.Vec3;var d=1e-4;this.setPrecision=function(a){d=a};var e=new a.Vec3,f=new a.Vec3,g=new a.Vec3,h=new a.Vec3,i=new a.Vec3,j=new a.Vec3,k=new a.Vec3,l=new a.Vec3;this.intersectBody=function(b){if(b.shape instanceof a.ConvexPolyhedron)return this.intersectShape(b.shape,b.quaternion,b.position,b);if(b.shape instanceof a.Box)return this.intersectShape(b.shape.convexPolyhedronRepresentation,b.quaternion,b.position,b);console.warn("Ray intersection is this far only implemented for ConvexPolyhedron and Box shapes.")},this.intersectShape=function(b,c,h,i){var m,n=[];if(b instanceof a.ConvexPolyhedron){var o=q(this.origin,this.direction,h);if(o>b.boundingSphereRadius())return n;var p,r,s=b.faces,t=b.vertices,u=b.faceNormals;for(fi=0;fi<s.length;fi++){var v=s[fi],w=u[fi],x=c,y=h;t[v[0]].copy(j),x.vmult(j,j),j.vadd(y,j),j.vsub(this.origin,j),x.vmult(w,k),p=this.direction.dot(k);if(Math.abs(p)<d)continue;r=k.dot(j)/p;if(r<0)continue;if(p<0){this.direction.mult(r,l),l.vadd(this.origin,l),t[v[0]].copy(e),x.vmult(e,e),y.vadd(e,e);for(var z=1;z<v.length-1;z++){t[v[z]].copy(f),t[v[z+1]].copy(g),x.vmult(f,f),x.vmult(g,g),y.vadd(f,f),y.vadd(g,g);if(B(l,e,f,g)){m={distance:this.origin.distanceTo(l),point:l.copy(),face:v,body:i},n.push(m);break}}}}}return n},this.intersectBodies=function(a){var b=[];for(var c=0,d=a.length;c<d;c++){var e=this.intersectBody(a[c]);Array.prototype.push.apply(b,e)}return b.sort(function(a,b){return a.distance-b.distance}),b};var m=new a.Vec3,n=new a.Vec3,o,p,r,s,t,u,v,w,x,y,z=new a.Vec3,A=new a.Vec3},a.Ray.prototype.constructor=a.Ray,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=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[g]+=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+=b.elements[d+g*3]*this.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){k=l;do m=l-k,f[m+e*g]+=f[m+e*h];while(--k);break}if(f[g+e*g]!==0)for(h=g+1;h<j;h++){var o=f[g+e*h]/f[g+e*g];k=l;do m=l-k,f[m+e*h]=m<=g?0:f[m+e*h]-f[m+e*g]*o;while(--k)}}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[b+3*a];this.elements[b+3*a]=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.Mat3.prototype.reverse=function(b){b=b||new a.Mat3;var c=3,d=6,e=new Float32Array(c*d),f,g;for(f=0;f<3;f++)for(g=0;g<3;g++)e[f+d*g]=this.elements[f+3*g];e[3]=1,e[9]=0,e[15]=0,e[4]=0,e[10]=1,e[16]=0,e[5]=0,e[11]=0,e[17]=1;var h=3,i=h,j,k=d,l;do{f=i-h;if(e[f+d*f]===0)for(g=f+1;g<i;g++)if(e[f+d*g]!==0){j=k;do l=k-j,e[l+d*f]+=e[l+d*g];while(--j);break}if(e[f+d*f]!==0)for(g=f+1;g<i;g++){var m=e[f+d*g]/e[f+d*f];j=k;do l=k-j,e[l+d*g]=l<=f?0:e[l+d*g]-e[l+d*f]*m;while(--j)}}while(--h);f=2;do{g=f-1;do{var m=e[f+d*g]/e[f+d*f];j=d;do l=d-j,e[l+d*g]=e[l+d*g]-e[l+d*f]*m;while(--j)}while(g--)}while(--f);f=2;do{var m=1/e[f+d*f];j=d;do l=d-j,e[l+d*f]=e[l+d*f]*m;while(--j)}while(f--);f=2;do{g=2;do{l=e[c+g+d*f];if(isNaN(l)||l===Infinity)throw"Could not reverse! A=["+this.toString()+"]";b.e(f,g,l)}while(g--)}while(f--);return b},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){var d=b.x,e=b.y,f=b.z,g=this.x,h=this.y,i=this.z;c=c||new a.Vec3;var j=[this.x,this.y,this.z],k=[b.x,b.y,b.z];return c.x=h*f-i*e,c.y=i*d-g*f,c.z=g*e-h*d,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=this.x,b=this.y,c=this.z,d=Math.sqrt(a*a+b*b+c*c);if(d>0){var e=1/d;this.x*=e,this.y*=e,this.z*=e}else this.x=0,this.y=0,this.z=0;return d},a.Vec3.prototype.unit=function(b){b=b||new a.Vec3;var c=this.x,d=this.y,e=this.z,f=Math.sqrt(c*c+d*d+e*e);return f>0?(f=1/f,b.x=c*f,b.y=d*f,b.z=e*f):(b.x=0,b.y=0,b.z=0),b},a.Vec3.prototype.norm=function(){var a=this.x,b=this.y,c=this.z;return Math.sqrt(a*a+b*b+c*c)},a.Vec3.prototype.norm2=function(){return this.dot(this)},a.Vec3.prototype.distanceTo=function(a){var b=this.x,c=this.y,d=this.z,e=a.x,f=a.y,g=a.z;return Math.sqrt((e-b)*(e-b)+(f-c)*(f-c)+(g-d)*(g-d))},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){var d=this.x,e=this.y,f=this.z;c.x=d+(a.x-d)*b,c.y=e+(a.y-e)*b,c.z=f+(a.z-f)*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.toAxisAngle=function(b){b=b||new a.Vec3,this.normalize();var c=2*Math.acos(this.w),d=Math.sqrt(1-this.w*this.w);return d<.001?(b.x=this.x,b.y=this.y,b.z=this.z):(b.x=this.x/d,b.y=this.y/d,b.z=this.z/d),[b,c]},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()};var b=new a.Vec3,d=new a.Vec3,e=new a.Vec3;a.Quaternion.prototype.mult=function(c,f){var g=this.w;return f==undefined&&(f=new a.Quaternion),b.set(this.x,this.y,this.z),d.set(c.x,c.y,c.z),f.w=g*c.w-b.dot(d),b.cross(d,e),f.x=g*d.x+c.w*b.x+e.x,f.y=g*d.y+c.w*b.y+e.y,f.z=g*d.z+c.w*b.z+e.z,f},a.Quaternion.prototype.inverse=function(b){var c=this.x,d=this.y,e=this.z,f=this.w;b==undefined&&(b=new a.Quaternion),this.conjugate(b);var g=1/(c*c+d*d+e*e+f*f);return b.x*=g,b.y*=g,b.z*=g,b.w*=g,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.calculateLocalAABB=function(){throw new Error(".calculateLocalAABB is not implemented for this Shape yet!")},a.Shape.types={SPHERE:1,PLANE:2,BOX:4,COMPOUND:8,CONVEXPOLYHEDRON:16},a.Body=function(b){a.EventTarget.apply(this),this.type=b;var c=this;this.world=null,this.preStep=null,this.postStep=null},a.Body.DYNAMIC=1,a.Body.STATIC=2,a.Body.KINEMATIC=4,a.Particle=function(b,c){if(typeof b!="number")throw new Error("Argument 1 (mass) must be a number.");if(!(typeof c=="undefined"||c instanceof a.Material))throw new Error("Argument 3 (material) must be an instance of CANNON.Material.");a.Body.call(this,"particle");var d=this;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.mass=b,this.invMass=b>0?1/b:0,this.material=c,this.linearDamping=.01,this.motionstate=b<=0?a.Body.STATIC:a.Body.DYNAMIC,this.allowSleep=!0;var e=0;this.isAwake=function(){return e==0},this.isSleepy=function(){return e==1},this.isSleeping=function(){return e==2},this.sleepSpeedLimit=.1,this.sleepTimeLimit=1e3;var f=(new Date).getTime();this.wakeUp=function(){e=0,d.dispatchEvent({type:"wakeup"})},this.sleep=function(){e=2},this.sleepTick=function(){d.allowSleep&&(e==0&&d.velocity.norm()<d.sleepSpeedLimit?(e=1,f=(new Date).getTime(),d.dispatchEvent({type:"sleepy"})):e==1&&d.velocity.norm()>d.sleepSpeedLimit?d.wakeUp():e==1&&(new Date).getTime()-f>d.sleepTimeLimit&&(e=2,d.dispatchEvent({type:"sleep"})))}},a.RigidBody=function(b,c,d){if(typeof b!="number")throw new Error("Argument 1 (mass) must be a number.");if(!(typeof d=="undefined"||d instanceof a.Material))throw new Error("Argument 3 (material) must be an instance of CANNON.Material.");a.Particle.call(this,b,d);var e=this;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.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.angularDamping=.01,this.aabbmin=new a.Vec3,this.aabbmax=new a.Vec3,this.calculateAABB()},a.RigidBody.constructor=a.RigidBody,a.RigidBody.prototype.calculateAABB=function(){this.shape.calculateWorldAABB(this.position,this.quaternion,this.aabbmin,this.aabbmax)},a.RigidBody.prototype.applyImpulse=function(b,c,d){d=d||1/60;var e=new a.Vec3,f=new a.Vec3;b.vsub(this.position,e),e.cross(c,f),this.velocity.vadd(c.mult(d),this.velocity),this.angularVelocity.vadd(f.mult(d),this.angularVelocity)},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.Sphere.prototype.calculateWorldAABB=function(a,b,c,d){var e=this.radius,f=["x","y","z"];for(var g=0;g<f.length;g++){var h=f[g];c[h]=a[h]-e,d[h]=a[h]+e}},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)));if(c!=undefined)for(var f=0;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()};var f=new a.Vec3,h=new a.Vec3;a.Box.prototype.forEachWorldCorner=function(a,b,c){var d=this.halfExtents,e=[[d.x,d.y,d.z],[-d.x,d.y,d.z],[-d.x,-d.y,d.z],[-d.x,-d.y,-d.z],[d.x,-d.y,-d.z],[d.x,d.y,-d.z],[-d.x,d.y,-d.z],[d.x,-d.y,d.z]];for(var g=0;g<e.length;g++)f.set(e[g][0],e[g][1],e[g][2]),b.vmult(f,f),a.vadd(f,f),c(f.x,f.y,f.z)},a.Box.prototype.calculateWorldAABB=function(a,b,c,d){c.set(Infinity,Infinity,Infinity),d.set(-Infinity,-Infinity,-Infinity),this.forEachWorldCorner(a,b,function(a,b,e){a>d.x&&(d.x=a),b>d.y&&(d.y=b),e>d.z&&(d.z=e),a<c.x&&(c.x=a),b<c.y&&(c.y=b),e<c.z&&(c.z=e)})},a.Plane=function(){a.Shape.call(this),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};var k=new a.Vec3(0,0,1);a.Plane.prototype.calculateWorldAABB=function(a,b,c,d){b.vmult(k,k),c.set(Infinity,Infinity,Infinity);var e=["x","y","z"];for(var f=0;f<e.length;f++){var g=e[f];k[g]==1&&(d[g]=a[g]),k[g]==-1&&(c[g]=a[g])}},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};var l=new a.Vec3,m=new a.Vec3,o=new a.Vec3,p=new a.Vec3;a.Compound.prototype.calculateWorldAABB=function(a,b,c,d){var e=this.childShapes.length;c.set(Infinity,Infinity,Infinity),d.set(-Infinity,-Infinity,-Infinity);for(var f=0;f<e;f++)this.childOffsets[f].copy(o),b.vmult(o,o),a.vadd(o,o),this.childShapes[f].calculateWorldAABB(o,this.childOrientations[f],m,l),m.x<c.x&&(c.x=m.x),m.y<c.y&&(c.y=m.y),m.z<c.z&&(c.z=m.z),l.x>d.x&&(d.x=l.x),l.y>d.y&&(d.y=l.y),l.z>d.z&&(d.z=l.z)},a.ConvexPolyhedron=function(b,c,d){function r(a,b,c,d,e){var f=a.vertices.length,g=null,h=null,i=a.vertices;for(var j=0;j<f;j++){i[j].copy(q),d.vmult(q,q),q.vadd(c,q);var k=q.dot(b);if(g===null||k>g)g=k;if(h===null||k<h)h=k}if(h>g){var l=h;h=g,g=l}e[0]=g,e[1]=h}function H(b,c){var d=e.vertices[b[0]],f=e.vertices[b[1]],g=e.vertices[b[2]],h=new a.Vec3;M(d,f,g,h);var i=h.dot(d);return h.dot(c)>=i}function I(a,b){var c=e.faces[a],d=e.vertices[c[0]],f=e.vertices[c[1]],g=e.vertices[c[2]];return M(d,f,g,b)}function J(a,b){var c=e.faces[a],d=e.faceNormals[a],f=e.vertices[c[0]],g=-d.dot(f);return g}function M(a,b,c,d){b.vsub(a,L),c.vsub(b,K),K.cross(L,d),d.isZero()||d.normalize()}function N(a){var b=e.faces[a],c="";for(var d=0;d<b.length;d++)c+=" ("+e.vertices[b[d]]+")";return c}function O(a,b){return a[0]===b[1]&&a[1]===b[0]}function P(){return(Math.random()-.5)*2*1e-6}var e=this;a.Shape.call(this),this.type=a.Shape.types.CONVEXPOLYHEDRON,this.vertices=b||[],this.faces=c||[],this.faceNormals=d||[],this.uniqueEdges=[];var f=this.vertices.length;for(var g=0;g<f;g++){var h=this.vertices[g];if(!(h instanceof a.Vec3))throw"Argument 1 must be instance of CANNON.Vec3";this.vertices.push(h)}for(var i=0;i<this.faces.length;i++){var j=this.faces[i].length,k=j;for(var l=0;l<k;l++){var m=(l+1)\%j,n=new a.Vec3;this.vertices[this.faces[i][l]].vsub(this.vertices[this.faces[i][m]],n),n.normalize();var o=!1;for(var h=0;h<this.uniqueEdges.length;h++)if(this.uniqueEdges[h].almostEquals(n)||this.uniqueEdges[h].almostEquals(n)){o=!0;break}o||this.uniqueEdges.push(n);if(n)n.face1=i;else{var p;p.m_face0=i,edges.insert(vp,p)}}}var q=new a.Vec3;this.testSepAxis=function(a,b,c,d,e,f){var g=[],h=[],i=this;r(i,a,c,d,g),r(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,o=l-k;return depth=n<o?n:o,depth};var s=new a.Vec3,t=new a.Vec3,u=new a.Vec3,v=new a.Vec3,w=new a.Vec3,x=new a.Vec3;this.findSeparatingAxis=function(a,b,c,d,e,f){var g=Infinity,h=this,i=0,j=h.faces.length;for(var k=0;k<j;k++){h.faceNormals[k].copy(s),c.vmult(s,s);var l=h.testSepAxis(s,a,b,c,d,e);if(l===!1)return!1;l<g&&(g=l,s.copy(f))}var m=a.faces.length;for(var k=0;k<m;k++){a.faceNormals[k].copy(t),e.vmult(t,t),i++;var l=h.testSepAxis(t,a,b,c,d,e);if(l===!1)return!1;l<g&&(g=l,t.copy(f))}var n,o,p,q,r=0;for(var y=0;y<h.uniqueEdges.length;y++){h.uniqueEdges[y].copy(v),c.vmult(v,v);for(var z=0;z<a.uniqueEdges.length;z++){a.uniqueEdges[z].copy(w),e.vmult(w,w),v.cross(w,x),r++;if(!x.almostZero()){x.normalize();var A=h.testSepAxis(x,a,b,c,d,e);if(A===!1)return!1;A<g&&(g=A,x.copy(f))}}}return d.vsub(b,u),u.dot(f)>0&&f.negate(f),!0};var y=new a.Vec3;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;for(var o=0;o<d.faces.length;o++){d.faceNormals[o].copy(y),f.vmult(y,y),e.vadd(y,y);var p=y.dot(g);p>n&&(n=p,m=o)}var q=[];polyB=d.faces[m];var r=polyB.length;for(var s=0;s<r;s++){var t=d.vertices[polyB[s]],u=new a.Vec3;t.copy(u),f.vmult(u,u),e.vadd(u,u),q.push(u)}m>=0&&this.clipFaceAgainstHull(g,b,c,q,h,i,j)};var z=new a.Vec3,A=new a.Vec3,B=new a.Vec3,C=new a.Vec3,D=new a.Vec3,E=new a.Vec3,F=new a.Vec3,G=new a.Vec3;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;for(var o=0;o<i.faces.length;o++){i.faceNormals[o].copy(z),d.vmult(z,z),c.vadd(z,z);var p=z.dot(b);p<n&&(n=p,m=o)}if(m<0){console.log("--- did not find any closest face... ---");return}var q=i.faces[m];q.connectedFaces=[];for(var r=0;r<i.faces.length;r++)for(var s=0;s<i.faces[r].length;s++)q.indexOf(i.faces[r][s])!==-1&&r!==m&&q.connectedFaces.indexOf(r)===-1&&q.connectedFaces.push(r);var t=k.length,u=q.length,v=[];for(var w=0;w<u;w++){var x=i.vertices[q[w]],y=i.vertices[q[(w+1)\%u]];x.vsub(y,A),A.copy(B),d.vmult(B,B),c.vadd(B,B),this.faceNormals[m].copy(C),d.vmult(C,C),c.vadd(C,C),B.cross(C,D),D.negate(D),x.copy(E),d.vmult(E,E),c.vadd(E,E);var H=-E.dot(D),I,K=q.connectedFaces[w];this.faceNormals[K].copy(F);var L=J(K);F.copy(G),d.vmult(G,G);var I=L-G.dot(c);this.clipFaceAgainstPlane(k,l,G,I);while(k.length)k.shift();while(l.length)k.push(l.shift())}this.faceNormals[m].copy(F);var L=J(m);F.copy(G),d.vmult(G,G);var I=L-G.dot(c);for(var r=0;r<k.length;r++){var M=G.dot(k[r])+I;M<=f&&(console.log("clamped: depth="+M+" to minDist="+(f+"")),M=f);if(M<=g){var N=k[r];if(M<=0){var O={point:N,normal:G,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,K=new a.Vec3,L=new a.Vec3;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;var q=new a.Vec3;a.ConvexPolyhedron.prototype.calculateWorldAABB=function(a,b,c,d){var e=this.vertices.length,f=this.vertices,g,h,i,j,k,l;for(var m=0;m<e;m++){f[m].copy(q),b.vmult(q,q),a.vadd(q,q);var n=q;if(n.x<g||g==undefined)g=n.x;else if(n.x>j||j==undefined)j=n.x;if(n.y<h||h==undefined)h=n.y;else if(n.y>k||k==undefined)k=n.y;if(n.z<i||i==undefined)i=n.z;else if(n.z>l||l==undefined)l=n.z}c.set(g,h,i),d.set(j,k,l)},a.Cylinder=function(b,c,d,e){var f=e,g=[],h=[],i=[],j=[],k=[],l=Math.cos,m=Math.sin;g.push(new a.Vec3(c*l(0),c*m(0),-d*.5)),j.push(0),g.push(new a.Vec3(b*l(0),b*m(0),d*.5)),k.push(1);for(var n=0;n<f;n++){var o=2*Math.PI/f*(n+1),p=2*Math.PI/f*(n+.5);n<f-1?(g.push(new a.Vec3(c*l(o),c*m(o),-d*.5)),j.push(2*(n+1)),g.push(new a.Vec3(b*l(o),b*m(o),d*.5)),k.push(2*(n+1)+1),h.push(new a.Vec3(l(p),m(p),0)),i.push([2*n,2*n+1,2*(n+1),2*(n+1)+1])):(i.push([2*n,2*n+1,0,1]),h.push(new a.Vec3(l(p),m(p),0)))}i.push(k),h.push(new a.Vec3(0,0,1)),i.push(j),h.push(new a.Vec3(0,0,-1)),this.type=a.Shape.types.CONVEXPOLYHEDRON,a.ConvexPolyhedron.call(this,g,i,h)},a.Cylinder.prototype=new a.ConvexPolyhedron,a.Solver=function(){this.iterations=10,this.h=1/60,this.k=1e3,this.d=4,this.a=0,this.b=0,this.eps=0,this.setSpookParams(this.k,this.d),this.reset(0),this.debug=!1,this.debug&&console.log("a:",this.a,"b",this.b,"eps",this.eps,"k",this.k,"d",this.d)},a.Solver.prototype.setSpookParams=function(a,b){var c=this.h;this.k=a,this.d=b,this.a=4/(c*(1+4*b)),this.b=4*b/(1+4*b),this.eps=4/(c*c*a*(1+4*b))},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 l=",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[l="+A+"]:",E),console.log("Gq[l="+A+"]:",F),console.log("GW[l="+A+"]:",G),console.log("GMinvf[l="+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.EventTarget=function(){var a={};this.addEventListener=function(b,c){a[b]==undefined&&(a[b]=[]),a[b].indexOf(c)===-1&&a[b].push(c)},this.dispatchEvent=function(b){for(var c in a[b.type])a[b.type][c](b)},this.removeEventListener=function(b,c){var d=a[b].indexOf(c);d!==-1&&a[b].splice(d,1)}},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(){a.EventTarget.apply(this),this.allowSleep=!1,this.enableImpulses=!1,this.quatNormalizeSkip=2,this.quatNormalizeFast=!0,this.time=0,this.stepnumber=0,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.solver=new a.Solver,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},this.doProfiling=!1,this.profile={broadphase:0,integrate:0,nearphase:0,solve:0,makeContactConstraints:0}},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.addCollisionImpulse=function(b,c,d){var e=b.ri,f=b.rj,g=b.ni,h=b.bi;bj=b.bj;var i=h.velocity,j=bj.velocity,k=j.vsub(i),l=e.crossmat(),m=f.crossmat(),n=h.inertia&&h.inertia.x>0?1/h.inertia.x:0,o=new a.Mat3([n,0,0,0,n,0,0,0,n]);n=bj.inertia&&bj.inertia.x>0?1/bj.inertia.x:0;var p=new a.Mat3([n,0,0,0,n,0,0,0,n]),q=h.invMass+bj.invMass,r=new a.Mat3([q,0,0,0,q,0,0,0,q]),s=l.mmult(o.mmult(l)),t=m.mmult(p.mmult(m));for(var u=0;u<9;u++)r.elements[u]-=s.elements[u]+t.elements[u];var v=g.mult(-c*k.dot(g)),w=r.solve(v.vsub(k));if(d>0){var x=g.mult(w.dot(g)),y=w.vsub(x);if(y.norm2()>x.mult(d).norm2()){var z=k.vsub(g.mult(k.dot(g))),A=z.mult(1/(z.norm()+1e-4)),B=-(1+c)*k.dot(g)/g.dot(r.vmult(g.vsub(A.mult(d))));w=g.mult(B).vsub(A.mult(d*B))}}var C=h.invMass,D=bj.invMass;C&&(i.x=i.x-w.x*C,i.y=i.y-w.y*C,i.z=i.z-w.z*C),D&&(j.x=j.x+w.x*D,j.y=j.y+w.y*D,j.z=j.z+w.z*D);if(h.inertia||bj.inertia){var E=h.angularVelocity,F=bj.angularVelocity;if(h.inertia){var G=e.cross(w),H=G.mult(h.inertia.x?1/h.inertia.x:0);E.vsub(H,E)}bj.inertia&&(G=f.cross(w),H=G.mult(bj.inertia.x?1/bj.inertia.x:0),F.vadd(H,F))}},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?cm[e+c*b]=0:cm[c+e*b]=0}},a.World.prototype.collisionMatrixGet=function(a,b,c){var d=this.bodies.length;typeof c=="undefined"&&(c=!0);if(c&&a<b||!c&&a>b){var e=b;b=a,a=e}return this.collision_matrix[a+b*d]},a.World.prototype.collisionMatrixSet=function(a,b,c,d){var e=this.bodies.length;typeof d=="undefined"&&(d=!0);if(d&&a<b||!d&&a>b){var f=b;b=a,a=f}this.collision_matrix[a+b*e]=c},a.World.prototype.collisionMatrixTick=function(){var a=this.bodies.length;for(var b=0;b<a;b++)for(var c=0;c<b;c++){var d=this.collisionMatrixGet(b,c,!0);this.collisionMatrixSet(b,c,d,!1),this.collisionMatrixSet(b,c,0,!0)}},a.World.prototype.add=function(b){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 instanceof a.RigidBody&&(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.removeConstraint=function(a){var b=this.constraints.indexOf(a);b!=-1&&this.constraints.splice(b,1)},a.World.prototype.id=function(){return this.nextId++},a.World.prototype.remove=function(a){a.world=null;var b=this.numObjects(),c=this.bodies;for(var d in c)c[d].id==a.id&&c.splice(d,1);this.collision_matrix=new Int16Array((b-1)*(b-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._now=function(){return window.performance.webkitNow?window.performance.webkitNow():Date.now()},a.World.prototype.step=function(b){var c=this,d=this,e=this.numObjects(),f=this.bodies,g=this.solver,h=this.gravity,i=this.doProfiling,j=this.profile,k=a.Body.DYNAMIC,l=this._now,m,n=this.collision_matrix;i&&(m=l()),b==undefined&&(this.last_dt?b=this.last_dt:b=this.default_dt);var o=h.x,p=h.y,q=h.z;for(var r=0;r<e;r++){var s=f[r];if(s.motionstate&k){var t=s.force,u=s.mass;t.x+=u*o,t.y+=u*p,t.z+=u*q}}i&&(m=l());var v=this.broadphase.collisionPairs(this),w=v[0],x=v[1];i&&(j.broadphase=l()-m),this.collisionMatrixTick(),g.reset(e),i&&(m=l());var y=this.contacts;this.contacts=[],this.contactgen.getContacts(w,x,this,this.contacts,y),i&&(j.nearphase=l()-m),i&&(m=l());var z=this.temp,A=this.contacts,B=A.length;for(var C=0;C<B;C++){var D=A[C],s=D.bi,E=D.bj,r=f.indexOf(s),F=f.indexOf(E),G=this.collisionMatrixGet(r,F,!1),H=.3,I=.2,n=this.getContactMaterial(s.material,E.material);n&&(H=n.friction,I=n.restitution);var J=z.gvec;J.set(E.position.x+D.rj.x-s.position.x-D.ri.x,E.position.y+D.rj.y-s.position.y-D.ri.y,E.position.z+D.rj.z-s.position.z-D.ri.z);var K=J.dot(D.ni);if(K<0){this.collisionMatrixSet(r,F,1,!0),this.collisionMatrixGet(r,F,true)!=this.collisionMatrixGet(r,F,false)&&(s.dispatchEvent({type:"collide","with":E}),E.dispatchEvent({type:"collide","with":s}),s.wakeUp(),E.wakeUp(),this.enableImpulses&&this.addCollisionImpulse(D,I,H));var L=s.velocity,M=s.angularVelocity,N=E.velocity,O=E.angularVelocity,P=D.ni,Q=[z.t1,z.t2];P.tangents(Q[0],Q[1]);var R;M?R=L.vadd(M.cross(D.ri)):R=L.copy();var S;O?S=N.vadd(O.cross(D.rj)):S=N.copy();var T=S.vsub(R),U;O&&M?U=O.cross(D.rj).vsub(M.cross(D.ri)):M?U=M.cross(D.ri).negate():O&&(U=O.cross(D.rj));var V=N.vsub(L),W;O&&M?W=D.rj.cross(O).vsub(D.ri.cross(M)):M?W=D.ri.cross(M).negate():O&&(W=D.rj.cross(O)),V.vsub(W,V);var X=s.invMass,Y=E.invMass,Z=s.invInertia?s.invInertia.x:0,,_,Y,Y,Y,ab,bb,cb],[-ib.x,-ib.y,-ib.z,0,0,0,ib.x,ib.y,ib.z,0,0,0],[-fb.x,-fb.y,-fb.z,0,0,0,fb.x,fb.y,fb.z,0,0,0],[s.force.x,s.force.y,s.force.z,jb,kb,lb,-E.force.x,-E.force.y,-E.force.z,-mb,-kb,-lb],0,"inf",r,F);if(H>0){var K=h.norm();for(var pb=0;pb<Q.length;pb++){var qb=Q[pb],rb=D.ri.cross(qb),sb=D.rj.cross(qb),tb=qb.mult(T.dot(qb)),ub=rb.unit().mult(T.dot(rb.unit())),vb=sb.unit().mult(-T.dot(sb.unit()));g.addConstraint([-qb.x,-qb.y,-qb.z,-rb.x,-rb.y,-rb.z,qb.x,qb.y,qb.z,sb.x,sb.y,sb.z],[X,X,X,Z,b=s.postStep;b.call(s)}if(c.allowSleep)for(var r=0;r<e;r++)f[r].sleepTick()},a.ContactPoint=function(b,c,d,e,f){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 g(d,e,h,i,j,k,l,m,n){function q(c,d){if(b.length){var e=b.pop();return e.bi=c,e.bj=d,e}return new a.ContactPoint(c,d)}function r(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 s(a,b,c,d,e,f,h,i,j){for(var k=0;k<c.childShapes.length;k++){var l=[];g(l,b,c.childShapes[k],d,e.vadd(h.vmult(c.childOffsets[k])),f,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 o=!1;if(e&&h&&e.type>h.type){var p;p=h,h=e,e=p,p=j,j=i,i=p,p=l,l=k,k=p,p=n,n=m,m=p,o=!0}if(e&&h){if(e.type==a.Shape.types.SPHERE){if(h.type==a.Shape.types.SPHERE){var t=q(m,n);j.vsub(i,t.ni),t.ni.normalize(),t.ni.copy(t.ri),t.ni.copy(t.rj),t.ri.mult(e.radius,t.ri),t.rj.mult(-h.radius,t.rj),d.push(t)}else if(h.type==a.Shape.types.PLANE){var t=q(m,n);t.ni.set(0,0,1),l.vmult(t.ni,t.ni),t.ni.negate(t.ni),t.ni.normalize(),t.ni.mult(e.radius,t.ri);var u=i.vsub(j),v=t.ni.mult(t.ni.dot(u));t.rj=u.vsub(v),v.norm()<=e.radius&&d.push(t)}else if(h.type==a.Shape.types.BOX){var w=i.vsub(j),x=h.getSideNormals(!0,l),y=e.radius,z=[],A=!1;for(var B=0;B<x.length&&!A;B++){var C=x[B].copy(),D=C.norm();C.normalize();var E=w.dot(C);if(E<D+y&&E>0){var F=x[(B+1)%3].copy(),G=x[(B+2)%3].copy(),H=F.norm(),I=G.norm();F.normalize(),G.normalize();var J=w.dot(F),K=w.dot(G);if(J<H&&J>-H&&K<I&&K>-I){A=!0;var t=q(m,n);C.mult(-y,t.ri),C.copy(t.ni),t.ni.negate(t.ni),C.mult(D).vadd(F.mult(J)).vadd(G.mult(K),t.rj),d.push(t)}}}var L=c.get();for(var M=0;M<2&&!A;M++)for(var N=0;N<2&&!A;N++)for(var O=0;O<2&&!A;O++){L.set(0,0,0),M?L.vadd(x[0],L):L.vsub(x[0],L),N?L.vadd(x[1],L):L.vsub(x[1],L),O?L.vadd(x[2],L):L.vsub(x[2],L);var P=j.vadd(L).vsub(i);if(P.norm()<y){A=!0;var t=q(m,n);P.copy(t.ri),t.ri.normalize(),t.ri.copy(t.ni),t.ri.mult(y,t.ri),L.copy(t.rj),d.push(t)}}c.release(L),L=null;var Q=c.get(),R=c.get(),t=c.get(),S=c.get(),T=c.get();for(var M=0;M<x.length&&!A;M++)for(var N=0;N<x.length&&!A;N++)if(M%3!=N%3){x[N].cross(x[M],Q),Q.normalize(),x[M].vadd(x[N],R),i.copy(t),t.vsub(R,t),t.vsub(j,t);var U=t.dot(Q);Q.mult(U,S);var O=0;while(O==M%3||O==N%3)O++;i.copy(T),T.vsub(S,T),T.vsub(R,T),T.vsub(j,T);var V=Math.abs(U),W=T.norm();if(V<x[O].norm()&&W<y){A=!0;var X=q(m,n);R.vadd(S,X.rj),X.rj.copy(X.rj),T.negate(X.ni),X.ni.normalize(),X.rj.copy(X.ri),X.ri.vadd(j,X.ri),X.ri.vsub(i,X.ri),X.ri.normalize(),X.ri.mult(y,X.ri),d.push(X)}}c.release(Q,R,t,S,T)}else if(h.type==a.Shape.types.COMPOUND)s(d,e,h,i,j,k,l,m,n);else if(h.type==a.Shape.types.CONVEXPOLYHEDRON)throw new Error("sphere/convexpolyhedron contacts not implemented yet.")}else if(e.type==a.Shape.types.PLANE){if(h.type==a.Shape.types.PLANE)throw"Plane-plane collision... wait, you did WHAT?";if(h.type==a.Shape.types.BOX){var Y=new a.Vec3(0,0,1);k.vmult(Y,Y);var Z=0,.length&&Z<=4;B++){var t=q(m,n),_=[B].copy(t.rj);var ab=_.vsub(i),bb=Y.dot(ab);if(bb<=0){Z++;var cb=Y.mult(bb);ab.vsub(cb,t.ri),Y.copy(t.ni),d.push(t)}}}else if(h.type==a.Shape.types.COMPOUND)s(d,e,h,i,j,k,l,m,n);else if(h.type==a.Shape.types.CONVEXPOLYHEDRON){var db=c.get(),eb=c.get();db.set(1,0,0),eb.set(0,1,0),k.vmult(db,db),k.vmult(eb,eb),db.mult(1e5,db),eb.mult(1e5,eb);var Y=c.get();Y.set(0,0,1),k.vmult(Y,Y),f.vertices[0].set(-db.x-eb.x-Y.x,-db.y-eb.y-Y.y,-db.z-eb.z-Y.z),f.vertices[1].set(db.x-eb.x+0*Y.x,db.y-eb.y+0*Y.y,db.z-eb.z+0*Y.z),f.vertices[2].set(db.x+eb.x-Y.x,db.y+eb.y-Y.y,db.z+eb.z-Y.z),f.vertices[3].set(-db.x+eb.x-Y.x,-db.y+eb.y-Y.y,-db.z+eb.z-Y.z),f.vertices[4].set(-db.x-eb.x+0*Y.x,-db.y-eb.y+0*Y.y,-db.z-eb.z+0*Y.z),f.vertices[5].set(+db.x-eb.x+0*Y.x,db.y-eb.y+0*Y.y,db.z-eb.z+0*Y.z),f.vertices[6].set(+db.x+eb.x+0*Y.x,+db.y+eb.y+0*Y.y,db.z+eb.z+0*Y.z),f.vertices[7].set(-db.x+eb.x+0*Y.x,-db.y+eb.y+0*Y.y,-db.z+eb.z+0*Y.z),db.normalize(),eb.normalize(),f.faceNormals[0].set(-Y.x,-Y.y,-Y.z),f.faceNormals[1].set(Y.x,Y.y,Y.z),f.faceNormals[2].set(-eb.x,-eb.y,-eb.z),f.faceNormals[3].set(eb.x,eb.y,eb.z),f.faceNormals[4].set(-db.x,-db.y,-db.z),f.faceNormals[5].set(db.x,db.y,db.z);var fb=c.get();Y.negate(fb);var gb=c.get();if(h.testSepAxis(fb,f,j,l,i,k)!==!1){var X=[];f.clipAgainstHull(i,k,h,j,l,fb,-100,100,X);for(var M=0;M<X.length;M++){var t=q(m,n);fb.negate(t.ni),X[M].normal.negate(gb),gb.mult(X[M].depth,gb),t.ri.set(X[M].point.x+gb.x,X[M].point.y+gb.y,X[M].point.z+gb.z),t.rj.set(X[M].point.x,X[M].point.y,X[M].point.z),t.rj.vsub(j,t.rj),t.ri.vsub(i,t.ri),d.push(t)}}c.release(gb,db,eb,fb,Y)}}else if(e.type==a.Shape.types.BOX)h.type==a.Shape.types.BOX?g(d,e.convexPolyhedronRepresentation,h.convexPolyhedronRepresentation,i,j,k,l,m,n):h.type==a.Shape.types.COMPOUND?s(d,e,h,i,j,k,l,m,n):h.type==a.Shape.types.CONVEXPOLYHEDRON&&g(d,e.convexPolyhedronRepresentation,h,i,j,k,l,m,n);else if(e.type==a.Shape.types.COMPOUND)h.type==a.Shape.types.COMPOUND?s(d,e,h,i,j,k,l,m,n):h.type==a.Shape.types.CONVEXPOLYHEDRON&&s(d,h,e,j,i,l,k,n,m);else if(e.type==a.Shape.types.CONVEXPOLYHEDRON&&h.type==a.Shape.types.CONVEXPOLYHEDRON){var fb=new a.Vec3;if(e.findSeparatingAxis(h,i,k,j,l,fb)){var X=[],gb=new a.Vec3;e.clipAgainstHull(i,k,h,j,l,fb,-100,100,X);for(var M=0;M<X.length;M++){var t=q(m,n);fb.negate(t.ni),X[M].normal.negate(gb),gb.mult(X[M].depth,gb),t.ri.set(X[M].point.x+gb.x,X[M].point.y+gb.y,X[M].point.z+gb.z),t.rj.set(X[M].point.x,X[M].point.y,X[M].point.z),t.rj.vsub(j,t.rj),t.ri.vsub(i,t.ri),d.push(t)}}}}else{var hb=e?n:m,ib=e?m:n,jb=ib.shape,kb=jb.type;if(kb==a.Shape.types.PLANE){var lb=new a.Vec3(0,0,1);ib.quaternion.vmult(lb,lb);var mb=new a.Vec3;hb.position.vsub(ib.position,mb);var E=lb.dot(mb);if(E<=0){var t=q(hb,ib);lb.copy(t.ni),t.ni.negate(t.ni),t.ri.set(0,0,0);var nb=new a.Vec3;lb.mult(lb.dot(hb.position),nb),hb.position.vsub(nb,nb),nb.copy(t.rj),d.push(t)}}else if(kb==a.Shape.types.SPHERE){var lb=new a.Vec3(0,0,1);hb.position.vsub(ib.position,lb);var ob=lb.norm2();if(ob<=Math.pow(jb.radius,2)){var t=q(hb,ib);lb.normalize(),lb.copy(t.rj),t.rj.mult(jb.radius,t.rj),lb.copy(t.ni),t.ni.negate(t.ni),t.ri.set(0,0,0),d.push(t)}}}for(var pb=0;o&&pb<d.length;pb++)r(d[pb])}this.contactReduction=!0;var b=[],c=new a.Vec3Pool,d=[new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3],e=[new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3,new a.Vec3],f=new a.ConvexPolyhedron(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]],e);this.reduceContacts=function(a){},this.getContacts=function(a,c,d,e,f){for(var h=0;f&&h<f.length;h++)b.push(f[h]);for(var i=0;i<a.length;i++){var j=a[i],k=c[i];g(e,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.Vec3?null:c);this.equations.push(e)},a.DistanceConstraint.prototype=new a.Constraint,a.DistanceConstraint.prototype.constructor=a.DistanceConstraint,a.DistanceConstraint.prototype.update=function(){var a=this.equations[0],b=this.body_i,c=this.body_j,d=typeof c.mass=="number";d?c.position.vsub(b.position,a.G1):b.position.vsub(c,a.G1),a.G1.normalize(),a.G1.isZero()&&a.G1.set(1,0,0),a.G1.negate(a.G3),a.setDefaultMassProps(),a.setDefaultForce(),a.g1.set((d?c.position.x:c.x)-b.position.x-a.G1.x*this.distance,(d?c.position.y:c.y)-b.position.y-a.G1.y*this.distance,(d?c.position.z:c.z)-b.position.z-a.G1.z*this.distance),a.g1.negate(a.g1),a.g1.negate(a.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&&a.invInertia.copy(this.iM2)),b&&(this.iM3.set(b.invMass,b.invMass,b.invMass),b.invInertia&&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&&a.tau.copy(this.f2)),b&&(b.force.copy(this.f3),b.tau&&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,this.equations.push(new a.Equation(b,d)),this.piWorld=new a.Vec3,this.pjWorld=new a.Vec3,this.ri=new a.Vec3,this.rj=new a.Vec3,this.di=new a.Vec3,this.dj=new a.Vec3,this.temp=new a.Vec3},a.PointToPointConstraint.prototype=new a.Constraint,a.PointToPointConstraint.prototype.constructor=a.PointToPointConstraint,a.PointToPointConstraint.prototype.update=function(){var b=this.equations[0],c=this.body_i,d=this.body_j,e=this.pivot_i,f=this.pivot_j,g=this.temp,h=this.di,i=this.dj,j=this.ri,k=this.rj,l=typeof d.mass=="number",m=this.piWorld,n=this.pjWorld;c.quaternion.vmult(e,m),d.quaternion.vmult(f,n),c.quaternion.vmult(e,j),d.quaternion.vmult(f,k),m.vadd(c.position,m),n.vadd(d.position,n);var o=new a.Vec3,p=new a.Vec3;j.copy(o),k.copy(p),o.normalize(),p.normalize(),m.vsub(n,h),n.vsub(m,i);var q=h.unit();q.negate(b.G1),q.copy(b.G3),j.cross(q,b.G2),k.cross(q,b.G4),b.G2.negate(b.G2),h.copy(b.g1),i.copy(b.g3),b.g1.mult(.5,b.g1),b.g3.mult(.5,b.g3),c.velocity.copy(b.W1),c.angularVelocity.copy(b.W2),d.velocity.copy(b.W3),d.angularVelocity.copy(b.W4),b.setDefaultMassProps(),b.setDefaultForce()},a.DistanceConstraint.prototype.setMaxForce=function(a){this.equations[0].lambdamax=Math.abs(a),this.equations[0].lambdamin=-this.equations[0].lambdamax},typeof module!="undefined"?module.exports=a:this.CANNON=a}).apply(this);
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.