class Go{constructor(){this.matrix={}}get(t,e){let{id:s}=t,{id:i}=e;if(i>s){const n=i;i=s,s=n}return`${s}-${i}`in this.matrix}set(t,e,s){let{id:i}=t,{id:n}=e;if(n>i){const o=n;n=i,i=o}s?this.matrix[`${i}-${n}`]=!0:delete this.matrix[`${i}-${n}`]}reset(){this.matrix={}}setNumObjects(t){}}class et{constructor(t){t===void 0&&(t=[0,0,0,0,0,0,0,0,0]),this.elements=t}identity(){const t=this.elements;t[0]=1,t[1]=0,t[2]=0,t[3]=0,t[4]=1,t[5]=0,t[6]=0,t[7]=0,t[8]=1}setZero(){const t=this.elements;t[0]=0,t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=0,t[6]=0,t[7]=0,t[8]=0}setTrace(t){const e=this.elements;e[0]=t.x,e[4]=t.y,e[8]=t.z}getTrace(t){t===void 0&&(t=new l);const e=this.elements;return t.x=e[0],t.y=e[4],t.z=e[8],t}vmult(t,e){e===void 0&&(e=new l);const s=this.elements,i=t.x,n=t.y,o=t.z;return e.x=s[0]*i+s[1]*n+s[2]*o,e.y=s[3]*i+s[4]*n+s[5]*o,e.z=s[6]*i+s[7]*n+s[8]*o,e}smult(t){for(let e=0;e0){const n=1/i;this.x*=n,this.y*=n,this.z*=n}else this.x=0,this.y=0,this.z=0;return i}unit(t){t===void 0&&(t=new l);const e=this.x,s=this.y,i=this.z;let n=Math.sqrt(e*e+s*s+i*i);return n>0?(n=1/n,t.x=e*n,t.y=s*n,t.z=i*n):(t.x=1,t.y=0,t.z=0),t}length(){const t=this.x,e=this.y,s=this.z;return Math.sqrt(t*t+e*e+s*s)}lengthSquared(){return this.dot(this)}distanceTo(t){const e=this.x,s=this.y,i=this.z,n=t.x,o=t.y,r=t.z;return Math.sqrt((n-e)*(n-e)+(o-s)*(o-s)+(r-i)*(r-i))}distanceSquared(t){const e=this.x,s=this.y,i=this.z,n=t.x,o=t.y,r=t.z;return(n-e)*(n-e)+(o-s)*(o-s)+(r-i)*(r-i)}scale(t,e){e===void 0&&(e=new l);const s=this.x,i=this.y,n=this.z;return e.x=t*s,e.y=t*i,e.z=t*n,e}vmul(t,e){return e===void 0&&(e=new l),e.x=t.x*this.x,e.y=t.y*this.y,e.z=t.z*this.z,e}addScaledVector(t,e,s){return s===void 0&&(s=new l),s.x=this.x+t*e.x,s.y=this.y+t*e.y,s.z=this.z+t*e.z,s}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}isZero(){return this.x===0&&this.y===0&&this.z===0}negate(t){return t===void 0&&(t=new l),t.x=-this.x,t.y=-this.y,t.z=-this.z,t}tangents(t,e){const s=this.length();if(s>0){const i=$e,n=1/s;i.set(this.x*n,this.y*n,this.z*n);const o=Ze;Math.abs(i.x)<.9?(o.set(1,0,0),i.cross(o,t)):(o.set(0,1,0),i.cross(o,t)),i.cross(t,e)}else t.set(1,0,0),e.set(0,1,0)}toString(){return`${this.x},${this.y},${this.z}`}toArray(){return[this.x,this.y,this.z]}copy(t){return this.x=t.x,this.y=t.y,this.z=t.z,this}lerp(t,e,s){const i=this.x,n=this.y,o=this.z;s.x=i+(t.x-i)*e,s.y=n+(t.y-n)*e,s.z=o+(t.z-o)*e}almostEquals(t,e){return e===void 0&&(e=1e-6),!(Math.abs(this.x-t.x)>e||Math.abs(this.y-t.y)>e||Math.abs(this.z-t.z)>e)}almostZero(t){return t===void 0&&(t=1e-6),!(Math.abs(this.x)>t||Math.abs(this.y)>t||Math.abs(this.z)>t)}isAntiparallelTo(t,e){return this.negate(ae),ae.almostEquals(t,e)}clone(){return new l(this.x,this.y,this.z)}}l.ZERO=new l(0,0,0);l.UNIT_X=new l(1,0,0);l.UNIT_Y=new l(0,1,0);l.UNIT_Z=new l(0,0,1);const $e=new l,Ze=new l,ae=new l;class G{constructor(t){t===void 0&&(t={}),this.lowerBound=new l,this.upperBound=new l,t.lowerBound&&this.lowerBound.copy(t.lowerBound),t.upperBound&&this.upperBound.copy(t.upperBound)}setFromPoints(t,e,s,i){const n=this.lowerBound,o=this.upperBound,r=s;n.copy(t[0]),r&&r.vmult(n,n),o.copy(n);for(let c=1;co.x&&(o.x=a.x),a.xo.y&&(o.y=a.y),a.yo.z&&(o.z=a.z),a.z=n.x&&e.y<=i.y&&s.y>=n.y&&e.z<=i.z&&s.z>=n.z}getCorners(t,e,s,i,n,o,r,c){const a=this.lowerBound,h=this.upperBound;t.copy(a),e.set(h.x,a.y,a.z),s.set(h.x,h.y,a.z),i.set(a.x,h.y,h.z),n.set(h.x,a.y,h.z),o.set(a.x,h.y,a.z),r.set(a.x,a.y,h.z),c.copy(h)}toLocalFrame(t,e){const s=he,i=s[0],n=s[1],o=s[2],r=s[3],c=s[4],a=s[5],h=s[6],u=s[7];this.getCorners(i,n,o,r,c,a,h,u);for(let d=0;d!==8;d++){const f=s[d];t.pointToLocal(f,f)}return e.setFromPoints(s)}toWorldFrame(t,e){const s=he,i=s[0],n=s[1],o=s[2],r=s[3],c=s[4],a=s[5],h=s[6],u=s[7];this.getCorners(i,n,o,r,c,a,h,u);for(let d=0;d!==8;d++){const f=s[d];t.pointToWorld(f,f)}return e.setFromPoints(s)}overlapsRay(t){const{direction:e,from:s}=t,i=1/e.x,n=1/e.y,o=1/e.z,r=(this.lowerBound.x-s.x)*i,c=(this.upperBound.x-s.x)*i,a=(this.lowerBound.y-s.y)*n,h=(this.upperBound.y-s.y)*n,u=(this.lowerBound.z-s.z)*o,d=(this.upperBound.z-s.z)*o,f=Math.max(Math.max(Math.min(r,c),Math.min(a,h)),Math.min(u,d)),p=Math.min(Math.min(Math.max(r,c),Math.max(a,h)),Math.max(u,d));return!(p<0||f>p)}}const le=new l,he=[new l,new l,new l,new l,new l,new l,new l,new l];class de{constructor(){this.matrix=[]}get(t,e){let{index:s}=t,{index:i}=e;if(i>s){const n=i;i=s,s=n}return this.matrix[(s*(s+1)>>1)+i-1]}set(t,e,s){let{index:i}=t,{index:n}=e;if(n>i){const o=n;n=i,i=o}this.matrix[(i*(i+1)>>1)+n-1]=s?1:0}reset(){for(let t=0,e=this.matrix.length;t!==e;t++)this.matrix[t]=0}setNumObjects(t){this.matrix.length=t*(t-1)>>1}}class We{addEventListener(t,e){this._listeners===void 0&&(this._listeners={});const s=this._listeners;return s[t]===void 0&&(s[t]=[]),s[t].includes(e)||s[t].push(e),this}hasEventListener(t,e){if(this._listeners===void 0)return!1;const s=this._listeners;return!!(s[t]!==void 0&&s[t].includes(e))}hasAnyEventListener(t){return this._listeners===void 0?!1:this._listeners[t]!==void 0}removeEventListener(t,e){if(this._listeners===void 0)return this;const s=this._listeners;if(s[t]===void 0)return this;const i=s[t].indexOf(e);return i!==-1&&s[t].splice(i,1),this}dispatchEvent(t){if(this._listeners===void 0)return this;const s=this._listeners[t.type];if(s!==void 0){t.target=this;for(let i=0,n=s.length;i.499&&(s=2*Math.atan2(o,a),i=Math.PI/2,n=0),h<-.499&&(s=-2*Math.atan2(o,a),i=-Math.PI/2,n=0),s===void 0){const u=o*o,d=r*r,f=c*c;s=Math.atan2(2*r*a-2*o*c,1-2*d-2*f),i=Math.asin(2*h),n=Math.atan2(2*o*a-2*r*c,1-2*u-2*f)}break;default:throw new Error(`Euler order ${e} not supported yet.`)}t.y=s,t.z=i,t.x=n}setFromEuler(t,e,s,i){i===void 0&&(i="XYZ");const n=Math.cos(t/2),o=Math.cos(e/2),r=Math.cos(s/2),c=Math.sin(t/2),a=Math.sin(e/2),h=Math.sin(s/2);return i==="XYZ"?(this.x=c*o*r+n*a*h,this.y=n*a*r-c*o*h,this.z=n*o*h+c*a*r,this.w=n*o*r-c*a*h):i==="YXZ"?(this.x=c*o*r+n*a*h,this.y=n*a*r-c*o*h,this.z=n*o*h-c*a*r,this.w=n*o*r+c*a*h):i==="ZXY"?(this.x=c*o*r-n*a*h,this.y=n*a*r+c*o*h,this.z=n*o*h+c*a*r,this.w=n*o*r-c*a*h):i==="ZYX"?(this.x=c*o*r-n*a*h,this.y=n*a*r+c*o*h,this.z=n*o*h-c*a*r,this.w=n*o*r+c*a*h):i==="YZX"?(this.x=c*o*r+n*a*h,this.y=n*a*r+c*o*h,this.z=n*o*h-c*a*r,this.w=n*o*r-c*a*h):i==="XZY"&&(this.x=c*o*r-n*a*h,this.y=n*a*r-c*o*h,this.z=n*o*h+c*a*r,this.w=n*o*r+c*a*h),this}clone(){return new D(this.x,this.y,this.z,this.w)}slerp(t,e,s){s===void 0&&(s=new D);const i=this.x,n=this.y,o=this.z,r=this.w;let c=t.x,a=t.y,h=t.z,u=t.w,d,f,p,m,v;return f=i*c+n*a+o*h+r*u,f<0&&(f=-f,c=-c,a=-a,h=-h,u=-u),1-f>1e-6?(d=Math.acos(f),p=Math.sin(d),m=Math.sin((1-e)*d)/p,v=Math.sin(e*d)/p):(m=1-e,v=e),s.x=m*i+v*c,s.y=m*n+v*a,s.z=m*o+v*h,s.w=m*r+v*u,s}integrate(t,e,s,i){i===void 0&&(i=new D);const n=t.x*s.x,o=t.y*s.y,r=t.z*s.z,c=this.x,a=this.y,h=this.z,u=this.w,d=e*.5;return i.x+=d*(n*u+o*h-r*a),i.y+=d*(o*u+r*c-n*h),i.z+=d*(r*u+n*a-o*c),i.w+=d*(-n*c-o*a-r*h),i}}const Ke=new l,Qe=new l,Je={SPHERE:1,PLANE:2,BOX:4,COMPOUND:8,CONVEXPOLYHEDRON:16,HEIGHTFIELD:32,PARTICLE:64,CYLINDER:128,TRIMESH:256};class S{constructor(t){t===void 0&&(t={}),this.id=S.idCounter++,this.type=t.type||0,this.boundingSphereRadius=0,this.collisionResponse=t.collisionResponse?t.collisionResponse:!0,this.collisionFilterGroup=t.collisionFilterGroup!==void 0?t.collisionFilterGroup:1,this.collisionFilterMask=t.collisionFilterMask!==void 0?t.collisionFilterMask:-1,this.material=t.material?t.material:null,this.body=null}updateBoundingSphereRadius(){throw`computeBoundingSphereRadius() not implemented for shape type ${this.type}`}volume(){throw`volume() not implemented for shape type ${this.type}`}calculateLocalInertia(t,e){throw`calculateLocalInertia() not implemented for shape type ${this.type}`}calculateWorldAABB(t,e,s,i){throw`calculateWorldAABB() not implemented for shape type ${this.type}`}}S.idCounter=0;S.types=Je;class j{constructor(t){t===void 0&&(t={}),this.position=new l,this.quaternion=new D,t.position&&this.position.copy(t.position),t.quaternion&&this.quaternion.copy(t.quaternion)}pointToLocal(t,e){return j.pointToLocalFrame(this.position,this.quaternion,t,e)}pointToWorld(t,e){return j.pointToWorldFrame(this.position,this.quaternion,t,e)}vectorToWorldFrame(t,e){return e===void 0&&(e=new l),this.quaternion.vmult(t,e),e}static pointToLocalFrame(t,e,s,i){return i===void 0&&(i=new l),s.vsub(t,i),e.conjugate(ue),ue.vmult(i,i),i}static pointToWorldFrame(t,e,s,i){return i===void 0&&(i=new l),e.vmult(s,i),i.vadd(t,i),i}static vectorToWorldFrame(t,e,s){return s===void 0&&(s=new l),t.vmult(e,s),s}static vectorToLocalFrame(t,e,s,i){return i===void 0&&(i=new l),e.w*=-1,e.vmult(s,i),e.w*=-1,i}}const ue=new D;class vt extends S{constructor(t){t===void 0&&(t={});const{vertices:e=[],faces:s=[],normals:i=[],axes:n,boundingSphereRadius:o}=t;super({type:S.types.CONVEXPOLYHEDRON}),this.vertices=e,this.faces=s,this.faceNormals=i,this.faceNormals.length===0&&this.computeNormals(),o?this.boundingSphereRadius=o:this.updateBoundingSphereRadius(),this.worldVertices=[],this.worldVerticesNeedsUpdate=!0,this.worldFaceNormals=[],this.worldFaceNormalsNeedsUpdate=!0,this.uniqueAxes=n?n.slice():null,this.uniqueEdges=[],this.computeEdges()}computeEdges(){const t=this.faces,e=this.vertices,s=this.uniqueEdges;s.length=0;const i=new l;for(let n=0;n!==t.length;n++){const o=t[n],r=o.length;for(let c=0;c!==r;c++){const a=(c+1)%r;e[o[c]].vsub(e[o[a]],i),i.normalize();let h=!1;for(let u=0;u!==s.length;u++)if(s[u].almostEquals(i)||s[u].almostEquals(i)){h=!0;break}h||s.push(i.clone())}}}computeNormals(){this.faceNormals.length=this.faces.length;for(let t=0;td&&(d=m,u=p)}const f=[];for(let p=0;p=0&&this.clipFaceAgainstHull(o,t,e,f,r,c,a)}findSeparatingAxis(t,e,s,i,n,o,r,c){const a=new l,h=new l,u=new l,d=new l,f=new l,p=new l;let m=Number.MAX_VALUE;const v=this;if(v.uniqueAxes)for(let w=0;w!==v.uniqueAxes.length;w++){s.vmult(v.uniqueAxes[w],a);const y=v.testSepAxis(a,t,e,s,i,n);if(y===!1)return!1;y0&&o.negate(o),!0}testSepAxis(t,e,s,i,n,o){const r=this;vt.project(r,t,s,i,$t),vt.project(e,t,n,o,Zt);const c=$t[0],a=$t[1],h=Zt[0],u=Zt[1];if(ce.x&&(e.x=n.x),n.ye.y&&(e.y=n.y),n.ze.z&&(e.z=n.z)}}computeWorldFaceNormals(t){const e=this.faceNormals.length;for(;this.worldFaceNormals.lengtht&&(t=i)}this.boundingSphereRadius=Math.sqrt(t)}calculateWorldAABB(t,e,s,i){const n=this.vertices;let o,r,c,a,h,u,d=new l;for(let f=0;fa)&&(a=p.x),(r===void 0||p.yh)&&(h=p.y),(c===void 0||p.zu)&&(u=p.z)}s.set(o,r,c),i.set(a,h,u)}volume(){return 4*Math.PI*this.boundingSphereRadius/3}getAveragePointLocal(t){t===void 0&&(t=new l);const e=this.vertices;for(let s=0;s0||u>0&&f<0)return!1}return n?1:-1}static project(t,e,s,i,n){const o=t.vertices.length,r=es;let c=0,a=0;const h=ss,u=t.vertices;h.setZero(),j.vectorToLocalFrame(s,i,e,r),j.pointToLocalFrame(s,i,h,h);const d=h.dot(r);a=c=u[0].dot(r);for(let f=1;fc&&(c=p),pc){const f=a;a=c,c=f}n[0]=c,n[1]=a}}const $t=[],Zt=[],ts=new l,es=new l,ss=new l;class Dt extends S{constructor(t){super({type:S.types.BOX}),this.halfExtents=t,this.convexPolyhedronRepresentation=null,this.updateConvexPolyhedronRepresentation(),this.updateBoundingSphereRadius()}updateConvexPolyhedronRepresentation(){const t=this.halfExtents.x,e=this.halfExtents.y,s=this.halfExtents.z,i=l,n=[new i(-t,-e,-s),new i(t,-e,-s),new i(t,e,-s),new i(-t,e,-s),new i(-t,-e,s),new i(t,-e,s),new i(t,e,s),new i(-t,e,s)],o=[[3,2,1,0],[4,5,6,7],[5,4,0,1],[2,3,7,6],[0,4,7,3],[1,2,6,5]],r=[new i(0,0,1),new i(0,1,0),new i(1,0,0)],c=new vt({vertices:n,faces:o,axes:r});this.convexPolyhedronRepresentation=c,c.material=this.material}calculateLocalInertia(t,e){return e===void 0&&(e=new l),Dt.calculateInertia(this.halfExtents,t,e),e}static calculateInertia(t,e,s){const i=t;s.x=1/12*e*(2*i.y*2*i.y+2*i.z*2*i.z),s.y=1/12*e*(2*i.x*2*i.x+2*i.z*2*i.z),s.z=1/12*e*(2*i.y*2*i.y+2*i.x*2*i.x)}getSideNormals(t,e){const s=t,i=this.halfExtents;if(s[0].set(i.x,0,0),s[1].set(0,i.y,0),s[2].set(0,0,i.z),s[3].set(-i.x,0,0),s[4].set(0,-i.y,0),s[5].set(0,0,-i.z),e!==void 0)for(let n=0;n!==s.length;n++)e.vmult(s[n],s[n]);return s}volume(){return 8*this.halfExtents.x*this.halfExtents.y*this.halfExtents.z}updateBoundingSphereRadius(){this.boundingSphereRadius=this.halfExtents.length()}forEachWorldCorner(t,e,s){const i=this.halfExtents,n=[[i.x,i.y,i.z],[-i.x,i.y,i.z],[-i.x,-i.y,i.z],[-i.x,-i.y,-i.z],[i.x,-i.y,-i.z],[i.x,i.y,-i.z],[-i.x,i.y,-i.z],[i.x,-i.y,i.z]];for(let o=0;oi.x&&(i.x=a),h>i.y&&(i.y=h),u>i.z&&(i.z=u),a0?1/e:0,this.material=t.material||null,this.linearDamping=typeof t.linearDamping=="number"?t.linearDamping:.01,this.type=e<=0?M.STATIC:M.DYNAMIC,typeof t.type==typeof M.STATIC&&(this.type=t.type),this.allowSleep=typeof t.allowSleep<"u"?t.allowSleep:!0,this.sleepState=M.AWAKE,this.sleepSpeedLimit=typeof t.sleepSpeedLimit<"u"?t.sleepSpeedLimit:.1,this.sleepTimeLimit=typeof t.sleepTimeLimit<"u"?t.sleepTimeLimit:1,this.timeLastSleepy=0,this.wakeUpAfterNarrowphase=!1,this.torque=new l,this.quaternion=new D,this.initQuaternion=new D,this.previousQuaternion=new D,this.interpolatedQuaternion=new D,t.quaternion&&(this.quaternion.copy(t.quaternion),this.initQuaternion.copy(t.quaternion),this.previousQuaternion.copy(t.quaternion),this.interpolatedQuaternion.copy(t.quaternion)),this.angularVelocity=new l,t.angularVelocity&&this.angularVelocity.copy(t.angularVelocity),this.initAngularVelocity=new l,this.shapes=[],this.shapeOffsets=[],this.shapeOrientations=[],this.inertia=new l,this.invInertia=new l,this.invInertiaWorld=new et,this.invMassSolve=0,this.invInertiaSolve=new l,this.invInertiaWorldSolve=new et,this.fixedRotation=typeof t.fixedRotation<"u"?t.fixedRotation:!1,this.angularDamping=typeof t.angularDamping<"u"?t.angularDamping:.01,this.linearFactor=new l(1,1,1),t.linearFactor&&this.linearFactor.copy(t.linearFactor),this.angularFactor=new l(1,1,1),t.angularFactor&&this.angularFactor.copy(t.angularFactor),this.aabb=new G,this.aabbNeedsUpdate=!0,this.boundingRadius=0,this.wlambda=new l,this.isTrigger=!!t.isTrigger,t.shape&&this.addShape(t.shape),this.updateMassProperties()}wakeUp(){const t=this.sleepState;this.sleepState=M.AWAKE,this.wakeUpAfterNarrowphase=!1,t===M.SLEEPING&&this.dispatchEvent(M.wakeupEvent)}sleep(){this.sleepState=M.SLEEPING,this.velocity.set(0,0,0),this.angularVelocity.set(0,0,0),this.wakeUpAfterNarrowphase=!1}sleepTick(t){if(this.allowSleep){const e=this.sleepState,s=this.velocity.lengthSquared()+this.angularVelocity.lengthSquared(),i=this.sleepSpeedLimit**2;e===M.AWAKE&&si?this.wakeUp():e===M.SLEEPY&&t-this.timeLastSleepy>this.sleepTimeLimit&&(this.sleep(),this.dispatchEvent(M.sleepEvent))}}updateSolveMassProperties(){this.sleepState===M.SLEEPING||this.type===M.KINEMATIC?(this.invMassSolve=0,this.invInertiaSolve.setZero(),this.invInertiaWorldSolve.setZero()):(this.invMassSolve=this.invMass,this.invInertiaSolve.copy(this.invInertia),this.invInertiaWorldSolve.copy(this.invInertiaWorld))}pointToLocalFrame(t,e){return e===void 0&&(e=new l),t.vsub(this.position,e),this.quaternion.conjugate().vmult(e,e),e}vectorToLocalFrame(t,e){return e===void 0&&(e=new l),this.quaternion.conjugate().vmult(t,e),e}pointToWorldFrame(t,e){return e===void 0&&(e=new l),this.quaternion.vmult(t,e),e.vadd(this.position,e),e}vectorToWorldFrame(t,e){return e===void 0&&(e=new l),this.quaternion.vmult(t,e),e}addShape(t,e,s){const i=new l,n=new D;return e&&i.copy(e),s&&n.copy(s),this.shapes.push(t),this.shapeOffsets.push(i),this.shapeOrientations.push(n),this.updateMassProperties(),this.updateBoundingRadius(),this.aabbNeedsUpdate=!0,t.body=this,this}removeShape(t){const e=this.shapes.indexOf(t);return e===-1?(console.warn("Shape does not belong to the body"),this):(this.shapes.splice(e,1),this.shapeOffsets.splice(e,1),this.shapeOrientations.splice(e,1),this.updateMassProperties(),this.updateBoundingRadius(),this.aabbNeedsUpdate=!0,t.body=null,this)}updateBoundingRadius(){const t=this.shapes,e=this.shapeOffsets,s=t.length;let i=0;for(let n=0;n!==s;n++){const o=t[n];o.updateBoundingSphereRadius();const r=e[n].length(),c=o.boundingSphereRadius;r+c>i&&(i=r+c)}this.boundingRadius=i}updateAABB(){const t=this.shapes,e=this.shapeOffsets,s=this.shapeOrientations,i=t.length,n=is,o=ns,r=this.quaternion,c=this.aabb,a=os;for(let h=0;h!==i;h++){const u=t[h];r.vmult(e[h],n),n.vadd(this.position,n),r.mult(s[h],o),u.calculateWorldAABB(n,o,a.lowerBound,a.upperBound),h===0?c.copy(a):c.extend(a)}this.aabbNeedsUpdate=!1}updateInertiaWorld(t){const e=this.invInertia;if(!(e.x===e.y&&e.y===e.z&&!t)){const s=rs,i=cs;s.setRotationFromQuaternion(this.quaternion),s.transpose(i),s.scale(e,s),s.mmult(i,this.invInertiaWorld)}}applyForce(t,e){if(e===void 0&&(e=new l),this.type!==M.DYNAMIC)return;this.sleepState===M.SLEEPING&&this.wakeUp();const s=ls;e.cross(t,s),this.force.vadd(t,this.force),this.torque.vadd(s,this.torque)}applyLocalForce(t,e){if(e===void 0&&(e=new l),this.type!==M.DYNAMIC)return;const s=hs,i=ds;this.vectorToWorldFrame(t,s),this.vectorToWorldFrame(e,i),this.applyForce(s,i)}applyTorque(t){this.type===M.DYNAMIC&&(this.sleepState===M.SLEEPING&&this.wakeUp(),this.torque.vadd(t,this.torque))}applyImpulse(t,e){if(e===void 0&&(e=new l),this.type!==M.DYNAMIC)return;this.sleepState===M.SLEEPING&&this.wakeUp();const s=e,i=us;i.copy(t),i.scale(this.invMass,i),this.velocity.vadd(i,this.velocity);const n=ps;s.cross(t,n),this.invInertiaWorld.vmult(n,n),this.angularVelocity.vadd(n,this.angularVelocity)}applyLocalImpulse(t,e){if(e===void 0&&(e=new l),this.type!==M.DYNAMIC)return;const s=fs,i=ms;this.vectorToWorldFrame(t,s),this.vectorToWorldFrame(e,i),this.applyImpulse(s,i)}updateMassProperties(){const t=vs;this.invMass=this.mass>0?1/this.mass:0;const e=this.inertia,s=this.fixedRotation;this.updateAABB(),t.set((this.aabb.upperBound.x-this.aabb.lowerBound.x)/2,(this.aabb.upperBound.y-this.aabb.lowerBound.y)/2,(this.aabb.upperBound.z-this.aabb.lowerBound.z)/2),Dt.calculateInertia(t,this.mass,e),this.invInertia.set(e.x>0&&!s?1/e.x:0,e.y>0&&!s?1/e.y:0,e.z>0&&!s?1/e.z:0),this.updateInertiaWorld(!0)}getVelocityAtWorldPoint(t,e){const s=new l;return t.vsub(this.position,s),this.angularVelocity.cross(s,e),this.velocity.vadd(e,e),e}integrate(t,e,s){if(this.previousPosition.copy(this.position),this.previousQuaternion.copy(this.quaternion),!(this.type===M.DYNAMIC||this.type===M.KINEMATIC)||this.sleepState===M.SLEEPING)return;const i=this.velocity,n=this.angularVelocity,o=this.position,r=this.force,c=this.torque,a=this.quaternion,h=this.invMass,u=this.invInertiaWorld,d=this.linearFactor,f=h*t;i.x+=r.x*f*d.x,i.y+=r.y*f*d.y,i.z+=r.z*f*d.z;const p=u.elements,m=this.angularFactor,v=c.x*m.x,w=c.y*m.y,y=c.z*m.z;n.x+=t*(p[0]*v+p[1]*w+p[2]*y),n.y+=t*(p[3]*v+p[4]*w+p[5]*y),n.z+=t*(p[6]*v+p[7]*w+p[8]*y),o.x+=i.x*t,o.y+=i.y*t,o.z+=i.z*t,a.integrate(this.angularVelocity,t,this.angularFactor,a),e&&(s?a.normalizeFast():a.normalize()),this.aabbNeedsUpdate=!0,this.updateInertiaWorld()}}M.idCounter=0;M.COLLIDE_EVENT_NAME="collide";M.DYNAMIC=se.DYNAMIC;M.STATIC=se.STATIC;M.KINEMATIC=se.KINEMATIC;M.AWAKE=ie.AWAKE;M.SLEEPY=ie.SLEEPY;M.SLEEPING=ie.SLEEPING;M.wakeupEvent={type:"wakeup"};M.sleepyEvent={type:"sleepy"};M.sleepEvent={type:"sleep"};const is=new l,ns=new D,os=new G,rs=new et,cs=new et,as=new et,ls=new l,hs=new l,ds=new l,us=new l,ps=new l,fs=new l,ms=new l,vs=new l;class ne{constructor(){this.world=null,this.useBoundingBoxes=!1,this.dirty=!0}collisionPairs(t,e,s){throw new Error("collisionPairs not implemented for this BroadPhase class!")}needBroadphaseCollision(t,e){return!(!(t.collisionFilterGroup&e.collisionFilterMask)||!(e.collisionFilterGroup&t.collisionFilterMask)||(t.type&M.STATIC||t.sleepState===M.SLEEPING)&&(e.type&M.STATIC||e.sleepState===M.SLEEPING))}intersectionTest(t,e,s,i){this.useBoundingBoxes?this.doBoundingBoxBroadphase(t,e,s,i):this.doBoundingSphereBroadphase(t,e,s,i)}doBoundingSphereBroadphase(t,e,s,i){const n=ys;e.position.vsub(t.position,n);const o=(t.boundingRadius+e.boundingRadius)**2;n.lengthSquared()s.lengthSquared()}aabbQuery(t,e,s){return console.warn(".aabbQuery is not implemented in this Broadphase subclass."),[]}}const ys=new l;new l;new D;new l;const ws={keys:[]},xs=[],gs=[];new l;class Do extends ne{constructor(t,e,s,i,n){t===void 0&&(t=new l(100,100,100)),e===void 0&&(e=new l(-100,-100,-100)),s===void 0&&(s=10),i===void 0&&(i=10),n===void 0&&(n=10),super(),this.nx=s,this.ny=i,this.nz=n,this.aabbMin=t,this.aabbMax=e;const o=this.nx*this.ny*this.nz;if(o<=0)throw"GridBroadphase: Each dimension's n must be >0";this.bins=[],this.binLengths=[],this.bins.length=o,this.binLengths.length=o;for(let r=0;r=c&&(U=c-1),$<0?$=0:$>=a&&($=a-1),rt<0?rt=0:rt>=h&&(rt=h-1),ct<0?ct=0:ct>=c&&(ct=c-1),at<0?at=0:at>=a&&(at=a-1),lt<0?lt=0:lt>=h&&(lt=h-1),U*=u,$*=d,rt*=f,ct*=u,at*=d,lt*=f;for(let At=U;At<=ct;At+=u)for(let Bt=$;Bt<=at;Bt+=d)for(let Et=rt;Et<=lt;Et+=f){const ce=At+Bt+Et;F[ce][C[ce]++]=Q}}for(let R=0;R!==i;R++){const W=n[R],O=W.shapes[0];switch(O.type){case V:{const N=O,Z=W.position.x,K=W.position.y,Q=W.position.z,U=N.radius;L(Z-U,K-U,Q-U,Z+U,K+U,Q+U,W);break}case b:{const N=O;N.worldNormalNeedsUpdate&&N.computeWorldNormal(W.quaternion);const Z=N.worldNormal,K=w+I*.5-W.position.x,Q=y+_*.5-W.position.y,U=x+q*.5-W.position.z,$=bs;$.set(K,Q,U);for(let rt=0,ct=0;rt!==c;rt++,ct+=u,$.y=Q,$.x+=I)for(let at=0,lt=0;at!==a;at++,lt+=d,$.z=U,$.y+=_)for(let At=0,Bt=0;At!==h;At++,Bt+=f,$.z+=q)if($.dot(Z)1){const O=F[R];for(let N=0;N!==W;N++){const Z=O[N];for(let K=0;K!==N;K++){const Q=O[K];this.needBroadphaseCollision(Z,Q)&&this.intersectionTest(Z,Q,e,s)}}}}this.makePairsUnique(e,s)}}const bs=new l;new l;class As extends ne{constructor(){super()}collisionPairs(t,e,s){const i=t.bodies,n=i.length;let o,r;for(let c=0;c!==n;c++)for(let a=0;a!==c;a++)o=i[c],r=i[a],this.needBroadphaseCollision(o,r)&&this.intersectionTest(o,r,e,s)}aabbQuery(t,e,s){s===void 0&&(s=[]);for(let i=0;i{}}intersectWorld(t,e){return this.mode=e.mode||Y.ANY,this.result=e.result||new Rt,this.skipBackfaces=!!e.skipBackfaces,this.collisionFilterMask=typeof e.collisionFilterMask<"u"?e.collisionFilterMask:-1,this.collisionFilterGroup=typeof e.collisionFilterGroup<"u"?e.collisionFilterGroup:-1,this.checkCollisionResponse=typeof e.checkCollisionResponse<"u"?e.checkCollisionResponse:!0,e.from&&this.from.copy(e.from),e.to&&this.to.copy(e.to),this.callback=e.callback||(()=>{}),this.hasHit=!1,this.result.reset(),this.updateDirection(),this.getAABB(pe),Kt.length=0,t.broadphase.aabbQuery(t,pe,Kt),this.intersectBodies(Kt),this.hasHit}intersectBody(t,e){e&&(this.result=e,this.updateDirection());const s=this.checkCollisionResponse;if(s&&!t.collisionResponse||!(this.collisionFilterGroup&t.collisionFilterMask)||!(t.collisionFilterGroup&this.collisionFilterMask))return;const i=Bs,n=Es;for(let o=0,r=t.shapes.length;ot.boundingSphereRadius)return;const r=this[t.type];r&&r.call(this,t,e,s,i,t)}_intersectBox(t,e,s,i,n){return this._intersectConvex(t.convexPolyhedronRepresentation,e,s,i,n)}_intersectPlane(t,e,s,i,n){const o=this.from,r=this.to,c=this.direction,a=new l(0,0,1);e.vmult(a,a);const h=new l;o.vsub(s,h);const u=h.dot(a);r.vsub(s,h);const d=h.dot(a);if(u*d>0||o.distanceTo(r)=0&&m<=1&&(o.lerp(r,m,f),f.vsub(s,p),p.normalize(),this.reportIntersection(p,f,n,i,-1)),this.result.shouldStop)return;v>=0&&v<=1&&(o.lerp(r,v,f),f.vsub(s,p),p.normalize(),this.reportIntersection(p,f,n,i,-1))}}_intersectConvex(t,e,s,i,n,o){const r=Is,c=me,a=o&&o.faceList||null,h=t.faces,u=t.vertices,d=t.faceNormals,f=this.direction,p=this.from,m=this.to,v=p.distanceTo(m),w=a?a.length:h.length,y=this.result;for(let x=0;!y.shouldStop&&xv||this.reportIntersection(r,J,n,i,g)}}}}_intersectTrimesh(t,e,s,i,n,o){const r=_s,c=Ws,a=Vs,h=me,u=Ts,d=Rs,f=Ps,p=Ls,m=qs,v=t.indices;t.vertices;const w=this.from,y=this.to,x=this.direction;a.position.copy(s),a.quaternion.copy(e),j.vectorToLocalFrame(s,e,x,u),j.pointToLocalFrame(s,e,w,d),j.pointToLocalFrame(s,e,y,f),f.x*=t.scale.x,f.y*=t.scale.y,f.z*=t.scale.z,d.x*=t.scale.x,d.y*=t.scale.y,d.z*=t.scale.z,f.vsub(d,u),u.normalize();const g=d.distanceSquared(f);t.tree.rayQuery(this,a,c);for(let A=0,E=c.length;!this.result.shouldStop&&A!==E;A++){const I=c[A];t.getNormal(I,r),t.getVertex(v[I*3],tt),tt.vsub(d,h);const _=u.dot(r),q=r.dot(h)/_;if(q<0)continue;u.scale(q,J),J.vadd(d,J),t.getVertex(v[I*3+1],it),t.getVertex(v[I*3+2],nt);const T=J.distanceSquared(d);!(Y.pointInTriangle(J,it,tt,nt)||Y.pointInTriangle(J,tt,it,nt))||T>g||(j.vectorToWorldFrame(e,r,m),j.pointToWorldFrame(s,e,J,p),this.reportIntersection(m,p,n,i,I))}c.length=0}reportIntersection(t,e,s,i,n){const o=this.from,r=this.to,c=o.distanceTo(e),a=this.result;if(!(this.skipBackfaces&&t.dot(this.direction)>0))switch(a.hitFaceIndex=typeof n<"u"?n:-1,this.mode){case Y.ALL:this.hasHit=!0,a.set(o,r,t,e,s,i,c),a.hasHit=!0,this.callback(a);break;case Y.CLOSEST:(c=0&&(u=n*a-o*r)>=0&&h+u=0&&!(t[n].aabb.lowerBound.x<=i.aabb.lowerBound.x);n--)t[n+1]=t[n];t[n+1]=i}return t}static insertionSortY(t){for(let e=1,s=t.length;e=0&&!(t[n].aabb.lowerBound.y<=i.aabb.lowerBound.y);n--)t[n+1]=t[n];t[n+1]=i}return t}static insertionSortZ(t){for(let e=1,s=t.length;e=0&&!(t[n].aabb.lowerBound.z<=i.aabb.lowerBound.z);n--)t[n+1]=t[n];t[n+1]=i}return t}constructor(t){super(),this.axisList=[],this.world=null,this.axisIndex=0;const e=this.axisList;this._addBodyHandler=s=>{e.push(s.body)},this._removeBodyHandler=s=>{const i=e.indexOf(s.body);i!==-1&&e.splice(i,1)},t&&this.setWorld(t)}setWorld(t){this.axisList.length=0;for(let e=0;eu?h>d?this.axisIndex=0:this.axisIndex=2:u>d?this.axisIndex=1:this.axisIndex=2}aabbQuery(t,e,s){s===void 0&&(s=[]),this.dirty&&(this.sortList(),this.dirty=!1);const i=this.axisIndex;let n="x";i===1&&(n="y"),i===2&&(n="z");const o=this.axisList;e.lowerBound[n],e.upperBound[n];for(let r=0;r"u"&&(s=t.position.distanceTo(e.position)),this.distance=s;const n=this.distanceEquation=new Tt(t,e);this.equations.push(n),n.minForce=-i,n.maxForce=i}update(){const t=this.bodyA,e=this.bodyB,s=this.distanceEquation,i=this.distance*.5,n=s.ni;e.position.vsub(t.position,n),n.normalize(),n.scale(i,s.ri),n.scale(-i,s.rj)}}class Xo extends re{constructor(t,e,s){s===void 0&&(s={});const i=typeof s.maxForce<"u"?s.maxForce:1e6,n=new l,o=new l,r=new l;t.position.vadd(e.position,r),r.scale(.5,r),e.pointToLocalFrame(r,o),t.pointToLocalFrame(r,n),super(t,n,e,o,i),this.xA=t.vectorToLocalFrame(l.UNIT_X),this.xB=e.vectorToLocalFrame(l.UNIT_X),this.yA=t.vectorToLocalFrame(l.UNIT_Y),this.yB=e.vectorToLocalFrame(l.UNIT_Y),this.zA=t.vectorToLocalFrame(l.UNIT_Z),this.zB=e.vectorToLocalFrame(l.UNIT_Z);const c=this.rotationalEquation1=new St(t,e,s),a=this.rotationalEquation2=new St(t,e,s),h=this.rotationalEquation3=new St(t,e,s);this.equations.push(c,a,h)}update(){const t=this.bodyA,e=this.bodyB;this.motorEquation;const s=this.rotationalEquation1,i=this.rotationalEquation2,n=this.rotationalEquation3;super.update(),t.vectorToWorldFrame(this.xA,s.axisA),e.vectorToWorldFrame(this.yB,s.axisB),t.vectorToWorldFrame(this.yA,i.axisA),e.vectorToWorldFrame(this.zB,i.axisB),t.vectorToWorldFrame(this.zA,n.axisA),e.vectorToWorldFrame(this.xB,n.axisB)}}const ei=new l,si=new l;class ii extends yt{constructor(t,e,s){s===void 0&&(s=1e6),super(t,e,-s,s),this.axisA=new l,this.axisB=new l,this.targetVelocity=0}computeB(t){this.a;const e=this.b;this.bi,this.bj;const s=this.axisA,i=this.axisB,n=this.jacobianElementA,o=this.jacobianElementB;n.rotational.copy(s),i.negate(o.rotational);const r=this.computeGW()-this.targetVelocity,c=this.computeGiMf();return-r*e-t*c}}class ni extends re{constructor(t,e,s){s===void 0&&(s={});const i=typeof s.maxForce<"u"?s.maxForce:1e6,n=s.pivotA?s.pivotA.clone():new l,o=s.pivotB?s.pivotB.clone():new l;super(t,n,e,o,i),(this.axisA=s.axisA?s.axisA.clone():new l(1,0,0)).normalize(),(this.axisB=s.axisB?s.axisB.clone():new l(1,0,0)).normalize(),this.collideConnected=!!s.collideConnected;const a=this.rotationalEquation1=new St(t,e,s),h=this.rotationalEquation2=new St(t,e,s),u=this.motorEquation=new ii(t,e,i);u.enabled=!1,this.equations.push(a,h,u)}enableMotor(){this.motorEquation.enabled=!0}disableMotor(){this.motorEquation.enabled=!1}setMotorSpeed(t){this.motorEquation.targetVelocity=t}setMotorMaxForce(t){this.motorEquation.maxForce=t,this.motorEquation.minForce=-t}update(){const t=this.bodyA,e=this.bodyB,s=this.motorEquation,i=this.rotationalEquation1,n=this.rotationalEquation2,o=oi,r=ri,c=this.axisA,a=this.axisB;super.update(),t.quaternion.vmult(c,o),e.quaternion.vmult(a,r),o.tangents(i.axisA,n.axisA),i.axisB.copy(r),n.axisB.copy(r),this.motorEquation.enabled&&(t.quaternion.vmult(this.axisA,s.axisA),e.quaternion.vmult(this.axisB,s.axisB))}}const oi=new l,ri=new l;class be extends yt{constructor(t,e,s){super(t,e,-s,s),this.ri=new l,this.rj=new l,this.t=new l}computeB(t){this.a;const e=this.b;this.bi,this.bj;const s=this.ri,i=this.rj,n=ci,o=ai,r=this.t;s.cross(r,n),i.cross(r,o);const c=this.jacobianElementA,a=this.jacobianElementB;r.negate(c.spatial),n.negate(c.rotational),a.spatial.copy(r),a.rotational.copy(o);const h=this.computeGW(),u=this.computeGiMf();return-h*e-t*u}}const ci=new l,ai=new l;class Yt{constructor(t,e,s){s=Ut.defaults(s,{friction:.3,restitution:.3,contactEquationStiffness:1e7,contactEquationRelaxation:3,frictionEquationStiffness:1e7,frictionEquationRelaxation:3}),this.id=Yt.idCounter++,this.materials=[t,e],this.friction=s.friction,this.restitution=s.restitution,this.contactEquationStiffness=s.contactEquationStiffness,this.contactEquationRelaxation=s.contactEquationRelaxation,this.frictionEquationStiffness=s.frictionEquationStiffness,this.frictionEquationRelaxation=s.frictionEquationRelaxation}}Yt.idCounter=0;class Xt{constructor(t){t===void 0&&(t={});let e="";typeof t=="string"&&(e=t,t={}),this.name=e,this.id=Xt.idCounter++,this.friction=typeof t.friction<"u"?t.friction:-1,this.restitution=typeof t.restitution<"u"?t.restitution:-1}}Xt.idCounter=0;class $o{constructor(t,e,s){s===void 0&&(s={}),this.restLength=typeof s.restLength=="number"?s.restLength:1,this.stiffness=s.stiffness||100,this.damping=s.damping||1,this.bodyA=t,this.bodyB=e,this.localAnchorA=new l,this.localAnchorB=new l,s.localAnchorA&&this.localAnchorA.copy(s.localAnchorA),s.localAnchorB&&this.localAnchorB.copy(s.localAnchorB),s.worldAnchorA&&this.setWorldAnchorA(s.worldAnchorA),s.worldAnchorB&&this.setWorldAnchorB(s.worldAnchorB)}setWorldAnchorA(t){this.bodyA.pointToLocalFrame(t,this.localAnchorA)}setWorldAnchorB(t){this.bodyB.pointToLocalFrame(t,this.localAnchorB)}getWorldAnchorA(t){this.bodyA.pointToWorldFrame(this.localAnchorA,t)}getWorldAnchorB(t){this.bodyB.pointToWorldFrame(this.localAnchorB,t)}applyForce(){const t=this.stiffness,e=this.damping,s=this.restLength,i=this.bodyA,n=this.bodyB,o=li,r=hi,c=di,a=ui,h=xi,u=pi,d=fi,f=mi,p=vi,m=yi,v=wi;this.getWorldAnchorA(u),this.getWorldAnchorB(d),u.vsub(i.position,f),d.vsub(n.position,p),d.vsub(u,o);const w=o.length();r.copy(o),r.normalize(),n.velocity.vsub(i.velocity,c),n.angularVelocity.cross(p,h),c.vadd(h,c),i.angularVelocity.cross(f,h),c.vsub(h,c),r.scale(-t*(w-s)-e*c.dot(r),a),i.force.vsub(a,i.force),n.force.vadd(a,n.force),f.cross(a,m),p.cross(a,v),i.torque.vsub(m,i.torque),n.torque.vadd(v,n.torque)}}const li=new l,hi=new l,di=new l,ui=new l,pi=new l,fi=new l,mi=new l,vi=new l,yi=new l,wi=new l,xi=new l;class gi{constructor(t){t===void 0&&(t={}),t=Ut.defaults(t,{chassisConnectionPointLocal:new l,chassisConnectionPointWorld:new l,directionLocal:new l,directionWorld:new l,axleLocal:new l,axleWorld:new l,suspensionRestLength:1,suspensionMaxLength:2,radius:1,suspensionStiffness:100,dampingCompression:10,dampingRelaxation:10,frictionSlip:10.5,forwardAcceleration:1,sideAcceleration:1,steering:0,rotation:0,deltaRotation:0,rollInfluence:.01,maxSuspensionForce:Number.MAX_VALUE,isFrontWheel:!0,clippedInvContactDotSuspension:1,suspensionRelativeVelocity:0,suspensionForce:0,slipInfo:0,skidInfo:0,suspensionLength:0,maxSuspensionTravel:1,useCustomSlidingRotationalSpeed:!1,customSlidingRotationalSpeed:-.1}),this.maxSuspensionTravel=t.maxSuspensionTravel,this.customSlidingRotationalSpeed=t.customSlidingRotationalSpeed,this.useCustomSlidingRotationalSpeed=t.useCustomSlidingRotationalSpeed,this.sliding=!1,this.chassisConnectionPointLocal=t.chassisConnectionPointLocal.clone(),this.chassisConnectionPointWorld=t.chassisConnectionPointWorld.clone(),this.directionLocal=t.directionLocal.clone(),this.directionWorld=t.directionWorld.clone(),this.axleLocal=t.axleLocal.clone(),this.axleWorld=t.axleWorld.clone(),this.suspensionRestLength=t.suspensionRestLength,this.suspensionMaxLength=t.suspensionMaxLength,this.radius=t.radius,this.suspensionStiffness=t.suspensionStiffness,this.dampingCompression=t.dampingCompression,this.dampingRelaxation=t.dampingRelaxation,this.frictionSlip=t.frictionSlip,this.forwardAcceleration=t.forwardAcceleration,this.sideAcceleration=t.sideAcceleration,this.steering=0,this.rotation=0,this.deltaRotation=0,this.rollInfluence=t.rollInfluence,this.maxSuspensionForce=t.maxSuspensionForce,this.engineForce=0,this.brake=0,this.isFrontWheel=t.isFrontWheel,this.clippedInvContactDotSuspension=1,this.suspensionRelativeVelocity=0,this.suspensionForce=0,this.slipInfo=0,this.skidInfo=0,this.suspensionLength=0,this.sideImpulse=0,this.forwardImpulse=0,this.raycastResult=new Rt,this.worldTransform=new j,this.isInContact=!1}updateWheel(t){const e=this.raycastResult;if(this.isInContact){const s=e.hitNormalWorld.dot(e.directionWorld);e.hitPointWorld.vsub(t.position,Be),t.getVelocityAtWorldPoint(Be,Ae);const i=e.hitNormalWorld.dot(Ae);if(s>=-.1)this.suspensionRelativeVelocity=0,this.clippedInvContactDotSuspension=1/.1;else{const n=-1/s;this.suspensionRelativeVelocity=i*n,this.clippedInvContactDotSuspension=n}}else e.suspensionLength=this.suspensionRestLength,this.suspensionRelativeVelocity=0,e.directionWorld.scale(-1,e.hitNormalWorld),this.clippedInvContactDotSuspension=1}}const Ae=new l,Be=new l;class Zo{constructor(t){this.chassisBody=t.chassisBody,this.wheelInfos=[],this.sliding=!1,this.world=null,this.indexRightAxis=typeof t.indexRightAxis<"u"?t.indexRightAxis:2,this.indexForwardAxis=typeof t.indexForwardAxis<"u"?t.indexForwardAxis:0,this.indexUpAxis=typeof t.indexUpAxis<"u"?t.indexUpAxis:1,this.constraints=[],this.preStepCallback=()=>{},this.currentVehicleSpeedKmHour=0,this.numWheelsOnGround=0}addWheel(t){t===void 0&&(t={});const e=new gi(t),s=this.wheelInfos.length;return this.wheelInfos.push(e),s}setSteeringValue(t,e){const s=this.wheelInfos[e];s.steering=t}applyEngineForce(t,e){this.wheelInfos[e].engineForce=t}setBrake(t,e){this.wheelInfos[e].brake=t}addToWorld(t){t.addBody(this.chassisBody);const e=this;this.preStepCallback=()=>{e.updateVehicle(t.dt)},t.addEventListener("preStep",this.preStepCallback),this.world=t}getVehicleAxisWorld(t,e){e.set(t===0?1:0,t===1?1:0,t===2?1:0),this.chassisBody.vectorToWorldFrame(e,e)}updateVehicle(t){const e=this.wheelInfos,s=e.length,i=this.chassisBody;for(let u=0;ud.maxSuspensionForce&&(f=d.maxSuspensionForce),d.raycastResult.hitNormalWorld.scale(f*t,o),d.raycastResult.hitPointWorld.vsub(i.position,r),i.applyImpulse(o,r)}this.updateFriction(t);const c=new l,a=new l,h=new l;for(let u=0;u0?1:-1)*d.customSlidingRotationalSpeed*t),Math.abs(d.brake)>Math.abs(d.engineForce)&&(d.deltaRotation=0),d.rotation+=d.deltaRotation,d.deltaRotation*=.99}}updateSuspension(t){const s=this.chassisBody.mass,i=this.wheelInfos,n=i.length;for(let o=0;of&&(t.suspensionLength=f,t.raycastResult.reset());const p=t.raycastResult.hitNormalWorld.dot(t.directionWorld),m=new l;i.getVelocityAtWorldPoint(t.raycastResult.hitPointWorld,m);const v=t.raycastResult.hitNormalWorld.dot(m);if(p>=-.1)t.suspensionRelativeVelocity=0,t.clippedInvContactDotSuspension=1/.1;else{const w=-1/p;t.suspensionRelativeVelocity=v*w,t.clippedInvContactDotSuspension=w}}else t.suspensionLength=t.suspensionRestLength+0*t.maxSuspensionTravel,t.suspensionRelativeVelocity=0,t.directionWorld.scale(-1,t.raycastResult.hitNormalWorld),t.clippedInvContactDotSuspension=1;return n}updateWheelTransformWorld(t){t.isInContact=!1;const e=this.chassisBody;e.pointToWorldFrame(t.chassisConnectionPointLocal,t.chassisConnectionPointWorld),e.vectorToWorldFrame(t.directionLocal,t.directionWorld),e.vectorToWorldFrame(t.axleLocal,t.axleWorld)}updateWheelTransform(t){const e=bi,s=Ai,i=Bi,n=this.wheelInfos[t];this.updateWheelTransformWorld(n),n.directionLocal.scale(-1,e),s.copy(n.axleLocal),e.cross(s,i),i.normalize(),s.normalize();const o=n.steering,r=new D;r.setFromAxisAngle(e,o);const c=new D;c.setFromAxisAngle(s,n.rotation);const a=n.worldTransform.quaternion;this.chassisBody.quaternion.mult(r,a),a.mult(c,a),a.normalize();const h=n.worldTransform.position;h.copy(n.directionWorld),h.scale(n.suspensionLength,h),h.vadd(n.chassisConnectionPointWorld,h)}getWheelTransformWorld(t){return this.wheelInfos[t].worldTransform}updateFriction(t){const e=Si,s=this.wheelInfos,i=s.length,n=this.chassisBody,o=Ii,r=Fi;this.numWheelsOnGround=0;for(let h=0;hv){this.sliding=!0,u.sliding=!0;const g=p/Math.sqrt(x);u.skidInfo*=g}}}if(this.sliding)for(let h=0;h1.1)return 0;const o=Vi,r=ji,c=ki;z.getVelocityAtWorldPoint(t,o),e.getVelocityAtWorldPoint(s,r),o.vsub(r,c);const a=i.dot(c),h=.2,u=1/(z.invMass+e.invMass);return-h*a*u}class Hi extends S{constructor(t){if(super({type:S.types.SPHERE}),this.radius=t!==void 0?t:1,this.radius<0)throw new Error("The sphere radius cannot be negative.");this.updateBoundingSphereRadius()}calculateLocalInertia(t,e){e===void 0&&(e=new l);const s=2*t*this.radius*this.radius/5;return e.x=s,e.y=s,e.z=s,e}volume(){return 4*Math.PI*Math.pow(this.radius,3)/3}updateBoundingSphereRadius(){this.boundingSphereRadius=this.radius}calculateWorldAABB(t,e,s,i){const n=this.radius,o=["x","y","z"];for(let r=0;rthis.particles.length&&this.neighbors.pop())}getNeighbors(t,e){const s=this.particles.length,i=t.id,n=this.smoothingRadius*this.smoothingRadius,o=Gi;for(let r=0;r!==s;r++){const c=this.particles[r];c.position.vsub(t.position,o),i!==c.id&&o.lengthSquared()e&&(e=n)}this.maxValue=e}setHeightValueAtIndex(t,e,s){const i=this.data;i[t][e]=s,this.clearCachedConvexTrianglePillar(t,e,!1),t>0&&(this.clearCachedConvexTrianglePillar(t-1,e,!0),this.clearCachedConvexTrianglePillar(t-1,e,!1)),e>0&&(this.clearCachedConvexTrianglePillar(t,e-1,!0),this.clearCachedConvexTrianglePillar(t,e-1,!1)),e>0&&t>0&&this.clearCachedConvexTrianglePillar(t-1,e-1,!0)}getRectMinMax(t,e,s,i,n){n===void 0&&(n=[]);const o=this.data;let r=this.minValue;for(let c=t;c<=s;c++)for(let a=e;a<=i;a++){const h=o[c][a];h>r&&(r=h)}n[0]=this.minValue,n[1]=r}getIndexOfPosition(t,e,s,i){const n=this.elementSize,o=this.data;let r=Math.floor(t/n),c=Math.floor(e/n);return s[0]=r,s[1]=c,i&&(r<0&&(r=0),c<0&&(c=0),r>=o.length-1&&(r=o.length-1),c>=o[0].length-1&&(c=o[0].length-1)),!(r<0||c<0||r>=o.length-1||c>=o[0].length-1)}getTriangleAt(t,e,s,i,n,o){const r=Ce;this.getIndexOfPosition(t,e,r,s);let c=r[0],a=r[1];const h=this.data;s&&(c=Math.min(h.length-2,Math.max(0,c)),a=Math.min(h[0].length-2,Math.max(0,a)));const u=this.elementSize,d=(t/u-c)**2+(e/u-a)**2,f=(t/u-(c+1))**2+(e/u-(a+1))**2,p=d>f;return this.getTriangle(c,a,p,i,n,o),p}getNormalAt(t,e,s,i){const n=tn,o=en,r=sn,c=nn,a=on;this.getTriangleAt(t,e,s,n,o,r),o.vsub(n,c),r.vsub(n,a),c.cross(a,i),i.normalize()}getAabbAtIndex(t,e,s){let{lowerBound:i,upperBound:n}=s;const o=this.data,r=this.elementSize;i.set(t*r,e*r,o[t][e]),n.set((t+1)*r,(e+1)*r,o[t+1][e+1])}getHeightAt(t,e,s){const i=this.data,n=Ki,o=Qi,r=Ji,c=Ce;this.getIndexOfPosition(t,e,c,s);let a=c[0],h=c[1];s&&(a=Math.min(i.length-2,Math.max(0,a)),h=Math.min(i[0].length-2,Math.max(0,h)));const u=this.getTriangleAt(t,e,s,n,o,r);rn(t,e,n.x,n.y,o.x,o.y,r.x,r.y,Se);const d=Se;return u?i[a+1][h+1]*d.x+i[a][h+1]*d.y+i[a+1][h]*d.z:i[a][h]*d.x+i[a+1][h]*d.y+i[a][h+1]*d.z}getCacheConvexTrianglePillarKey(t,e,s){return`${t}_${e}_${s?1:0}`}getCachedConvexTrianglePillar(t,e,s){return this._cachedPillars[this.getCacheConvexTrianglePillarKey(t,e,s)]}setCachedConvexTrianglePillar(t,e,s,i,n){this._cachedPillars[this.getCacheConvexTrianglePillarKey(t,e,s)]={convex:i,offset:n}}clearCachedConvexTrianglePillar(t,e,s){delete this._cachedPillars[this.getCacheConvexTrianglePillarKey(t,e,s)]}getTriangle(t,e,s,i,n,o){const r=this.data,c=this.elementSize;s?(i.set((t+1)*c,(e+1)*c,r[t+1][e+1]),n.set(t*c,(e+1)*c,r[t][e+1]),o.set((t+1)*c,e*c,r[t+1][e])):(i.set(t*c,e*c,r[t][e]),n.set((t+1)*c,e*c,r[t+1][e]),o.set(t*c,(e+1)*c,r[t][e+1]))}getConvexTrianglePillar(t,e,s){let i=this.pillarConvex,n=this.pillarOffset;if(this.cacheEnabled){const u=this.getCachedConvexTrianglePillar(t,e,s);if(u){this.pillarConvex=u.convex,this.pillarOffset=u.offset;return}i=new vt,n=new l,this.pillarConvex=i,this.pillarOffset=n}const o=this.data,r=this.elementSize,c=i.faces;i.vertices.length=6;for(let u=0;u<6;u++)i.vertices[u]||(i.vertices[u]=new l);c.length=5;for(let u=0;u<5;u++)c[u]||(c[u]=[]);const a=i.vertices,h=(Math.min(o[t][e],o[t+1][e],o[t][e+1],o[t+1][e+1])-this.minValue)/2+this.minValue;s?(n.set((t+.75)*r,(e+.75)*r,h),a[0].set(.25*r,.25*r,o[t+1][e+1]-h),a[1].set(-.75*r,.25*r,o[t][e+1]-h),a[2].set(.25*r,-.75*r,o[t+1][e]-h),a[3].set(.25*r,.25*r,-Math.abs(h)-1),a[4].set(-.75*r,.25*r,-Math.abs(h)-1),a[5].set(.25*r,-.75*r,-Math.abs(h)-1),c[0][0]=0,c[0][1]=1,c[0][2]=2,c[1][0]=5,c[1][1]=4,c[1][2]=3,c[2][0]=2,c[2][1]=5,c[2][2]=3,c[2][3]=0,c[3][0]=3,c[3][1]=4,c[3][2]=1,c[3][3]=0,c[4][0]=1,c[4][1]=4,c[4][2]=5,c[4][3]=2):(n.set((t+.25)*r,(e+.25)*r,h),a[0].set(-.25*r,-.25*r,o[t][e]-h),a[1].set(.75*r,-.25*r,o[t+1][e]-h),a[2].set(-.25*r,.75*r,o[t][e+1]-h),a[3].set(-.25*r,-.25*r,-Math.abs(h)-1),a[4].set(.75*r,-.25*r,-Math.abs(h)-1),a[5].set(-.25*r,.75*r,-Math.abs(h)-1),c[0][0]=0,c[0][1]=1,c[0][2]=2,c[1][0]=5,c[1][1]=4,c[1][2]=3,c[2][0]=0,c[2][1]=2,c[2][2]=5,c[2][3]=3,c[3][0]=1,c[3][1]=0,c[3][2]=3,c[3][3]=4,c[4][0]=4,c[4][1]=5,c[4][2]=2,c[4][3]=1),i.computeNormals(),i.computeEdges(),i.updateBoundingSphereRadius(),this.setCachedConvexTrianglePillar(t,e,s,i,n)}calculateLocalInertia(t,e){return e===void 0&&(e=new l),e.set(0,0,0),e}volume(){return Number.MAX_VALUE}calculateWorldAABB(t,e,s,i){s.set(-Number.MAX_VALUE,-Number.MAX_VALUE,-Number.MAX_VALUE),i.set(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE)}updateBoundingSphereRadius(){const t=this.data,e=this.elementSize;this.boundingSphereRadius=new l(t.length*e,t[0].length*e,Math.max(Math.abs(this.maxValue),Math.abs(this.minValue))).length()}setHeightsFromImage(t,e){const{x:s,z:i,y:n}=e,o=document.createElement("canvas");o.width=t.width,o.height=t.height;const r=o.getContext("2d");r.drawImage(t,0,0);const c=r.getImageData(0,0,t.width,t.height),a=this.data;a.length=0,this.elementSize=Math.abs(s)/c.width;for(let h=0;h=0;t--)this.children[t].removeEmptyNodes(),!this.children[t].children.length&&!this.children[t].data.length&&this.children.splice(t,1)}}class cn extends ot{constructor(t,e){e===void 0&&(e={}),super({root:null,aabb:t}),this.maxDepth=typeof e.maxDepth<"u"?e.maxDepth:8}}const wt=new l,jt=new G;class ee extends S{constructor(t,e){super({type:S.types.TRIMESH}),this.vertices=new Float32Array(t),this.indices=new Int16Array(e),this.normals=new Float32Array(e.length),this.aabb=new G,this.edges=null,this.scale=new l(1,1,1),this.tree=new cn,this.updateEdges(),this.updateNormals(),this.updateAABB(),this.updateBoundingSphereRadius(),this.updateTree()}updateTree(){const t=this.tree;t.reset(),t.aabb.copy(this.aabb);const e=this.scale;t.aabb.lowerBound.x*=1/e.x,t.aabb.lowerBound.y*=1/e.y,t.aabb.lowerBound.z*=1/e.z,t.aabb.upperBound.x*=1/e.x,t.aabb.upperBound.y*=1/e.y,t.aabb.upperBound.z*=1/e.z;const s=new G,i=new l,n=new l,o=new l,r=[i,n,o];for(let c=0;c{const o=is.x&&(s.x=n.x),n.ys.y&&(s.y=n.y),n.zs.z&&(s.z=n.z)}updateAABB(){this.computeLocalAABB(this.aabb)}updateBoundingSphereRadius(){let t=0;const e=this.vertices,s=new l;for(let i=0,n=e.length/3;i!==n;i++){this.getVertex(i,s);const o=s.lengthSquared();o>t&&(t=o)}this.boundingSphereRadius=Math.sqrt(t)}calculateWorldAABB(t,e,s,i){const n=un,o=pn;n.position=t,n.quaternion=e,this.aabb.toWorldFrame(n,o),s.copy(o.lowerBound),i.copy(o.upperBound)}volume(){return 4*Math.PI*this.boundingSphereRadius/3}static createTorus(t,e,s,i,n){t===void 0&&(t=1),e===void 0&&(e=.5),s===void 0&&(s=8),i===void 0&&(i=6),n===void 0&&(n=Math.PI*2);const o=[],r=[];for(let c=0;c<=s;c++)for(let a=0;a<=i;a++){const h=a/i*n,u=c/s*Math.PI*2,d=(t+e*Math.cos(u))*Math.cos(h),f=(t+e*Math.cos(u))*Math.sin(h),p=e*Math.sin(u);o.push(d,f,p)}for(let c=1;c<=s;c++)for(let a=1;a<=i;a++){const h=(i+1)*c+a-1,u=(i+1)*(c-1)+a-1,d=(i+1)*(c-1)+a,f=(i+1)*c+a;r.push(h,u,f),r.push(u,d,f)}return new ee(o,r)}}const an=new l,kt=new G,ln=new l,hn=new l,Fe=new l,Ie=new l,Me=new l,Ne=new l,_e=new l,xt=new G,dn=new l,un=new j,pn=new G;class Ue{constructor(){this.equations=[]}solve(t,e){return 0}addEquation(t){t.enabled&&!t.bi.isTrigger&&!t.bj.isTrigger&&this.equations.push(t)}removeEquation(t){const e=this.equations,s=e.indexOf(t);s!==-1&&e.splice(s,1)}removeAllEquations(){this.equations.length=0}}class fn extends Ue{constructor(){super(),this.iterations=10,this.tolerance=1e-7}solve(t,e){let s=0;const i=this.iterations,n=this.tolerance*this.tolerance,o=this.equations,r=o.length,c=e.bodies,a=c.length,h=t;let u,d,f,p,m,v;if(r!==0)for(let g=0;g!==a;g++)c[g].updateSolveMassProperties();const w=vn,y=yn,x=mn;w.length=r,y.length=r,x.length=r;for(let g=0;g!==r;g++){const A=o[g];x[g]=0,y[g]=A.computeB(h),w[g]=1/A.computeC()}if(r!==0){for(let E=0;E!==a;E++){const I=c[E],_=I.vlambda,q=I.wlambda;_.set(0,0,0),q.set(0,0,0)}for(s=0;s!==i;s++){p=0;for(let E=0;E!==r;E++){const I=o[E];u=y[E],d=w[E],v=x[E],m=I.computeGWlambda(),f=d*(u-m-I.eps*v),v+fI.maxForce&&(f=I.maxForce-v),x[E]+=f,p+=f>0?f:-f,I.addToWlambda(f)}if(p*pt;)e.pop();for(;e.length=0&&h.restitution>=0&&(r.restitution=a.restitution*h.restitution),r.si=n||s,r.sj=o||i,r}createFrictionEquationsFromContact(t,e){const s=t.bi,i=t.bj,n=t.si,o=t.sj,r=this.world,c=this.currentContactMaterial;let a=c.friction;const h=n.material||s.material,u=o.material||i.material;if(h&&u&&h.friction>=0&&u.friction>=0&&(a=h.friction*u.friction),a>0){const d=a*r.gravity.length();let f=s.invMass+i.invMass;f>0&&(f=1/f);const p=this.frictionEquationPool,m=p.length?p.pop():new be(s,i,d*f),v=p.length?p.pop():new be(s,i,d*f);return m.bi=v.bi=s,m.bj=v.bj=i,m.minForce=v.minForce=-d*f,m.maxForce=v.maxForce=d*f,m.ri.copy(t.ri),m.rj.copy(t.rj),v.ri.copy(t.ri),v.rj.copy(t.rj),t.ni.tangents(m.t,v.t),m.setSpookParams(c.frictionEquationStiffness,c.frictionEquationRelaxation,r.dt),v.setSpookParams(c.frictionEquationStiffness,c.frictionEquationRelaxation,r.dt),m.enabled=v.enabled=t.enabled,e.push(m,v),!0}return!1}createFrictionFromAverage(t){let e=this.result[this.result.length-1];if(!this.createFrictionEquationsFromContact(e,this.frictionResult)||t===1)return;const s=this.frictionResult[this.frictionResult.length-2],i=this.frictionResult[this.frictionResult.length-1];gt.setZero(),zt.setZero(),Ct.setZero();const n=e.bi;e.bj;for(let r=0;r!==t;r++)e=this.result[this.result.length-1-r],e.bi!==n?(gt.vadd(e.ni,gt),zt.vadd(e.ri,zt),Ct.vadd(e.rj,Ct)):(gt.vsub(e.ni,gt),zt.vadd(e.rj,zt),Ct.vadd(e.ri,Ct));const o=1/t;zt.scale(o,s.ri),Ct.scale(o,s.rj),i.ri.copy(s.ri),i.rj.copy(s.rj),gt.normalize(),gt.tangents(s.t,i.t)}getContacts(t,e,s,i,n,o,r){this.contactPointPool=n,this.frictionEquationPool=r,this.result=i,this.frictionResult=o;const c=Mn,a=Nn,h=Fn,u=In;for(let d=0,f=t.length;d!==f;d++){const p=t[d],m=e[d];let v=null;p.material&&m.material&&(v=s.getContactMaterial(p.material,m.material)||null);const w=p.type&M.KINEMATIC&&m.type&M.STATIC||p.type&M.STATIC&&m.type&M.KINEMATIC||p.type&M.KINEMATIC&&m.type&M.KINEMATIC;for(let y=0;yx.boundingSphereRadius+A.boundingSphereRadius)continue;let E=null;x.material&&A.material&&(E=s.getContactMaterial(x.material,A.material)||null),this.currentContactMaterial=E||v||s.defaultContactMaterial;const I=x.type|A.type,_=this[I];if(_){let q=!1;x.type0){const O=eo,N=so;O.copy(f[(B+1)%3]),N.copy(f[(B+2)%3]);const Z=O.length(),K=N.length();O.normalize(),N.normalize();const Q=Gt.dot(O),U=Gt.dot(N);if(Q-Z&&U-K){const $=Math.abs(W-R-p);if((I===null||$t.boundingSphereRadius+e.boundingSphereRadius)&&t.findSeparatingAxis(e,s,n,i,o,p,d,f)){const m=[],v=Eo;t.clipAgainstHull(s,n,e,i,o,p,-100,100,m);let w=0;for(let y=0;y!==m.length;y++){if(u)return!0;const x=this.createContactEquation(r,c,t,e,a,h),g=x.ri,A=x.rj;p.negate(x.ni),m[y].normal.negate(v),v.scale(m[y].depth,v),m[y].point.vadd(v,g),A.copy(m[y].point),g.vsub(s,g),A.vsub(i,A),g.vadd(s,g),g.vsub(r.position,g),A.vadd(i,A),A.vsub(c.position,A),this.result.push(x),w++,this.enableFrictionReduction||this.createFrictionEquationsFromContact(x,this.frictionResult)}this.enableFrictionReduction&&w&&this.createFrictionFromAverage(w)}}sphereConvex(t,e,s,i,n,o,r,c,a,h,u){const d=this.v3pool;s.vsub(i,ao);const f=e.faceNormals,p=e.faces,m=e.vertices,v=t.radius;let w=!1;for(let y=0;y!==m.length;y++){const x=m[y],g=po;o.vmult(x,g),i.vadd(g,g);const A=uo;if(g.vsub(s,A),A.lengthSquared()0){const V=[];for(let b=0,F=A.length;b!==F;b++){const C=d.get();o.vmult(m[A[b]],C),i.vadd(C,C),V.push(C)}if(Jn(V,E,s)){if(u)return!0;w=!0;const b=this.createContactEquation(r,c,t,e,a,h);E.scale(-v,b.ri),E.negate(b.ni);const F=d.get();E.scale(-T,F);const C=d.get();E.scale(-v,C),s.vsub(i,b.rj),b.rj.vadd(C,b.rj),b.rj.vadd(F,b.rj),b.rj.vadd(i,b.rj),b.rj.vsub(c.position,b.rj),b.ri.vadd(s,b.ri),b.ri.vsub(r.position,b.ri),d.release(F),d.release(C),this.result.push(b),this.createFrictionEquationsFromContact(b,this.frictionResult);for(let B=0,k=V.length;B!==k;B++)d.release(V[B]);return}else for(let b=0;b!==A.length;b++){const F=d.get(),C=d.get();o.vmult(m[A[(b+1)%A.length]],F),o.vmult(m[A[(b+2)%A.length]],C),i.vadd(F,F),i.vadd(C,C);const B=lo;C.vsub(F,B);const k=ho;B.unit(k);const L=d.get(),R=d.get();s.vsub(F,R);const W=R.dot(k);k.scale(W,L),L.vadd(F,L);const O=d.get();if(L.vsub(s,O),W>0&&W*Wd.length||x>d[0].length)return;w<0&&(w=0),y<0&&(y=0),x<0&&(x=0),g<0&&(g=0),w>=d.length&&(w=d.length-1),y>=d.length&&(y=d.length-1),g>=d[0].length&&(g=d[0].length-1),x>=d[0].length&&(x=d[0].length-1);const A=[];e.getRectMinMax(w,x,y,g,A);const E=A[0],I=A[1];if(v.z-f>I||v.z+f2)return}}boxHeightfield(t,e,s,i,n,o,r,c,a,h,u){return t.convexPolyhedronRepresentation.material=t.material,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,this.convexHeightfield(t.convexPolyhedronRepresentation,e,s,i,n,o,r,c,t,e,u)}convexHeightfield(t,e,s,i,n,o,r,c,a,h,u){const d=e.data,f=e.elementSize,p=t.boundingSphereRadius,m=To,v=Ro,w=_o;j.pointToLocalFrame(i,o,s,w);let y=Math.floor((w.x-p)/f)-1,x=Math.ceil((w.x+p)/f)+1,g=Math.floor((w.y-p)/f)-1,A=Math.ceil((w.y+p)/f)+1;if(x<0||A<0||y>d.length||g>d[0].length)return;y<0&&(y=0),x<0&&(x=0),g<0&&(g=0),A<0&&(A=0),y>=d.length&&(y=d.length-1),x>=d.length&&(x=d.length-1),A>=d[0].length&&(A=d[0].length-1),g>=d[0].length&&(g=d[0].length-1);const E=[];e.getRectMinMax(y,g,x,A,E);const I=E[0],_=E[1];if(!(w.z-p>_||w.z+p0&&C<0&&(v.vsub(d,w),m.copy(p),m.normalize(),B=w.dot(m),m.scale(B,w),w.vadd(d,w),w.distanceTo(v)0&&s===!0||h<=0&&s===!1){s===null&&(s=h>0);continue}else return!1}return!0}const Gt=new l,to=new l,eo=new l,so=new l,io=[new l,new l,new l,new l,new l,new l],no=new l,oo=new l,ro=new l,co=new l,ao=new l,lo=new l,ho=new l,uo=new l,po=new l,fo=new l,mo=new l,vo=new l,yo=new l,wo=new l;new l;new l;const xo=new l,go=new l,bo=new l,Ao=new l,Bo=new l,Eo=new l,zo=new l,Co=new l,So=new l,Fo=new l,Re=new D,Io=new l;new l;const Mo=new l,Pe=new l,No=new l,_o=new l,To=new l,Ro=[0],Po=new l,qo=new l;class qe{constructor(){this.current=[],this.previous=[]}getKey(t,e){if(ei[n];)n++;if(s!==i[n]){for(let o=i.length-1;o>=n;o--)i[o+1]=i[o];i[n]=s}}tick(){const t=this.current;this.current=this.previous,this.previous=t,this.current.length=0}getDiff(t,e){const s=this.current,i=this.previous,n=s.length,o=i.length;let r=0;for(let c=0;ci[r];)r++;a=h===i[r],a||Le(t,h)}r=0;for(let c=0;cs[r];)r++;a=s[r]===h,a||Le(e,h)}}}function Le(z,t){z.push((t&4294901760)>>16,t&65535)}const Jt=(z,t)=>z0;){const s=e.pop();delete t[s]}}}class nr extends We{constructor(t){t===void 0&&(t={}),super(),this.dt=-1,this.allowSleep=!!t.allowSleep,this.contacts=[],this.frictionEquations=[],this.quatNormalizeSkip=t.quatNormalizeSkip!==void 0?t.quatNormalizeSkip:0,this.quatNormalizeFast=t.quatNormalizeFast!==void 0?t.quatNormalizeFast:!1,this.time=0,this.stepnumber=0,this.default_dt=1/60,this.nextId=0,this.gravity=new l,t.gravity&&this.gravity.copy(t.gravity),this.broadphase=t.broadphase!==void 0?t.broadphase:new As,this.bodies=[],this.hasActiveBodies=!1,this.solver=t.solver!==void 0?t.solver:new fn,this.constraints=[],this.narrowphase=new Sn(this),this.collisionMatrix=new de,this.collisionMatrixPrevious=new de,this.bodyOverlapKeeper=new qe,this.shapeOverlapKeeper=new qe,this.contactmaterials=[],this.contactMaterialTable=new Lo,this.defaultMaterial=new Xt("default"),this.defaultContactMaterial=new Yt(this.defaultMaterial,this.defaultMaterial,{friction:.3,restitution:0}),this.doProfiling=!1,this.profile={solve:0,makeContactConstraints:0,broadphase:0,integrate:0,narrowphase:0},this.accumulator=0,this.subsystems=[],this.addBodyEvent={type:"addBody",body:null},this.removeBodyEvent={type:"removeBody",body:null},this.idToBodyMap={},this.broadphase.setWorld(this)}getContactMaterial(t,e){return this.contactMaterialTable.get(t.id,e.id)}collisionMatrixTick(){const t=this.collisionMatrixPrevious;this.collisionMatrixPrevious=this.collisionMatrix,this.collisionMatrix=t,this.collisionMatrix.reset(),this.bodyOverlapKeeper.tick(),this.shapeOverlapKeeper.tick()}addConstraint(t){this.constraints.push(t)}removeConstraint(t){const e=this.constraints.indexOf(t);e!==-1&&this.constraints.splice(e,1)}rayTest(t,e,s){s instanceof Rt?this.raycastClosest(t,e,{skipBackfaces:!0},s):this.raycastAll(t,e,{skipBackfaces:!0},s)}raycastAll(t,e,s,i){return s===void 0&&(s={}),s.mode=Y.ALL,s.from=t,s.to=e,s.callback=i,te.intersectWorld(this,s)}raycastAny(t,e,s,i){return s===void 0&&(s={}),s.mode=Y.ANY,s.from=t,s.to=e,s.result=i,te.intersectWorld(this,s)}raycastClosest(t,e,s,i){return s===void 0&&(s={}),s.mode=Y.CLOSEST,s.from=t,s.to=e,s.result=i,te.intersectWorld(this,s)}addBody(t){this.bodies.includes(t)||(t.index=this.bodies.length,this.bodies.push(t),t.world=this,t.initPosition.copy(t.position),t.initVelocity.copy(t.velocity),t.timeLastSleepy=this.time,t instanceof M&&(t.initAngularVelocity.copy(t.angularVelocity),t.initQuaternion.copy(t.quaternion)),this.collisionMatrix.setNumObjects(this.bodies.length),this.addBodyEvent.body=t,this.idToBodyMap[t.id]=t,this.dispatchEvent(this.addBodyEvent))}removeBody(t){t.world=null;const e=this.bodies.length-1,s=this.bodies,i=s.indexOf(t);if(i!==-1){s.splice(i,1);for(let n=0;n!==s.length;n++)s[n].index=n;this.collisionMatrix.setNumObjects(e),this.removeBodyEvent.body=t,delete this.idToBodyMap[t.id],this.dispatchEvent(this.removeBodyEvent)}}getBodyById(t){return this.idToBodyMap[t]}getShapeById(t){const e=this.bodies;for(let s=0;s=t&&nt*1e3)););this.accumulator=this.accumulator%t;const o=this.accumulator/t;for(let r=0;r!==this.bodies.length;r++){const c=this.bodies[r];c.previousPosition.lerp(c.position,o,c.interpolatedPosition),c.previousQuaternion.slerp(c.quaternion,o,c.interpolatedQuaternion),c.previousQuaternion.normalize()}this.time+=e}}internalStep(t){this.dt=t;const e=this.contacts,s=Oo,i=Ho,n=this.bodies.length,o=this.bodies,r=this.solver,c=this.gravity,a=this.doProfiling,h=this.profile,u=M.DYNAMIC;let d=-1/0;const f=this.constraints,p=ko;c.length();const m=c.x,v=c.y,w=c.z;let y=0;for(a&&(d=X.now()),y=0;y!==n;y++){const b=o[y];if(b.type===u){const F=b.force,C=b.mass;F.x+=C*m,F.y+=C*v,F.z+=C*w}}for(let b=0,F=this.subsystems.length;b!==F;b++)this.subsystems[b].update();a&&(d=X.now()),s.length=0,i.length=0,this.broadphase.collisionPairs(this,s,i),a&&(h.broadphase=X.now()-d);let x=f.length;for(y=0;y!==x;y++){const b=f[y];if(!b.collideConnected)for(let F=s.length-1;F>=0;F-=1)(b.bodyA===s[F]&&b.bodyB===i[F]||b.bodyB===s[F]&&b.bodyA===i[F])&&(s.splice(F,1),i.splice(F,1))}this.collisionMatrixTick(),a&&(d=X.now());const g=jo,A=e.length;for(y=0;y!==A;y++)g.push(e[y]);e.length=0;const E=this.frictionEquations.length;for(y=0;y!==E;y++)p.push(this.frictionEquations[y]);for(this.frictionEquations.length=0,this.narrowphase.getContacts(s,i,this,e,g,this.frictionEquations,p),a&&(h.narrowphase=X.now()-d),a&&(d=X.now()),y=0;y=0&&B.material.friction>=0&&C.material.friction*B.material.friction,C.material.restitution>=0&&B.material.restitution>=0&&(F.restitution=C.material.restitution*B.material.restitution)),r.addEquation(F),C.allowSleep&&C.type===M.DYNAMIC&&C.sleepState===M.SLEEPING&&B.sleepState===M.AWAKE&&B.type!==M.STATIC){const W=B.velocity.lengthSquared()+B.angularVelocity.lengthSquared(),O=B.sleepSpeedLimit**2;W>=O*2&&(C.wakeUpAfterNarrowphase=!0)}if(B.allowSleep&&B.type===M.DYNAMIC&&B.sleepState===M.SLEEPING&&C.sleepState===M.AWAKE&&C.type!==M.STATIC){const W=C.velocity.lengthSquared()+C.angularVelocity.lengthSquared(),O=C.sleepSpeedLimit**2;W>=O*2&&(B.wakeUpAfterNarrowphase=!0)}this.collisionMatrix.set(C,B,!0),this.collisionMatrixPrevious.get(C,B)||(It.body=B,It.contact=F,C.dispatchEvent(It),It.body=C,B.dispatchEvent(It)),this.bodyOverlapKeeper.set(C.id,B.id),this.shapeOverlapKeeper.set(k.id,L.id)}for(this.emitContactEvents(),a&&(h.makeContactConstraints=X.now()-d,d=X.now()),y=0;y!==n;y++){const b=o[y];b.wakeUpAfterNarrowphase&&(b.wakeUp(),b.wakeUpAfterNarrowphase=!1)}for(x=f.length,y=0;y!==x;y++){const b=f[y];b.update();for(let F=0,C=b.equations.length;F!==C;F++){const B=b.equations[F];r.addEquation(B)}}r.solve(t,this),a&&(h.solve=X.now()-d),r.removeAllEquations();const _=Math.pow;for(y=0;y!==n;y++){const b=o[y];if(b.type&u){const F=_(1-b.linearDamping,t),C=b.velocity;C.scale(F,C);const B=b.angularVelocity;if(B){const k=_(1-b.angularDamping,t);B.scale(k,B)}}}this.dispatchEvent(Vo),a&&(d=X.now());const T=this.stepnumber%(this.quatNormalizeSkip+1)===0,P=this.quatNormalizeFast;for(y=0;y!==n;y++)o[y].integrate(t,T,P);this.clearForces(),this.broadphase.dirty=!0,a&&(h.integrate=X.now()-d),this.stepnumber+=1,this.dispatchEvent(Wo);let V=!0;if(this.allowSleep)for(V=!1,y=0;y!==n;y++){const b=o[y];b.sleepTick(this.time),b.sleepState!==M.SLEEPING&&(V=!0)}this.hasActiveBodies=V}emitContactEvents(){const t=this.hasAnyEventListener("beginContact"),e=this.hasAnyEventListener("endContact");if((t||e)&&this.bodyOverlapKeeper.getDiff(dt,ut),t){for(let n=0,o=dt.length;nDate.now()-z}new l;const Wo={type:"postStep"},Vo={type:"preStep"},It={type:M.COLLIDE_EVENT_NAME,body:null,contact:null},jo=[],ko=[],Oo=[],Ho=[],dt=[],ut=[],Mt={type:"beginContact",bodyA:null,bodyB:null},Nt={type:"endContact",bodyA:null,bodyB:null},pt={type:"beginShapeContact",bodyA:null,bodyB:null,shapeA:null,shapeB:null},ft={type:"endShapeContact",bodyA:null,bodyB:null,shapeA:null,shapeB:null};export{G as AABB,de as ArrayCollisionMatrix,ie as BODY_SLEEP_STATES,se as BODY_TYPES,M as Body,Dt as Box,ne as Broadphase,H as COLLISION_TYPES,Uo as ConeTwistConstraint,Pt as Constraint,Tt as ContactEquation,Yt as ContactMaterial,vt as ConvexPolyhedron,Jo as Cylinder,Yo as DistanceConstraint,yt as Equation,We as EventTarget,be as FrictionEquation,fn as GSSolver,Do as GridBroadphase,sr as Heightfield,ni as HingeConstraint,ve as JacobianElement,Xo as LockConstraint,et as Mat3,Xt as Material,As as NaiveBroadphase,Sn as Narrowphase,Go as ObjectCollisionMatrix,tr as Particle,er as Plane,re as PointToPointConstraint,zn as Pool,D as Quaternion,oe as RAY_MODES,Y as Ray,Rt as RaycastResult,Zo as RaycastVehicle,Ko as RigidVehicle,St as RotationalEquation,ii as RotationalMotorEquation,_t as SAPBroadphase,Je as SHAPE_TYPES,Qo as SPHSystem,S as Shape,Ue as Solver,Hi as Sphere,ir as SplitSolver,$o as Spring,j as Transform,ee as Trimesh,l as Vec3,Cn as Vec3Pool,gi as WheelInfo,nr as World};