TOPVERSE_Official/.output/public/_nuxt/cannon-es.73300c7b.js

2 lines
122 KiB
JavaScript

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;e<this.elements.length;e++)this.elements[e]*=t}mmult(t,e){e===void 0&&(e=new et);const s=this.elements,i=t.elements,n=e.elements,o=s[0],r=s[1],c=s[2],a=s[3],h=s[4],u=s[5],d=s[6],f=s[7],p=s[8],m=i[0],v=i[1],w=i[2],y=i[3],x=i[4],g=i[5],A=i[6],E=i[7],I=i[8];return n[0]=o*m+r*y+c*A,n[1]=o*v+r*x+c*E,n[2]=o*w+r*g+c*I,n[3]=a*m+h*y+u*A,n[4]=a*v+h*x+u*E,n[5]=a*w+h*g+u*I,n[6]=d*m+f*y+p*A,n[7]=d*v+f*x+p*E,n[8]=d*w+f*g+p*I,e}scale(t,e){e===void 0&&(e=new et);const s=this.elements,i=e.elements;for(let n=0;n!==3;n++)i[3*n+0]=t.x*s[3*n+0],i[3*n+1]=t.y*s[3*n+1],i[3*n+2]=t.z*s[3*n+2];return e}solve(t,e){e===void 0&&(e=new l);const s=3,i=4,n=[];let o,r;for(o=0;o<s*i;o++)n.push(0);for(o=0;o<3;o++)for(r=0;r<3;r++)n[o+i*r]=this.elements[o+3*r];n[3+4*0]=t.x,n[3+4*1]=t.y,n[3+4*2]=t.z;let c=3;const a=c;let h;const u=4;let d;do{if(o=a-c,n[o+i*o]===0){for(r=o+1;r<a;r++)if(n[o+i*r]!==0){h=u;do d=u-h,n[d+i*o]+=n[d+i*r];while(--h);break}}if(n[o+i*o]!==0)for(r=o+1;r<a;r++){const f=n[o+i*r]/n[o+i*o];h=u;do d=u-h,n[d+i*r]=d<=o?0:n[d+i*r]-n[d+i*o]*f;while(--h)}}while(--c);if(e.z=n[2*i+3]/n[2*i+2],e.y=(n[1*i+3]-n[1*i+2]*e.z)/n[1*i+1],e.x=(n[0*i+3]-n[0*i+2]*e.z-n[0*i+1]*e.y)/n[0*i+0],isNaN(e.x)||isNaN(e.y)||isNaN(e.z)||e.x===1/0||e.y===1/0||e.z===1/0)throw`Could not solve equation! Got x=[${e.toString()}], b=[${t.toString()}], A=[${this.toString()}]`;return e}e(t,e,s){if(s===void 0)return this.elements[e+3*t];this.elements[e+3*t]=s}copy(t){for(let e=0;e<t.elements.length;e++)this.elements[e]=t.elements[e];return this}toString(){let t="";const e=",";for(let s=0;s<9;s++)t+=this.elements[s]+e;return t}reverse(t){t===void 0&&(t=new et);const e=3,s=6,i=Xe;let n,o;for(n=0;n<3;n++)for(o=0;o<3;o++)i[n+s*o]=this.elements[n+3*o];i[3+6*0]=1,i[3+6*1]=0,i[3+6*2]=0,i[4+6*0]=0,i[4+6*1]=1,i[4+6*2]=0,i[5+6*0]=0,i[5+6*1]=0,i[5+6*2]=1;let r=3;const c=r;let a;const h=s;let u;do{if(n=c-r,i[n+s*n]===0){for(o=n+1;o<c;o++)if(i[n+s*o]!==0){a=h;do u=h-a,i[u+s*n]+=i[u+s*o];while(--a);break}}if(i[n+s*n]!==0)for(o=n+1;o<c;o++){const d=i[n+s*o]/i[n+s*n];a=h;do u=h-a,i[u+s*o]=u<=n?0:i[u+s*o]-i[u+s*n]*d;while(--a)}}while(--r);n=2;do{o=n-1;do{const d=i[n+s*o]/i[n+s*n];a=s;do u=s-a,i[u+s*o]=i[u+s*o]-i[u+s*n]*d;while(--a)}while(o--)}while(--n);n=2;do{const d=1/i[n+s*n];a=s;do u=s-a,i[u+s*n]=i[u+s*n]*d;while(--a)}while(n--);n=2;do{o=2;do{if(u=i[e+o+s*n],isNaN(u)||u===1/0)throw`Could not reverse! A=[${this.toString()}]`;t.e(n,o,u)}while(o--)}while(n--);return t}setRotationFromQuaternion(t){const e=t.x,s=t.y,i=t.z,n=t.w,o=e+e,r=s+s,c=i+i,a=e*o,h=e*r,u=e*c,d=s*r,f=s*c,p=i*c,m=n*o,v=n*r,w=n*c,y=this.elements;return y[3*0+0]=1-(d+p),y[3*0+1]=h-w,y[3*0+2]=u+v,y[3*1+0]=h+w,y[3*1+1]=1-(a+p),y[3*1+2]=f-m,y[3*2+0]=u-v,y[3*2+1]=f+m,y[3*2+2]=1-(a+d),this}transpose(t){t===void 0&&(t=new et);const e=this.elements,s=t.elements;let i;return s[0]=e[0],s[4]=e[4],s[8]=e[8],i=e[1],s[1]=e[3],s[3]=i,i=e[2],s[2]=e[6],s[6]=i,i=e[5],s[5]=e[7],s[7]=i,t}}const Xe=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];class l{constructor(t,e,s){t===void 0&&(t=0),e===void 0&&(e=0),s===void 0&&(s=0),this.x=t,this.y=e,this.z=s}cross(t,e){e===void 0&&(e=new l);const s=t.x,i=t.y,n=t.z,o=this.x,r=this.y,c=this.z;return e.x=r*n-c*i,e.y=c*s-o*n,e.z=o*i-r*s,e}set(t,e,s){return this.x=t,this.y=e,this.z=s,this}setZero(){this.x=this.y=this.z=0}vadd(t,e){if(e)e.x=t.x+this.x,e.y=t.y+this.y,e.z=t.z+this.z;else return new l(this.x+t.x,this.y+t.y,this.z+t.z)}vsub(t,e){if(e)e.x=this.x-t.x,e.y=this.y-t.y,e.z=this.z-t.z;else return new l(this.x-t.x,this.y-t.y,this.z-t.z)}crossmat(){return new et([0,-this.z,this.y,this.z,0,-this.x,-this.y,this.x,0])}normalize(){const t=this.x,e=this.y,s=this.z,i=Math.sqrt(t*t+e*e+s*s);if(i>0){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;c<t.length;c++){let a=t[c];r&&(r.vmult(a,le),a=le),a.x>o.x&&(o.x=a.x),a.x<n.x&&(n.x=a.x),a.y>o.y&&(o.y=a.y),a.y<n.y&&(n.y=a.y),a.z>o.z&&(o.z=a.z),a.z<n.z&&(n.z=a.z)}return e&&(e.vadd(n,n),e.vadd(o,o)),i&&(n.x-=i,n.y-=i,n.z-=i,o.x+=i,o.y+=i,o.z+=i),this}copy(t){return this.lowerBound.copy(t.lowerBound),this.upperBound.copy(t.upperBound),this}clone(){return new G().copy(this)}extend(t){this.lowerBound.x=Math.min(this.lowerBound.x,t.lowerBound.x),this.upperBound.x=Math.max(this.upperBound.x,t.upperBound.x),this.lowerBound.y=Math.min(this.lowerBound.y,t.lowerBound.y),this.upperBound.y=Math.max(this.upperBound.y,t.upperBound.y),this.lowerBound.z=Math.min(this.lowerBound.z,t.lowerBound.z),this.upperBound.z=Math.max(this.upperBound.z,t.upperBound.z)}overlaps(t){const e=this.lowerBound,s=this.upperBound,i=t.lowerBound,n=t.upperBound,o=i.x<=s.x&&s.x<=n.x||e.x<=n.x&&n.x<=s.x,r=i.y<=s.y&&s.y<=n.y||e.y<=n.y&&n.y<=s.y,c=i.z<=s.z&&s.z<=n.z||e.z<=n.z&&n.z<=s.z;return o&&r&&c}volume(){const t=this.lowerBound,e=this.upperBound;return(e.x-t.x)*(e.y-t.y)*(e.z-t.z)}contains(t){const e=this.lowerBound,s=this.upperBound,i=t.lowerBound,n=t.upperBound;return e.x<=i.x&&s.x>=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<n;i++)s[i].call(this,t)}return this}}class D{constructor(t,e,s,i){t===void 0&&(t=0),e===void 0&&(e=0),s===void 0&&(s=0),i===void 0&&(i=1),this.x=t,this.y=e,this.z=s,this.w=i}set(t,e,s,i){return this.x=t,this.y=e,this.z=s,this.w=i,this}toString(){return`${this.x},${this.y},${this.z},${this.w}`}toArray(){return[this.x,this.y,this.z,this.w]}setFromAxisAngle(t,e){const s=Math.sin(e*.5);return this.x=t.x*s,this.y=t.y*s,this.z=t.z*s,this.w=Math.cos(e*.5),this}toAxisAngle(t){t===void 0&&(t=new l),this.normalize();const e=2*Math.acos(this.w),s=Math.sqrt(1-this.w*this.w);return s<.001?(t.x=this.x,t.y=this.y,t.z=this.z):(t.x=this.x/s,t.y=this.y/s,t.z=this.z/s),[t,e]}setFromVectors(t,e){if(t.isAntiparallelTo(e)){const s=Ke,i=Qe;t.tangents(s,i),this.setFromAxisAngle(s,Math.PI)}else{const s=t.cross(e);this.x=s.x,this.y=s.y,this.z=s.z,this.w=Math.sqrt(t.length()**2*e.length()**2)+t.dot(e),this.normalize()}return this}mult(t,e){e===void 0&&(e=new D);const s=this.x,i=this.y,n=this.z,o=this.w,r=t.x,c=t.y,a=t.z,h=t.w;return e.x=s*h+o*r+i*a-n*c,e.y=i*h+o*c+n*r-s*a,e.z=n*h+o*a+s*c-i*r,e.w=o*h-s*r-i*c-n*a,e}inverse(t){t===void 0&&(t=new D);const e=this.x,s=this.y,i=this.z,n=this.w;this.conjugate(t);const o=1/(e*e+s*s+i*i+n*n);return t.x*=o,t.y*=o,t.z*=o,t.w*=o,t}conjugate(t){return t===void 0&&(t=new D),t.x=-this.x,t.y=-this.y,t.z=-this.z,t.w=this.w,t}normalize(){let t=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);return t===0?(this.x=0,this.y=0,this.z=0,this.w=0):(t=1/t,this.x*=t,this.y*=t,this.z*=t,this.w*=t),this}normalizeFast(){const t=(3-(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w))/2;return t===0?(this.x=0,this.y=0,this.z=0,this.w=0):(this.x*=t,this.y*=t,this.z*=t,this.w*=t),this}vmult(t,e){e===void 0&&(e=new l);const s=t.x,i=t.y,n=t.z,o=this.x,r=this.y,c=this.z,a=this.w,h=a*s+r*n-c*i,u=a*i+c*s-o*n,d=a*n+o*i-r*s,f=-o*s-r*i-c*n;return e.x=h*a+f*-o+u*-c-d*-r,e.y=u*a+f*-r+d*-o-h*-c,e.z=d*a+f*-c+h*-r-u*-o,e}copy(t){return this.x=t.x,this.y=t.y,this.z=t.z,this.w=t.w,this}toEuler(t,e){e===void 0&&(e="YZX");let s,i,n;const o=this.x,r=this.y,c=this.z,a=this.w;switch(e){case"YZX":const h=o*r+c*a;if(h>.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;t<this.faces.length;t++){for(let i=0;i<this.faces[t].length;i++)if(!this.vertices[this.faces[t][i]])throw new Error(`Vertex ${this.faces[t][i]} not found!`);const e=this.faceNormals[t]||new l;this.getFaceNormal(t,e),e.negate(e),this.faceNormals[t]=e;const s=this.vertices[this.faces[t][0]];if(e.dot(s)<0){console.error(`.faceNormals[${t}] = Vec3(${e.toString()}) looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right hand rule.`);for(let i=0;i<this.faces[t].length;i++)console.warn(`.vertices[${this.faces[t][i]}] = Vec3(${this.vertices[this.faces[t][i]].toString()})`)}}}getFaceNormal(t,e){const s=this.faces[t],i=this.vertices[s[0]],n=this.vertices[s[1]],o=this.vertices[s[2]];vt.computeNormal(i,n,o,e)}static computeNormal(t,e,s,i){const n=new l,o=new l;e.vsub(t,o),s.vsub(e,n),n.cross(o,i),i.isZero()||i.normalize()}clipAgainstHull(t,e,s,i,n,o,r,c,a){const h=new l;let u=-1,d=-Number.MAX_VALUE;for(let p=0;p<s.faces.length;p++){h.copy(s.faceNormals[p]),n.vmult(h,h);const m=h.dot(o);m>d&&(d=m,u=p)}const f=[];for(let p=0;p<s.faces[u].length;p++){const m=s.vertices[s.faces[u][p]],v=new l;v.copy(m),n.vmult(v,v),i.vadd(v,v),f.push(v)}u>=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;y<m&&(m=y,o.copy(a))}else{const w=r?r.length:v.faces.length;for(let y=0;y<w;y++){const x=r?r[y]:y;a.copy(v.faceNormals[x]),s.vmult(a,a);const g=v.testSepAxis(a,t,e,s,i,n);if(g===!1)return!1;g<m&&(m=g,o.copy(a))}}if(t.uniqueAxes)for(let w=0;w!==t.uniqueAxes.length;w++){n.vmult(t.uniqueAxes[w],h);const y=v.testSepAxis(h,t,e,s,i,n);if(y===!1)return!1;y<m&&(m=y,o.copy(h))}else{const w=c?c.length:t.faces.length;for(let y=0;y<w;y++){const x=c?c[y]:y;h.copy(t.faceNormals[x]),n.vmult(h,h);const g=v.testSepAxis(h,t,e,s,i,n);if(g===!1)return!1;g<m&&(m=g,o.copy(h))}}for(let w=0;w!==v.uniqueEdges.length;w++){s.vmult(v.uniqueEdges[w],d);for(let y=0;y!==t.uniqueEdges.length;y++)if(n.vmult(t.uniqueEdges[y],f),d.cross(f,p),!p.almostZero()){p.normalize();const x=v.testSepAxis(p,t,e,s,i,n);if(x===!1)return!1;x<m&&(m=x,o.copy(p))}}return i.vsub(e,u),u.dot(o)>0&&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(c<u||h<a)return!1;const d=c-u,f=h-a;return d<f?d:f}calculateLocalInertia(t,e){const s=new l,i=new l;this.computeLocalAABB(i,s);const n=s.x-i.x,o=s.y-i.y,r=s.z-i.z;e.x=1/12*t*(2*o*2*o+2*r*2*r),e.y=1/12*t*(2*n*2*n+2*r*2*r),e.z=1/12*t*(2*o*2*o+2*n*2*n)}getPlaneConstantOfFace(t){const e=this.faces[t],s=this.faceNormals[t],i=this.vertices[e[0]];return-s.dot(i)}clipFaceAgainstHull(t,e,s,i,n,o,r){const c=new l,a=new l,h=new l,u=new l,d=new l,f=new l,p=new l,m=new l,v=this,w=[],y=i,x=w;let g=-1,A=Number.MAX_VALUE;for(let T=0;T<v.faces.length;T++){c.copy(v.faceNormals[T]),s.vmult(c,c);const P=c.dot(t);P<A&&(A=P,g=T)}if(g<0)return;const E=v.faces[g];E.connectedFaces=[];for(let T=0;T<v.faces.length;T++)for(let P=0;P<v.faces[T].length;P++)E.indexOf(v.faces[T][P])!==-1&&T!==g&&E.connectedFaces.indexOf(T)===-1&&E.connectedFaces.push(T);const I=E.length;for(let T=0;T<I;T++){const P=v.vertices[E[T]],V=v.vertices[E[(T+1)%I]];P.vsub(V,a),h.copy(a),s.vmult(h,h),e.vadd(h,h),u.copy(this.faceNormals[g]),s.vmult(u,u),e.vadd(u,u),h.cross(u,d),d.negate(d),f.copy(P),s.vmult(f,f),e.vadd(f,f);const b=E.connectedFaces[T];p.copy(this.faceNormals[b]);const F=this.getPlaneConstantOfFace(b);m.copy(p),s.vmult(m,m);const C=F-m.dot(e);for(this.clipFaceAgainstPlane(y,x,m,C);y.length;)y.shift();for(;x.length;)y.push(x.shift())}p.copy(this.faceNormals[g]);const _=this.getPlaneConstantOfFace(g);m.copy(p),s.vmult(m,m);const q=_-m.dot(e);for(let T=0;T<y.length;T++){let P=m.dot(y[T])+q;if(P<=n&&(console.log(`clamped: depth=${P} to minDist=${n}`),P=n),P<=o){const V=y[T];if(P<=1e-6){const b={point:V,normal:m,depth:P};r.push(b)}}}}clipFaceAgainstPlane(t,e,s,i){let n,o;const r=t.length;if(r<2)return e;let c=t[t.length-1],a=t[0];n=s.dot(c)+i;for(let h=0;h<r;h++){if(a=t[h],o=s.dot(a)+i,n<0)if(o<0){const u=new l;u.copy(a),e.push(u)}else{const u=new l;c.lerp(a,n/(n-o),u),e.push(u)}else if(o<0){const u=new l;c.lerp(a,n/(n-o),u),e.push(u),e.push(a)}c=a,n=o}return e}computeWorldVertices(t,e){for(;this.worldVertices.length<this.vertices.length;)this.worldVertices.push(new l);const s=this.vertices,i=this.worldVertices;for(let n=0;n!==this.vertices.length;n++)e.vmult(s[n],i[n]),t.vadd(i[n],i[n]);this.worldVerticesNeedsUpdate=!1}computeLocalAABB(t,e){const s=this.vertices;t.set(Number.MAX_VALUE,Number.MAX_VALUE,Number.MAX_VALUE),e.set(-Number.MAX_VALUE,-Number.MAX_VALUE,-Number.MAX_VALUE);for(let i=0;i<this.vertices.length;i++){const n=s[i];n.x<t.x?t.x=n.x:n.x>e.x&&(e.x=n.x),n.y<t.y?t.y=n.y:n.y>e.y&&(e.y=n.y),n.z<t.z?t.z=n.z:n.z>e.z&&(e.z=n.z)}}computeWorldFaceNormals(t){const e=this.faceNormals.length;for(;this.worldFaceNormals.length<e;)this.worldFaceNormals.push(new l);const s=this.faceNormals,i=this.worldFaceNormals;for(let n=0;n!==e;n++)t.vmult(s[n],i[n]);this.worldFaceNormalsNeedsUpdate=!1}updateBoundingSphereRadius(){let t=0;const e=this.vertices;for(let s=0;s!==e.length;s++){const i=e[s].lengthSquared();i>t&&(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;f<n.length;f++){d.copy(n[f]),e.vmult(d,d),t.vadd(d,d);const p=d;(o===void 0||p.x<o)&&(o=p.x),(a===void 0||p.x>a)&&(a=p.x),(r===void 0||p.y<r)&&(r=p.y),(h===void 0||p.y>h)&&(h=p.y),(c===void 0||p.z<c)&&(c=p.z),(u===void 0||p.z>u)&&(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;s<e.length;s++)t.vadd(e[s],t);return t.scale(1/e.length,t),t}transformAllPoints(t,e){const s=this.vertices.length,i=this.vertices;if(e){for(let n=0;n<s;n++){const o=i[n];e.vmult(o,o)}for(let n=0;n<this.faceNormals.length;n++){const o=this.faceNormals[n];e.vmult(o,o)}}if(t)for(let n=0;n<s;n++){const o=i[n];o.vadd(t,o)}}pointIsInside(t){const e=this.vertices,s=this.faces,i=this.faceNormals,n=null,o=new l;this.getAveragePointLocal(o);for(let r=0;r<this.faces.length;r++){let c=i[r];const a=e[s[r][0]],h=new l;t.vsub(a,h);const u=c.dot(h),d=new l;o.vsub(a,d);const f=c.dot(d);if(u<0&&f>0||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;f<o;f++){const p=u[f].dot(r);p>c&&(c=p),p<a&&(a=p)}if(a-=d,c-=d,a>c){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;o<n.length;o++)mt.set(n[o][0],n[o][1],n[o][2]),e.vmult(mt,mt),t.vadd(mt,mt),s(mt.x,mt.y,mt.z)}calculateWorldAABB(t,e,s,i){const n=this.halfExtents;st[0].set(n.x,n.y,n.z),st[1].set(-n.x,n.y,n.z),st[2].set(-n.x,-n.y,n.z),st[3].set(-n.x,-n.y,-n.z),st[4].set(n.x,-n.y,-n.z),st[5].set(n.x,n.y,-n.z),st[6].set(-n.x,n.y,-n.z),st[7].set(n.x,-n.y,n.z);const o=st[0];e.vmult(o,o),t.vadd(o,o),i.copy(o),s.copy(o);for(let r=1;r<8;r++){const c=st[r];e.vmult(c,c),t.vadd(c,c);const a=c.x,h=c.y,u=c.z;a>i.x&&(i.x=a),h>i.y&&(i.y=h),u>i.z&&(i.z=u),a<s.x&&(s.x=a),h<s.y&&(s.y=h),u<s.z&&(s.z=u)}}}const mt=new l,st=[new l,new l,new l,new l,new l,new l,new l,new l],se={DYNAMIC:1,STATIC:2,KINEMATIC:4},ie={AWAKE:0,SLEEPY:1,SLEEPING:2};class M extends We{constructor(t){t===void 0&&(t={}),super(),this.id=M.idCounter++,this.index=-1,this.world=null,this.vlambda=new l,this.collisionFilterGroup=typeof t.collisionFilterGroup=="number"?t.collisionFilterGroup:1,this.collisionFilterMask=typeof t.collisionFilterMask=="number"?t.collisionFilterMask:-1,this.collisionResponse=typeof t.collisionResponse=="boolean"?t.collisionResponse:!0,this.position=new l,this.previousPosition=new l,this.interpolatedPosition=new l,this.initPosition=new l,t.position&&(this.position.copy(t.position),this.previousPosition.copy(t.position),this.interpolatedPosition.copy(t.position),this.initPosition.copy(t.position)),this.velocity=new l,t.velocity&&this.velocity.copy(t.velocity),this.initVelocity=new l,this.force=new l;const e=typeof t.mass=="number"?t.mass:0;this.mass=e,this.invMass=e>0?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&&s<i?(this.sleepState=M.SLEEPY,this.timeLastSleepy=t,this.dispatchEvent(M.sleepyEvent)):e===M.SLEEPY&&s>i?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()<o&&(s.push(t),i.push(e))}doBoundingBoxBroadphase(t,e,s,i){t.aabbNeedsUpdate&&t.updateAABB(),e.aabbNeedsUpdate&&e.updateAABB(),t.aabb.overlaps(e.aabb)&&(s.push(t),i.push(e))}makePairsUnique(t,e){const s=ws,i=xs,n=gs,o=t.length;for(let r=0;r!==o;r++)i[r]=t[r],n[r]=e[r];t.length=0,e.length=0;for(let r=0;r!==o;r++){const c=i[r].id,a=n[r].id,h=c<a?`${c},${a}`:`${a},${c}`;s[h]=r,s.keys.push(h)}for(let r=0;r!==s.keys.length;r++){const c=s.keys.pop(),a=s[c];t.push(i[a]),e.push(n[a]),delete s[c]}}setWorld(t){}static boundingSphereCheck(t,e){const s=new l;t.position.vsub(e.position,s);const i=t.shapes[0],n=e.shapes[0];return Math.pow(i.boundingSphereRadius+n.boundingSphereRadius,2)>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<o;r++)this.bins[r]=[],this.binLengths[r]=0}collisionPairs(t,e,s){const i=t.bodies.length,n=t.bodies,o=this.aabbMax,r=this.aabbMin,c=this.nx,a=this.ny,h=this.nz,u=a*h,d=h,f=1,p=o.x,m=o.y,v=o.z,w=r.x,y=r.y,x=r.z,g=c/(p-w),A=a/(m-y),E=h/(v-x),I=(p-w)/c,_=(m-y)/a,q=(v-x)/h,T=Math.sqrt(I*I+_*_+q*q)*.5,P=S.types,V=P.SPHERE,b=P.PLANE;P.BOX,P.COMPOUND,P.CONVEXPOLYHEDRON;const F=this.bins,C=this.binLengths,B=this.bins.length;for(let R=0;R!==B;R++)C[R]=0;const k=Math.ceil;function L(R,W,O,N,Z,K,Q){let U=(R-w)*g|0,$=(W-y)*A|0,rt=(O-x)*E|0,ct=k((N-w)*g),at=k((Z-y)*A),lt=k((K-x)*E);U<0?U=0:U>=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)<T){const Et=ct+lt+Bt;F[Et][C[Et]++]=W}break}default:{W.aabbNeedsUpdate&&W.updateAABB(),L(W.aabb.lowerBound.x,W.aabb.lowerBound.y,W.aabb.lowerBound.z,W.aabb.upperBound.x,W.aabb.upperBound.y,W.aabb.upperBound.z,W);break}}}for(let R=0;R!==B;R++){const W=C[R];if(W>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<t.bodies.length;i++){const n=t.bodies[i];n.aabbNeedsUpdate&&n.updateAABB(),n.aabb.overlaps(e)&&s.push(n)}return s}}class Rt{constructor(){this.rayFromWorld=new l,this.rayToWorld=new l,this.hitNormalWorld=new l,this.hitPointWorld=new l,this.hasHit=!1,this.shape=null,this.body=null,this.hitFaceIndex=-1,this.distance=-1,this.shouldStop=!1}reset(){this.rayFromWorld.setZero(),this.rayToWorld.setZero(),this.hitNormalWorld.setZero(),this.hitPointWorld.setZero(),this.hasHit=!1,this.shape=null,this.body=null,this.hitFaceIndex=-1,this.distance=-1,this.shouldStop=!1}abort(){this.shouldStop=!0}set(t,e,s,i,n,o,r){this.rayFromWorld.copy(t),this.rayToWorld.copy(e),this.hitNormalWorld.copy(s),this.hitPointWorld.copy(i),this.shape=n,this.body=o,this.distance=r}}let Ve,je,ke,Oe,He,Ge,De;const oe={CLOSEST:1,ANY:2,ALL:4};Ve=S.types.SPHERE;je=S.types.PLANE;ke=S.types.BOX;Oe=S.types.CYLINDER;He=S.types.CONVEXPOLYHEDRON;Ge=S.types.HEIGHTFIELD;De=S.types.TRIMESH;class Y{get[Ve](){return this._intersectSphere}get[je](){return this._intersectPlane}get[ke](){return this._intersectBox}get[Oe](){return this._intersectConvex}get[He](){return this._intersectConvex}get[Ge](){return this._intersectHeightfield}get[De](){return this._intersectTrimesh}constructor(t,e){t===void 0&&(t=new l),e===void 0&&(e=new l),this.from=t.clone(),this.to=e.clone(),this.direction=new l,this.precision=1e-4,this.checkCollisionResponse=!0,this.skipBackfaces=!1,this.collisionFilterMask=-1,this.collisionFilterGroup=-1,this.mode=Y.ANY,this.result=new Rt,this.hasHit=!1,this.callback=s=>{}}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;o<r;o++){const c=t.shapes[o];if(!(s&&!c.collisionResponse)&&(t.quaternion.mult(t.shapeOrientations[o],n),t.quaternion.vmult(t.shapeOffsets[o],i),i.vadd(t.position,i),this.intersectShape(c,n,i,t),this.result.shouldStop))break}}intersectBodies(t,e){e&&(this.result=e,this.updateDirection());for(let s=0,i=t.length;!this.result.shouldStop&&s<i;s++)this.intersectBody(t[s])}updateDirection(){this.to.vsub(this.from,this.direction),this.direction.normalize()}intersectShape(t,e,s,i){const n=this.from;if(js(n,this.direction,s)>t.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)<u)return;const f=a.dot(c);if(Math.abs(f)<this.precision)return;const p=new l,m=new l,v=new l;o.vsub(s,p);const w=-a.dot(p)/f;c.scale(w,m),o.vadd(m,v),this.reportIntersection(a,v,n,i,-1)}getAABB(t){const{lowerBound:e,upperBound:s}=t,i=this.to,n=this.from;e.x=Math.min(i.x,n.x),e.y=Math.min(i.y,n.y),e.z=Math.min(i.z,n.z),s.x=Math.max(i.x,n.x),s.y=Math.max(i.y,n.y),s.z=Math.max(i.z,n.z)}_intersectHeightfield(t,e,s,i,n){t.data,t.elementSize;const o=zs;o.from.copy(this.from),o.to.copy(this.to),j.pointToLocalFrame(s,e,o.from,o.from),j.pointToLocalFrame(s,e,o.to,o.to),o.updateDirection();const r=Cs;let c,a,h,u;c=a=0,h=u=t.data.length-1;const d=new G;o.getAABB(d),t.getIndexOfPosition(d.lowerBound.x,d.lowerBound.y,r,!0),c=Math.max(c,r[0]),a=Math.max(a,r[1]),t.getIndexOfPosition(d.upperBound.x,d.upperBound.y,r,!0),h=Math.min(h,r[0]+1),u=Math.min(u,r[1]+1);for(let f=c;f<h;f++)for(let p=a;p<u;p++){if(this.result.shouldStop)return;if(t.getAabbAtIndex(f,p,d),!!d.overlapsRay(o)){if(t.getConvexTrianglePillar(f,p,!1),j.pointToWorldFrame(s,e,t.pillarOffset,qt),this._intersectConvex(t.pillarConvex,e,qt,i,n,fe),this.result.shouldStop)return;t.getConvexTrianglePillar(f,p,!0),j.pointToWorldFrame(s,e,t.pillarOffset,qt),this._intersectConvex(t.pillarConvex,e,qt,i,n,fe)}}}_intersectSphere(t,e,s,i,n){const o=this.from,r=this.to,c=t.radius,a=(r.x-o.x)**2+(r.y-o.y)**2+(r.z-o.z)**2,h=2*((r.x-o.x)*(o.x-s.x)+(r.y-o.y)*(o.y-s.y)+(r.z-o.z)*(o.z-s.z)),u=(o.x-s.x)**2+(o.y-s.y)**2+(o.z-s.z)**2-c**2,d=h**2-4*a*u,f=Ss,p=Fs;if(!(d<0))if(d===0)o.lerp(r,d,f),f.vsub(s,p),p.normalize(),this.reportIntersection(p,f,n,i,-1);else{const m=(-h-Math.sqrt(d))/(2*a),v=(-h+Math.sqrt(d))/(2*a);if(m>=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&&x<w;x++){const g=a?a[x]:x,A=h[g],E=d[g],I=e,_=s;c.copy(u[A[0]]),I.vmult(c,c),c.vadd(_,c),c.vsub(p,c),I.vmult(E,r);const q=f.dot(r);if(Math.abs(q)<this.precision)continue;const T=r.dot(c)/q;if(!(T<0)){f.scale(T,J),J.vadd(p,J),tt.copy(u[A[0]]),I.vmult(tt,tt),_.vadd(tt,tt);for(let P=1;!y.shouldStop&&P<A.length-1;P++){it.copy(u[A[P]]),nt.copy(u[A[P+1]]),I.vmult(it,it),I.vmult(nt,nt),_.vadd(it,it),_.vadd(nt,nt);const V=J.distanceTo(p);!(Y.pointInTriangle(J,tt,it,nt)||Y.pointInTriangle(J,it,tt,nt))||V>v||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<a.distance||!a.hasHit)&&(this.hasHit=!0,a.hasHit=!0,a.set(o,r,t,e,s,i,c));break;case Y.ANY:this.hasHit=!0,a.hasHit=!0,a.set(o,r,t,e,s,i,c),a.shouldStop=!0;break}}static pointInTriangle(t,e,s,i){i.vsub(e,bt),s.vsub(e,Ft),t.vsub(e,Qt);const n=bt.dot(bt),o=bt.dot(Ft),r=bt.dot(Qt),c=Ft.dot(Ft),a=Ft.dot(Qt);let h,u;return(h=c*r-o*a)>=0&&(u=n*a-o*r)>=0&&h+u<n*c-o*o}}Y.CLOSEST=oe.CLOSEST;Y.ANY=oe.ANY;Y.ALL=oe.ALL;const pe=new G,Kt=[],Ft=new l,Qt=new l,Bs=new l,Es=new D,J=new l,tt=new l,it=new l,nt=new l;new l;new Rt;const fe={faceList:[0]},qt=new l,zs=new Y,Cs=[],Ss=new l,Fs=new l,Is=new l,Ms=new l,Ns=new l,me=new l,_s=new l,Ts=new l,Rs=new l,Ps=new l,qs=new l,Ls=new l;new G;const Ws=[],Vs=new j,bt=new l,Lt=new l;function js(z,t,e){e.vsub(z,bt);const s=bt.dot(t);return t.scale(s,Lt),Lt.vadd(z,Lt),e.distanceTo(Lt)}class _t extends ne{static checkBounds(t,e,s){let i,n;s===0?(i=t.position.x,n=e.position.x):s===1?(i=t.position.y,n=e.position.y):s===2&&(i=t.position.z,n=e.position.z);const o=t.boundingRadius,r=e.boundingRadius,c=i+o;return n-r<c}static insertionSortX(t){for(let e=1,s=t.length;e<s;e++){const i=t[e];let n;for(n=e-1;n>=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<s;e++){const i=t[e];let n;for(n=e-1;n>=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<s;e++){const i=t[e];let n;for(n=e-1;n>=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;e<t.bodies.length;e++)this.axisList.push(t.bodies[e]);t.removeEventListener("addBody",this._addBodyHandler),t.removeEventListener("removeBody",this._removeBodyHandler),t.addEventListener("addBody",this._addBodyHandler),t.addEventListener("removeBody",this._removeBodyHandler),this.world=t,this.dirty=!0}collisionPairs(t,e,s){const i=this.axisList,n=i.length,o=this.axisIndex;let r,c;for(this.dirty&&(this.sortList(),this.dirty=!1),r=0;r!==n;r++){const a=i[r];for(c=r+1;c<n;c++){const h=i[c];if(this.needBroadphaseCollision(a,h)){if(!_t.checkBounds(a,h,o))break;this.intersectionTest(a,h,e,s)}}}}sortList(){const t=this.axisList,e=this.axisIndex,s=t.length;for(let i=0;i!==s;i++){const n=t[i];n.aabbNeedsUpdate&&n.updateAABB()}e===0?_t.insertionSortX(t):e===1?_t.insertionSortY(t):e===2&&_t.insertionSortZ(t)}autoDetectAxis(){let t=0,e=0,s=0,i=0,n=0,o=0;const r=this.axisList,c=r.length,a=1/c;for(let f=0;f!==c;f++){const p=r[f],m=p.position.x;t+=m,e+=m*m;const v=p.position.y;s+=v,i+=v*v;const w=p.position.z;n+=w,o+=w*w}const h=e-t*t*a,u=i-s*s*a,d=o-n*n*a;h>u?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<o.length;r++){const c=o[r];c.aabbNeedsUpdate&&c.updateAABB(),c.aabb.overlaps(e)&&s.push(c)}return s}}class Ut{static defaults(t,e){t===void 0&&(t={});for(let s in e)s in t||(t[s]=e[s]);return t}}class Pt{constructor(t,e,s){s===void 0&&(s={}),s=Ut.defaults(s,{collideConnected:!0,wakeUpBodies:!0}),this.equations=[],this.bodyA=t,this.bodyB=e,this.id=Pt.idCounter++,this.collideConnected=s.collideConnected,s.wakeUpBodies&&(t&&t.wakeUp(),e&&e.wakeUp())}update(){throw new Error("method update() not implmemented in this Constraint subclass!")}enable(){const t=this.equations;for(let e=0;e<t.length;e++)t[e].enabled=!0}disable(){const t=this.equations;for(let e=0;e<t.length;e++)t[e].enabled=!1}}Pt.idCounter=0;class ve{constructor(){this.spatial=new l,this.rotational=new l}multiplyElement(t){return t.spatial.dot(this.spatial)+t.rotational.dot(this.rotational)}multiplyVectors(t,e){return t.dot(this.spatial)+e.dot(this.rotational)}}class yt{constructor(t,e,s,i){s===void 0&&(s=-1e6),i===void 0&&(i=1e6),this.id=yt.idCounter++,this.minForce=s,this.maxForce=i,this.bi=t,this.bj=e,this.a=0,this.b=0,this.eps=0,this.jacobianElementA=new ve,this.jacobianElementB=new ve,this.enabled=!0,this.multiplier=0,this.setSpookParams(1e7,4,1/60)}setSpookParams(t,e,s){const i=e,n=t,o=s;this.a=4/(o*(1+4*i)),this.b=4*i/(1+4*i),this.eps=4/(o*o*n*(1+4*i))}computeB(t,e,s){const i=this.computeGW(),n=this.computeGq(),o=this.computeGiMf();return-n*t-i*e-o*s}computeGq(){const t=this.jacobianElementA,e=this.jacobianElementB,s=this.bi,i=this.bj,n=s.position,o=i.position;return t.spatial.dot(n)+e.spatial.dot(o)}computeGW(){const t=this.jacobianElementA,e=this.jacobianElementB,s=this.bi,i=this.bj,n=s.velocity,o=i.velocity,r=s.angularVelocity,c=i.angularVelocity;return t.multiplyVectors(n,r)+e.multiplyVectors(o,c)}computeGWlambda(){const t=this.jacobianElementA,e=this.jacobianElementB,s=this.bi,i=this.bj,n=s.vlambda,o=i.vlambda,r=s.wlambda,c=i.wlambda;return t.multiplyVectors(n,r)+e.multiplyVectors(o,c)}computeGiMf(){const t=this.jacobianElementA,e=this.jacobianElementB,s=this.bi,i=this.bj,n=s.force,o=s.torque,r=i.force,c=i.torque,a=s.invMassSolve,h=i.invMassSolve;return n.scale(a,ye),r.scale(h,we),s.invInertiaWorldSolve.vmult(o,xe),i.invInertiaWorldSolve.vmult(c,ge),t.multiplyVectors(ye,xe)+e.multiplyVectors(we,ge)}computeGiMGt(){const t=this.jacobianElementA,e=this.jacobianElementB,s=this.bi,i=this.bj,n=s.invMassSolve,o=i.invMassSolve,r=s.invInertiaWorldSolve,c=i.invInertiaWorldSolve;let a=n+o;return r.vmult(t.rotational,Wt),a+=Wt.dot(t.rotational),c.vmult(e.rotational,Wt),a+=Wt.dot(e.rotational),a}addToWlambda(t){const e=this.jacobianElementA,s=this.jacobianElementB,i=this.bi,n=this.bj,o=ks;i.vlambda.addScaledVector(i.invMassSolve*t,e.spatial,i.vlambda),n.vlambda.addScaledVector(n.invMassSolve*t,s.spatial,n.vlambda),i.invInertiaWorldSolve.vmult(e.rotational,o),i.wlambda.addScaledVector(t,o,i.wlambda),n.invInertiaWorldSolve.vmult(s.rotational,o),n.wlambda.addScaledVector(t,o,n.wlambda)}computeC(){return this.computeGiMGt()+this.eps}}yt.idCounter=0;const ye=new l,we=new l,xe=new l,ge=new l,Wt=new l,ks=new l;class Tt extends yt{constructor(t,e,s){s===void 0&&(s=1e6),super(t,e,0,s),this.restitution=0,this.ri=new l,this.rj=new l,this.ni=new l}computeB(t){const e=this.a,s=this.b,i=this.bi,n=this.bj,o=this.ri,r=this.rj,c=Os,a=Hs,h=i.velocity,u=i.angularVelocity;i.force,i.torque;const d=n.velocity,f=n.angularVelocity;n.force,n.torque;const p=Gs,m=this.jacobianElementA,v=this.jacobianElementB,w=this.ni;o.cross(w,c),r.cross(w,a),w.negate(m.spatial),c.negate(m.rotational),v.spatial.copy(w),v.rotational.copy(a),p.copy(n.position),p.vadd(r,p),p.vsub(i.position,p),p.vsub(o,p);const y=w.dot(p),x=this.restitution+1,g=x*d.dot(w)-x*h.dot(w)+f.dot(a)-u.dot(c),A=this.computeGiMf();return-y*e-g*s-t*A}getImpactVelocityAlongNormal(){const t=Ds,e=Us,s=Ys,i=Xs,n=$s;return this.bi.position.vadd(this.ri,s),this.bj.position.vadd(this.rj,i),this.bi.getVelocityAtWorldPoint(s,t),this.bj.getVelocityAtWorldPoint(i,e),t.vsub(e,n),this.ni.dot(n)}}const Os=new l,Hs=new l,Gs=new l,Ds=new l,Us=new l,Ys=new l,Xs=new l,$s=new l;class re extends Pt{constructor(t,e,s,i,n){e===void 0&&(e=new l),i===void 0&&(i=new l),n===void 0&&(n=1e6),super(t,s),this.pivotA=e.clone(),this.pivotB=i.clone();const o=this.equationX=new Tt(t,s),r=this.equationY=new Tt(t,s),c=this.equationZ=new Tt(t,s);this.equations.push(o,r,c),o.minForce=r.minForce=c.minForce=-n,o.maxForce=r.maxForce=c.maxForce=n,o.ni.set(1,0,0),r.ni.set(0,1,0),c.ni.set(0,0,1)}update(){const t=this.bodyA,e=this.bodyB,s=this.equationX,i=this.equationY,n=this.equationZ;t.quaternion.vmult(this.pivotA,s.ri),e.quaternion.vmult(this.pivotB,s.rj),i.ri.copy(s.ri),i.rj.copy(s.rj),n.ri.copy(s.ri),n.rj.copy(s.rj)}}class Zs extends yt{constructor(t,e,s){s===void 0&&(s={});const i=typeof s.maxForce<"u"?s.maxForce:1e6;super(t,e,-i,i),this.axisA=s.axisA?s.axisA.clone():new l(1,0,0),this.axisB=s.axisB?s.axisB.clone():new l(0,1,0),this.angle=typeof s.angle<"u"?s.angle:0}computeB(t){const e=this.a,s=this.b,i=this.axisA,n=this.axisB,o=Ks,r=Qs,c=this.jacobianElementA,a=this.jacobianElementB;i.cross(n,o),n.cross(i,r),c.rotational.copy(r),a.rotational.copy(o);const h=Math.cos(this.angle)-i.dot(n),u=this.computeGW(),d=this.computeGiMf();return-h*e-u*s-t*d}}const Ks=new l,Qs=new l;class St extends yt{constructor(t,e,s){s===void 0&&(s={});const i=typeof s.maxForce<"u"?s.maxForce:1e6;super(t,e,-i,i),this.axisA=s.axisA?s.axisA.clone():new l(1,0,0),this.axisB=s.axisB?s.axisB.clone():new l(0,1,0),this.maxAngle=Math.PI/2}computeB(t){const e=this.a,s=this.b,i=this.axisA,n=this.axisB,o=Js,r=ti,c=this.jacobianElementA,a=this.jacobianElementB;i.cross(n,o),n.cross(i,r),c.rotational.copy(r),a.rotational.copy(o);const h=Math.cos(this.maxAngle)-i.dot(n),u=this.computeGW(),d=this.computeGiMf();return-h*e-u*s-t*d}}const Js=new l,ti=new l;class Uo 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,this.axisB=s.axisB?s.axisB.clone():new l,this.collideConnected=!!s.collideConnected,this.angle=typeof s.angle<"u"?s.angle:0;const r=this.coneEquation=new Zs(t,e,s),c=this.twistEquation=new St(t,e,s);this.twistAngle=typeof s.twistAngle<"u"?s.twistAngle:0,r.maxForce=0,r.minForce=-i,c.maxForce=0,c.minForce=-i,this.equations.push(r,c)}update(){const t=this.bodyA,e=this.bodyB,s=this.coneEquation,i=this.twistEquation;super.update(),t.vectorToWorldFrame(this.axisA,s.axisA),e.vectorToWorldFrame(this.axisB,s.axisB),this.axisA.tangents(i.axisA,i.axisA),t.vectorToWorldFrame(i.axisA,i.axisA),this.axisB.tangents(i.axisB,i.axisB),e.vectorToWorldFrame(i.axisB,i.axisB),s.angle=this.angle,i.maxAngle=this.twistAngle}}new l;new l;class Yo extends Pt{constructor(t,e,s,i){i===void 0&&(i=1e6),super(t,e),typeof s>"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;u<s;u++)this.updateWheelTransform(u);this.currentVehicleSpeedKmHour=3.6*i.velocity.length();const n=new l;this.getVehicleAxisWorld(this.indexForwardAxis,n),n.dot(i.velocity)<0&&(this.currentVehicleSpeedKmHour*=-1);for(let u=0;u<s;u++)this.castRay(e[u]);this.updateSuspension(t);const o=new l,r=new l;for(let u=0;u<s;u++){const d=e[u];let f=d.suspensionForce;f>d.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;u<s;u++){const d=e[u];i.getVelocityAtWorldPoint(d.chassisConnectionPointWorld,h);let f=1;switch(this.indexUpAxis){case 1:f=-1;break}if(d.isInContact){this.getVehicleAxisWorld(this.indexForwardAxis,a);const p=a.dot(d.raycastResult.hitNormalWorld);d.raycastResult.hitNormalWorld.scale(p,c),a.vsub(c,a);const m=a.dot(h);d.deltaRotation=f*m*t/d.radius}(d.sliding||!d.isInContact)&&d.engineForce!==0&&d.useCustomSlidingRotationalSpeed&&(d.deltaRotation=(d.engineForce>0?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;o<n;o++){const r=i[o];if(r.isInContact){let c;const a=r.suspensionRestLength,h=r.suspensionLength,u=a-h;c=r.suspensionStiffness*u*r.clippedInvContactDotSuspension;const d=r.suspensionRelativeVelocity;let f;d<0?f=r.dampingCompression:f=r.dampingRelaxation,c-=f*d,r.suspensionForce=c*s,r.suspensionForce<0&&(r.suspensionForce=0)}else r.suspensionForce=0}}removeFromWorld(t){this.constraints,t.removeBody(this.chassisBody),t.removeEventListener("preStep",this.preStepCallback),this.world=null}castRay(t){const e=Ei,s=zi;this.updateWheelTransformWorld(t);const i=this.chassisBody;let n=-1;const o=t.suspensionRestLength+t.radius;t.directionWorld.scale(o,e);const r=t.chassisConnectionPointWorld;r.vadd(e,s);const c=t.raycastResult;c.reset();const a=i.collisionResponse;i.collisionResponse=!1,this.world.rayTest(r,s,c),i.collisionResponse=a;const h=c.body;if(t.raycastResult.groundObject=0,h){n=c.distance,t.raycastResult.hitNormalWorld=c.hitNormalWorld,t.isInContact=!0;const u=c.distance;t.suspensionLength=u-t.radius;const d=t.suspensionRestLength-t.maxSuspensionTravel,f=t.suspensionRestLength+t.maxSuspensionTravel;t.suspensionLength<d&&(t.suspensionLength=d),t.suspensionLength>f&&(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;h<i;h++){const u=s[h];u.raycastResult.body&&this.numWheelsOnGround++,u.sideImpulse=0,u.forwardImpulse=0,o[h]||(o[h]=new l),r[h]||(r[h]=new l)}for(let h=0;h<i;h++){const u=s[h],d=u.raycastResult.body;if(d){const f=r[h];this.getWheelTransformWorld(h).vectorToWorldFrame(Ci[this.indexRightAxis],f);const m=u.raycastResult.hitNormalWorld,v=f.dot(m);m.scale(v,e),f.vsub(e,f),f.normalize(),m.cross(f,o[h]),o[h].normalize(),u.sideImpulse=Oi(n,u.raycastResult.hitPointWorld,d,u.raycastResult.hitPointWorld,f),u.sideImpulse*=Mi}}const c=1,a=.5;this.sliding=!1;for(let h=0;h<i;h++){const u=s[h],d=u.raycastResult.body;let f=0;if(u.slipInfo=1,d){const m=u.brake?u.brake:0;f=Ri(n,d,u.raycastResult.hitPointWorld,o[h],m),f+=u.engineForce*t;const v=m/f;u.slipInfo*=v}if(u.forwardImpulse=0,u.skidInfo=1,d){u.skidInfo=1;const p=u.suspensionForce*t*u.frictionSlip,v=p*p;u.forwardImpulse=f;const w=u.forwardImpulse*a/u.forwardAcceleration,y=u.sideImpulse*c/u.sideAcceleration,x=w*w+y*y;if(u.sliding=!1,x>v){this.sliding=!0,u.sliding=!0;const g=p/Math.sqrt(x);u.skidInfo*=g}}}if(this.sliding)for(let h=0;h<i;h++){const u=s[h];u.sideImpulse!==0&&u.skidInfo<1&&(u.forwardImpulse*=u.skidInfo,u.sideImpulse*=u.skidInfo)}for(let h=0;h<i;h++){const u=s[h],d=new l;if(u.raycastResult.hitPointWorld.vsub(n.position,d),u.forwardImpulse!==0){const f=new l;o[h].scale(u.forwardImpulse,f),n.applyImpulse(f,d)}if(u.sideImpulse!==0){const f=u.raycastResult.body,p=new l;u.raycastResult.hitPointWorld.vsub(f.position,p);const m=new l;r[h].scale(u.sideImpulse,m),n.vectorToLocalFrame(d,d),d["xyz"[this.indexUpAxis]]*=u.rollInfluence,n.vectorToWorldFrame(d,d),n.applyImpulse(m,d),m.scale(-1,m),f.applyImpulse(m,p)}}}}new l;new l;new l;const bi=new l,Ai=new l,Bi=new l;new Y;new l;const Ei=new l,zi=new l,Ci=[new l(1,0,0),new l(0,1,0),new l(0,0,1)],Si=new l,Fi=[],Ii=[],Mi=1,Ni=new l,_i=new l,Ti=new l;function Ri(z,t,e,s,i){let n=0;const o=e,r=Ni,c=_i,a=Ti;z.getVelocityAtWorldPoint(o,r),t.getVelocityAtWorldPoint(o,c),r.vsub(c,a);const h=s.dot(a),u=Ee(z,e,s),d=Ee(t,e,s),p=1/(u+d);return n=-h*p,i<n&&(n=i),n<-i&&(n=-i),n}const Pi=new l,qi=new l,Li=new l,Wi=new l;function Ee(z,t,e){const s=Pi,i=qi,n=Li,o=Wi;return t.vsub(z.position,s),s.cross(e,i),z.invInertiaWorld.vmult(i,o),o.cross(s,n),z.invMass+e.dot(n)}const Vi=new l,ji=new l,ki=new l;function Oi(z,t,e,s,i){if(i.lengthSquared()>1.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;r<o.length;r++){const c=o[r];s[c]=t[c]-n,i[c]=t[c]+n}}}class Ko{constructor(t){t===void 0&&(t={}),this.wheelBodies=[],this.coordinateSystem=typeof t.coordinateSystem<"u"?t.coordinateSystem.clone():new l(1,2,3),t.chassisBody?this.chassisBody=t.chassisBody:this.chassisBody=new M({mass:1,shape:new Dt(new l(5,.5,2))}),this.constraints=[],this.wheelAxes=[],this.wheelForces=[]}addWheel(t){t===void 0&&(t={});let e;t.body?e=t.body:e=new M({mass:1,shape:new Hi(1.2)}),this.wheelBodies.push(e),this.wheelForces.push(0);const s=typeof t.position<"u"?t.position.clone():new l,i=new l;this.chassisBody.pointToWorldFrame(s,i),e.position.set(i.x,i.y,i.z);const n=typeof t.axis<"u"?t.axis.clone():new l(0,0,1);this.wheelAxes.push(n);const o=new ni(this.chassisBody,e,{pivotA:s,axisA:n,pivotB:l.ZERO,axisB:n,collideConnected:!1});return this.constraints.push(o),this.wheelBodies.length-1}setSteeringValue(t,e){const s=this.wheelAxes[e],i=Math.cos(t),n=Math.sin(t),o=s.x,r=s.z;this.constraints[e].axisA.set(-i*o+n*r,0,n*o+i*r)}setMotorSpeed(t,e){const s=this.constraints[e];s.enableMotor(),s.motorTargetVelocity=t}disableMotor(t){this.constraints[t].disableMotor()}setWheelForce(t,e){this.wheelForces[e]=t}applyWheelForce(t,e){const s=this.wheelAxes[e],i=this.wheelBodies[e],n=i.torque;s.scale(t,Vt),i.vectorToWorldFrame(Vt,Vt),n.vadd(Vt,n)}addToWorld(t){const e=this.constraints,s=this.wheelBodies.concat([this.chassisBody]);for(let i=0;i<s.length;i++)t.addBody(s[i]);for(let i=0;i<e.length;i++)t.addConstraint(e[i]);t.addEventListener("preStep",this._update.bind(this))}_update(){const t=this.wheelForces;for(let e=0;e<t.length;e++)this.applyWheelForce(t[e],e)}removeFromWorld(t){const e=this.constraints,s=this.wheelBodies.concat([this.chassisBody]);for(let i=0;i<s.length;i++)t.removeBody(s[i]);for(let i=0;i<e.length;i++)t.removeConstraint(e[i])}getWheelSpeed(t){const e=this.wheelAxes[t],i=this.wheelBodies[t].angularVelocity;return this.chassisBody.vectorToWorldFrame(e,ze),i.dot(ze)}}const Vt=new l,ze=new l;class Qo{constructor(){this.particles=[],this.density=1,this.smoothingRadius=1,this.speedOfSound=1,this.viscosity=.01,this.eps=1e-6,this.pressures=[],this.densities=[],this.neighbors=[]}add(t){this.particles.push(t),this.neighbors.length<this.particles.length&&this.neighbors.push([])}remove(t){const e=this.particles.indexOf(t);e!==-1&&(this.particles.splice(e,1),this.neighbors.length>this.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()<n&&e.push(c)}}update(){const t=this.particles.length,e=Di,s=this.speedOfSound,i=this.eps;for(let h=0;h!==t;h++){const u=this.particles[h],d=this.neighbors[h];d.length=0,this.getNeighbors(u,d),d.push(this.particles[h]);const f=d.length;let p=0;for(let m=0;m!==f;m++){u.position.vsub(d[m].position,e);const v=e.length(),w=this.w(v);p+=d[m].mass*w}this.densities[h]=p,this.pressures[h]=s*s*(this.densities[h]-this.density)}const n=Ui,o=Yi,r=Xi,c=$i,a=Zi;for(let h=0;h!==t;h++){const u=this.particles[h];n.set(0,0,0),o.set(0,0,0);let d,f;const p=this.neighbors[h],m=p.length;for(let v=0;v!==m;v++){const w=p[v];u.position.vsub(w.position,c);const y=c.length();d=-w.mass*(this.pressures[h]/(this.densities[h]*this.densities[h]+i)+this.pressures[v]/(this.densities[v]*this.densities[v]+i)),this.gradw(c,r),r.scale(d,r),n.vadd(r,n),w.velocity.vsub(u.velocity,a),a.scale(1/(1e-4+this.densities[h]*this.densities[v])*this.viscosity*w.mass,a),f=this.nablaw(y),a.scale(f,a),o.vadd(a,o)}o.scale(u.mass,o),n.scale(u.mass,n),u.force.vadd(o,u.force),u.force.vadd(n,u.force)}}w(t){const e=this.smoothingRadius;return 315/(64*Math.PI*e**9)*(e*e-t*t)**3}gradw(t,e){const s=t.length(),i=this.smoothingRadius;t.scale(945/(32*Math.PI*i**9)*(i*i-s*s)**2,e)}nablaw(t){const e=this.smoothingRadius;return 945/(32*Math.PI*e**9)*(e*e-t*t)*(7*t*t-3*e*e)}}const Gi=new l,Di=new l,Ui=new l,Yi=new l,Xi=new l,$i=new l,Zi=new l;class Jo extends vt{constructor(t,e,s,i){if(t===void 0&&(t=1),e===void 0&&(e=1),s===void 0&&(s=1),i===void 0&&(i=8),t<0)throw new Error("The cylinder radiusTop cannot be negative.");if(e<0)throw new Error("The cylinder radiusBottom cannot be negative.");const n=i,o=[],r=[],c=[],a=[],h=[],u=Math.cos,d=Math.sin;o.push(new l(-e*d(0),-s*.5,e*u(0))),a.push(0),o.push(new l(-t*d(0),s*.5,t*u(0))),h.push(1);for(let p=0;p<n;p++){const m=2*Math.PI/n*(p+1),v=2*Math.PI/n*(p+.5);p<n-1?(o.push(new l(-e*d(m),-s*.5,e*u(m))),a.push(2*p+2),o.push(new l(-t*d(m),s*.5,t*u(m))),h.push(2*p+3),c.push([2*p,2*p+1,2*p+3,2*p+2])):c.push([2*p,2*p+1,1,0]),(n%2===1||p<n/2)&&r.push(new l(-d(v),0,u(v)))}c.push(a),r.push(new l(0,1,0));const f=[];for(let p=0;p<h.length;p++)f.push(h[h.length-p-1]);c.push(f),super({vertices:o,faces:c,axes:r}),this.type=S.types.CYLINDER,this.radiusTop=t,this.radiusBottom=e,this.height=s,this.numSegments=i}}class tr extends S{constructor(){super({type:S.types.PARTICLE})}calculateLocalInertia(t,e){return e===void 0&&(e=new l),e.set(0,0,0),e}volume(){return 0}updateBoundingSphereRadius(){this.boundingSphereRadius=0}calculateWorldAABB(t,e,s,i){s.copy(t),i.copy(t)}}class er extends S{constructor(){super({type:S.types.PLANE}),this.worldNormal=new l,this.worldNormalNeedsUpdate=!0,this.boundingSphereRadius=Number.MAX_VALUE}computeWorldNormal(t){const e=this.worldNormal;e.set(0,0,1),t.vmult(e,e),this.worldNormalNeedsUpdate=!1}calculateLocalInertia(t,e){return e===void 0&&(e=new l),e}volume(){return Number.MAX_VALUE}calculateWorldAABB(t,e,s,i){ht.set(0,0,1),e.vmult(ht,ht);const n=Number.MAX_VALUE;s.set(-n,-n,-n),i.set(n,n,n),ht.x===1?i.x=t.x:ht.x===-1&&(s.x=t.x),ht.y===1?i.y=t.y:ht.y===-1&&(s.y=t.y),ht.z===1?i.z=t.z:ht.z===-1&&(s.z=t.z)}updateBoundingSphereRadius(){this.boundingSphereRadius=Number.MAX_VALUE}}const ht=new l;class sr extends S{constructor(t,e){e===void 0&&(e={}),e=Ut.defaults(e,{maxValue:null,minValue:null,elementSize:1}),super({type:S.types.HEIGHTFIELD}),this.data=t,this.maxValue=e.maxValue,this.minValue=e.minValue,this.elementSize=e.elementSize,e.minValue===null&&this.updateMinValue(),e.maxValue===null&&this.updateMaxValue(),this.cacheEnabled=!0,this.pillarConvex=new vt,this.pillarOffset=new l,this.updateBoundingSphereRadius(),this._cachedPillars={}}update(){this._cachedPillars={}}updateMinValue(){const t=this.data;let e=t[0][0];for(let s=0;s!==t.length;s++)for(let i=0;i!==t[s].length;i++){const n=t[s][i];n<e&&(e=n)}this.minValue=e}updateMaxValue(){const t=this.data;let e=t[0][0];for(let s=0;s!==t.length;s++)for(let i=0;i!==t[s].length;i++){const n=t[s][i];n>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<c.height;h++){const u=[];for(let d=0;d<c.width;d++){const f=c.data[(h*c.height+d)*4],p=c.data[(h*c.height+d)*4+1],m=c.data[(h*c.height+d)*4+2],v=(f+p+m)/4/255*i;s<0?u.push(v):u.unshift(v)}n<0?a.unshift(u):a.push(u)}this.updateMaxValue(),this.updateMinValue(),this.update()}}const Ce=[],Se=new l,Ki=new l,Qi=new l,Ji=new l,tn=new l,en=new l,sn=new l,nn=new l,on=new l;function rn(z,t,e,s,i,n,o,r,c){c.x=((n-r)*(z-o)+(o-i)*(t-r))/((n-r)*(e-o)+(o-i)*(s-r)),c.y=((r-s)*(z-o)+(e-o)*(t-r))/((n-r)*(e-o)+(o-i)*(s-r)),c.z=1-c.x-c.y}class ot{constructor(t){t===void 0&&(t={}),this.root=t.root||null,this.aabb=t.aabb?t.aabb.clone():new G,this.data=[],this.children=[]}reset(){this.children.length=this.data.length=0}insert(t,e,s){s===void 0&&(s=0);const i=this.data;if(!this.aabb.contains(t))return!1;const n=this.children,o=this.maxDepth||this.root.maxDepth;if(s<o){let r=!1;n.length||(this.subdivide(),r=!0);for(let c=0;c!==8;c++)if(n[c].insert(t,e,s+1))return!0;r&&(n.length=0)}return i.push(e),!0}subdivide(){const t=this.aabb,e=t.lowerBound,s=t.upperBound,i=this.children;i.push(new ot({aabb:new G({lowerBound:new l(0,0,0)})}),new ot({aabb:new G({lowerBound:new l(1,0,0)})}),new ot({aabb:new G({lowerBound:new l(1,1,0)})}),new ot({aabb:new G({lowerBound:new l(1,1,1)})}),new ot({aabb:new G({lowerBound:new l(0,1,1)})}),new ot({aabb:new G({lowerBound:new l(0,0,1)})}),new ot({aabb:new G({lowerBound:new l(1,0,1)})}),new ot({aabb:new G({lowerBound:new l(0,1,0)})})),s.vsub(e,wt),wt.scale(.5,wt);const n=this.root||this;for(let o=0;o!==8;o++){const r=i[o];r.root=n;const c=r.aabb.lowerBound;c.x*=wt.x,c.y*=wt.y,c.z*=wt.z,c.vadd(e,c),c.vadd(wt,r.aabb.upperBound)}}aabbQuery(t,e){this.data,this.children;const s=[this];for(;s.length;){const i=s.pop();i.aabb.overlaps(t)&&Array.prototype.push.apply(e,i.data),Array.prototype.push.apply(s,i.children)}return e}rayQuery(t,e,s){return t.getAABB(jt),jt.toLocalFrame(e,jt),this.aabbQuery(jt,s),s}removeEmptyNodes(){for(let t=this.children.length-1;t>=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<this.indices.length/3;c++){const a=c*3;this._getUnscaledVertex(this.indices[a],i),this._getUnscaledVertex(this.indices[a+1],n),this._getUnscaledVertex(this.indices[a+2],o),s.setFromPoints(r),t.insert(s,c)}t.removeEmptyNodes()}getTrianglesInAABB(t,e){kt.copy(t);const s=this.scale,i=s.x,n=s.y,o=s.z,r=kt.lowerBound,c=kt.upperBound;return r.x/=i,r.y/=n,r.z/=o,c.x/=i,c.y/=n,c.z/=o,this.tree.aabbQuery(kt,e)}setScale(t){const e=this.scale.x===this.scale.y&&this.scale.y===this.scale.z,s=t.x===t.y&&t.y===t.z;e&&s||this.updateNormals(),this.scale.copy(t),this.updateAABB(),this.updateBoundingSphereRadius()}updateNormals(){const t=an,e=this.normals;for(let s=0;s<this.indices.length/3;s++){const i=s*3,n=this.indices[i],o=this.indices[i+1],r=this.indices[i+2];this.getVertex(n,Me),this.getVertex(o,Ne),this.getVertex(r,_e),ee.computeNormal(Ne,Me,_e,t),e[i]=t.x,e[i+1]=t.y,e[i+2]=t.z}}updateEdges(){const t={},e=(i,n)=>{const o=i<n?`${i}_${n}`:`${n}_${i}`;t[o]=!0};for(let i=0;i<this.indices.length/3;i++){const n=i*3,o=this.indices[n],r=this.indices[n+1],c=this.indices[n+2];e(o,r),e(r,c),e(c,o)}const s=Object.keys(t);this.edges=new Int16Array(s.length*2);for(let i=0;i<s.length;i++){const n=s[i].split("_");this.edges[2*i]=parseInt(n[0],10),this.edges[2*i+1]=parseInt(n[1],10)}}getEdgeVertex(t,e,s){const i=this.edges[t*2+(e?1:0)];this.getVertex(i,s)}getEdgeVector(t,e){const s=ln,i=hn;this.getEdgeVertex(t,0,s),this.getEdgeVertex(t,1,i),i.vsub(s,e)}static computeNormal(t,e,s,i){e.vsub(t,Ie),s.vsub(e,Fe),Fe.cross(Ie,i),i.isZero()||i.normalize()}getVertex(t,e){const s=this.scale;return this._getUnscaledVertex(t,e),e.x*=s.x,e.y*=s.y,e.z*=s.z,e}_getUnscaledVertex(t,e){const s=t*3,i=this.vertices;return e.set(i[s],i[s+1],i[s+2])}getWorldVertex(t,e,s,i){return this.getVertex(t,i),j.pointToWorldFrame(e,s,i,i),i}getTriangleVertices(t,e,s,i){const n=t*3;this.getVertex(this.indices[n],e),this.getVertex(this.indices[n+1],s),this.getVertex(this.indices[n+2],i)}getNormal(t,e){const s=t*3;return e.set(this.normals[s],this.normals[s+1],this.normals[s+2])}calculateLocalInertia(t,e){this.computeLocalAABB(xt);const s=xt.upperBound.x-xt.lowerBound.x,i=xt.upperBound.y-xt.lowerBound.y,n=xt.upperBound.z-xt.lowerBound.z;return e.set(1/12*t*(2*i*2*i+2*n*2*n),1/12*t*(2*s*2*s+2*n*2*n),1/12*t*(2*i*2*i+2*s*2*s))}computeLocalAABB(t){const e=t.lowerBound,s=t.upperBound,i=this.vertices.length;this.vertices;const n=dn;this.getVertex(0,n),e.copy(n),s.copy(n);for(let o=0;o!==i;o++)this.getVertex(o,n),n.x<e.x?e.x=n.x:n.x>s.x&&(s.x=n.x),n.y<e.y?e.y=n.y:n.y>s.y&&(s.y=n.y),n.z<e.z?e.z=n.z:n.z>s.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+f<I.minForce?f=I.minForce-v:v+f>I.maxForce&&(f=I.maxForce-v),x[E]+=f,p+=f>0?f:-f,I.addToWlambda(f)}if(p*p<n)break}for(let E=0;E!==a;E++){const I=c[E],_=I.velocity,q=I.angularVelocity;I.vlambda.vmul(I.linearFactor,I.vlambda),_.vadd(I.vlambda,_),I.wlambda.vmul(I.angularFactor,I.wlambda),q.vadd(I.wlambda,q)}let g=o.length;const A=1/h;for(;g--;)o[g].multiplier=x[g]*A}return s}}const mn=[],vn=[],yn=[];class ir extends Ue{constructor(t){for(super(),this.iterations=10,this.tolerance=1e-7,this.subsolver=t,this.nodes=[],this.nodePool=[];this.nodePool.length<128;)this.nodePool.push(this.createNode())}createNode(){return{body:null,children:[],eqs:[],visited:!1}}solve(t,e){const s=wn,i=this.nodePool,n=e.bodies,o=this.equations,r=o.length,c=n.length,a=this.subsolver;for(;i.length<c;)i.push(this.createNode());s.length=c;for(let p=0;p<c;p++)s[p]=i[p];for(let p=0;p!==c;p++){const m=s[p];m.body=n[p],m.children.length=0,m.eqs.length=0,m.visited=!1}for(let p=0;p!==r;p++){const m=o[p],v=n.indexOf(m.bi),w=n.indexOf(m.bj),y=s[v],x=s[w];y.children.push(x),y.eqs.push(m),x.children.push(y),x.eqs.push(m)}let h,u=0,d=xn;a.tolerance=this.tolerance,a.iterations=this.iterations;const f=gn;for(;h=Ye(s);){d.length=0,f.bodies.length=0,An(h,Bn,f.bodies,d);const p=d.length;d=d.sort(En);for(let m=0;m!==p;m++)a.addEquation(d[m]);a.solve(t,f),a.removeAllEquations(),u++}return u}}const wn=[],xn=[],gn={bodies:[]},bn=M.STATIC;function Ye(z){const t=z.length;for(let e=0;e!==t;e++){const s=z[e];if(!s.visited&&!(s.body.type&bn))return s}return!1}const Ot=[];function An(z,t,e,s){for(Ot.push(z),z.visited=!0,t(z,e,s);Ot.length;){const i=Ot.pop();let n;for(;n=Ye(i.children);)n.visited=!0,t(n,e,s),Ot.push(n)}}function Bn(z,t,e){t.push(z.body);const s=z.eqs.length;for(let i=0;i!==s;i++){const n=z.eqs[i];e.includes(n)||e.push(n)}}function En(z,t){return t.id-z.id}class zn{constructor(){this.objects=[],this.type=Object}release(){const t=arguments.length;for(let e=0;e!==t;e++)this.objects.push(e<0||arguments.length<=e?void 0:arguments[e]);return this}get(){return this.objects.length===0?this.constructObject():this.objects.pop()}constructObject(){throw new Error("constructObject() not implemented in this Pool subclass yet!")}resize(t){const e=this.objects;for(;e.length>t;)e.pop();for(;e.length<t;)e.push(this.constructObject());return this}}class Cn extends zn{constructor(){super(...arguments),this.type=l}constructObject(){return new l}}const H={sphereSphere:S.types.SPHERE,spherePlane:S.types.SPHERE|S.types.PLANE,boxBox:S.types.BOX|S.types.BOX,sphereBox:S.types.SPHERE|S.types.BOX,planeBox:S.types.PLANE|S.types.BOX,convexConvex:S.types.CONVEXPOLYHEDRON,sphereConvex:S.types.SPHERE|S.types.CONVEXPOLYHEDRON,planeConvex:S.types.PLANE|S.types.CONVEXPOLYHEDRON,boxConvex:S.types.BOX|S.types.CONVEXPOLYHEDRON,sphereHeightfield:S.types.SPHERE|S.types.HEIGHTFIELD,boxHeightfield:S.types.BOX|S.types.HEIGHTFIELD,convexHeightfield:S.types.CONVEXPOLYHEDRON|S.types.HEIGHTFIELD,sphereParticle:S.types.PARTICLE|S.types.SPHERE,planeParticle:S.types.PLANE|S.types.PARTICLE,boxParticle:S.types.BOX|S.types.PARTICLE,convexParticle:S.types.PARTICLE|S.types.CONVEXPOLYHEDRON,cylinderCylinder:S.types.CYLINDER,sphereCylinder:S.types.SPHERE|S.types.CYLINDER,planeCylinder:S.types.PLANE|S.types.CYLINDER,boxCylinder:S.types.BOX|S.types.CYLINDER,convexCylinder:S.types.CONVEXPOLYHEDRON|S.types.CYLINDER,heightfieldCylinder:S.types.HEIGHTFIELD|S.types.CYLINDER,particleCylinder:S.types.PARTICLE|S.types.CYLINDER,sphereTrimesh:S.types.SPHERE|S.types.TRIMESH,planeTrimesh:S.types.PLANE|S.types.TRIMESH};class Sn{get[H.sphereSphere](){return this.sphereSphere}get[H.spherePlane](){return this.spherePlane}get[H.boxBox](){return this.boxBox}get[H.sphereBox](){return this.sphereBox}get[H.planeBox](){return this.planeBox}get[H.convexConvex](){return this.convexConvex}get[H.sphereConvex](){return this.sphereConvex}get[H.planeConvex](){return this.planeConvex}get[H.boxConvex](){return this.boxConvex}get[H.sphereHeightfield](){return this.sphereHeightfield}get[H.boxHeightfield](){return this.boxHeightfield}get[H.convexHeightfield](){return this.convexHeightfield}get[H.sphereParticle](){return this.sphereParticle}get[H.planeParticle](){return this.planeParticle}get[H.boxParticle](){return this.boxParticle}get[H.convexParticle](){return this.convexParticle}get[H.cylinderCylinder](){return this.convexConvex}get[H.sphereCylinder](){return this.sphereConvex}get[H.planeCylinder](){return this.planeConvex}get[H.boxCylinder](){return this.boxConvex}get[H.convexCylinder](){return this.convexConvex}get[H.heightfieldCylinder](){return this.heightfieldCylinder}get[H.particleCylinder](){return this.particleCylinder}get[H.sphereTrimesh](){return this.sphereTrimesh}get[H.planeTrimesh](){return this.planeTrimesh}constructor(t){this.contactPointPool=[],this.frictionEquationPool=[],this.result=[],this.frictionResult=[],this.v3pool=new Cn,this.world=t,this.currentContactMaterial=t.defaultContactMaterial,this.enableFrictionReduction=!1}createContactEquation(t,e,s,i,n,o){let r;this.contactPointPool.length?(r=this.contactPointPool.pop(),r.bi=t,r.bj=e):r=new Tt(t,e),r.enabled=t.collisionResponse&&e.collisionResponse&&s.collisionResponse&&i.collisionResponse;const c=this.currentContactMaterial;r.restitution=c.restitution,r.setSpookParams(c.contactEquationStiffness,c.contactEquationRelaxation,this.world.dt);const a=s.material||t.material,h=i.material||e.material;return a&&h&&a.restitution>=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;y<p.shapes.length;y++){p.quaternion.mult(p.shapeOrientations[y],c),p.quaternion.vmult(p.shapeOffsets[y],h),h.vadd(p.position,h);const x=p.shapes[y];for(let g=0;g<m.shapes.length;g++){m.quaternion.mult(m.shapeOrientations[g],a),m.quaternion.vmult(m.shapeOffsets[g],u),u.vadd(m.position,u);const A=m.shapes[g];if(!(x.collisionFilterMask&A.collisionFilterGroup&&A.collisionFilterMask&x.collisionFilterGroup)||h.distanceTo(u)>x.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.type<A.type?q=_.call(this,x,A,h,u,c,a,p,m,x,A,w):q=_.call(this,A,x,u,h,a,c,m,p,x,A,w),q&&w&&(s.shapeOverlapKeeper.set(x.id,A.id),s.bodyOverlapKeeper.set(p.id,m.id))}}}}}sphereSphere(t,e,s,i,n,o,r,c,a,h,u){if(u)return s.distanceSquared(i)<(t.radius+e.radius)**2;const d=this.createContactEquation(r,c,t,e,a,h);i.vsub(s,d.ni),d.ni.normalize(),d.ri.copy(d.ni),d.rj.copy(d.ni),d.ri.scale(t.radius,d.ri),d.rj.scale(-e.radius,d.rj),d.ri.vadd(s,d.ri),d.ri.vsub(r.position,d.ri),d.rj.vadd(i,d.rj),d.rj.vsub(c.position,d.rj),this.result.push(d),this.createFrictionEquationsFromContact(d,this.frictionResult)}spherePlane(t,e,s,i,n,o,r,c,a,h,u){const d=this.createContactEquation(r,c,t,e,a,h);if(d.ni.set(0,0,1),o.vmult(d.ni,d.ni),d.ni.negate(d.ni),d.ni.normalize(),d.ni.scale(t.radius,d.ri),s.vsub(i,Ht),d.ni.scale(d.ni.dot(Ht),Te),Ht.vsub(Te,d.rj),-Ht.dot(d.ni)<=t.radius){if(u)return!0;const f=d.ri,p=d.rj;f.vadd(s,f),f.vsub(r.position,f),p.vadd(i,p),p.vsub(c.position,p),this.result.push(d),this.createFrictionEquationsFromContact(d,this.frictionResult)}}boxBox(t,e,s,i,n,o,r,c,a,h,u){return t.convexPolyhedronRepresentation.material=t.material,e.convexPolyhedronRepresentation.material=e.material,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,this.convexConvex(t.convexPolyhedronRepresentation,e.convexPolyhedronRepresentation,s,i,n,o,r,c,t,e,u)}sphereBox(t,e,s,i,n,o,r,c,a,h,u){const d=this.v3pool,f=io;s.vsub(i,Gt),e.getSideNormals(f,o);const p=t.radius;let m=!1;const v=oo,w=ro,y=co;let x=null,g=0,A=0,E=0,I=null;for(let B=0,k=f.length;B!==k&&m===!1;B++){const L=to;L.copy(f[B]);const R=L.length();L.normalize();const W=Gt.dot(L);if(W<R+p&&W>0){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&&Q>-Z&&U<K&&U>-K){const $=Math.abs(W-R-p);if((I===null||$<I)&&(I=$,A=Q,E=U,x=R,v.copy(L),w.copy(O),y.copy(N),g++,u))return!0}}}if(g){m=!0;const B=this.createContactEquation(r,c,t,e,a,h);v.scale(-p,B.ri),B.ni.copy(v),B.ni.negate(B.ni),v.scale(x,v),w.scale(A,w),v.vadd(w,v),y.scale(E,y),v.vadd(y,B.rj),B.ri.vadd(s,B.ri),B.ri.vsub(r.position,B.ri),B.rj.vadd(i,B.rj),B.rj.vsub(c.position,B.rj),this.result.push(B),this.createFrictionEquationsFromContact(B,this.frictionResult)}let _=d.get();const q=no;for(let B=0;B!==2&&!m;B++)for(let k=0;k!==2&&!m;k++)for(let L=0;L!==2&&!m;L++)if(_.set(0,0,0),B?_.vadd(f[0],_):_.vsub(f[0],_),k?_.vadd(f[1],_):_.vsub(f[1],_),L?_.vadd(f[2],_):_.vsub(f[2],_),i.vadd(_,q),q.vsub(s,q),q.lengthSquared()<p*p){if(u)return!0;m=!0;const R=this.createContactEquation(r,c,t,e,a,h);R.ri.copy(q),R.ri.normalize(),R.ni.copy(R.ri),R.ri.scale(p,R.ri),R.rj.copy(_),R.ri.vadd(s,R.ri),R.ri.vsub(r.position,R.ri),R.rj.vadd(i,R.rj),R.rj.vsub(c.position,R.rj),this.result.push(R),this.createFrictionEquationsFromContact(R,this.frictionResult)}d.release(_),_=null;const T=d.get(),P=d.get(),V=d.get(),b=d.get(),F=d.get(),C=f.length;for(let B=0;B!==C&&!m;B++)for(let k=0;k!==C&&!m;k++)if(B%3!==k%3){f[k].cross(f[B],T),T.normalize(),f[B].vadd(f[k],P),V.copy(s),V.vsub(P,V),V.vsub(i,V);const L=V.dot(T);T.scale(L,b);let R=0;for(;R===B%3||R===k%3;)R++;F.copy(s),F.vsub(b,F),F.vsub(P,F),F.vsub(i,F);const W=Math.abs(L),O=F.length();if(W<f[R].length()&&O<p){if(u)return!0;m=!0;const N=this.createContactEquation(r,c,t,e,a,h);P.vadd(b,N.rj),N.rj.copy(N.rj),F.negate(N.ni),N.ni.normalize(),N.ri.copy(N.rj),N.ri.vadd(i,N.ri),N.ri.vsub(s,N.ri),N.ri.normalize(),N.ri.scale(p,N.ri),N.ri.vadd(s,N.ri),N.ri.vsub(r.position,N.ri),N.rj.vadd(i,N.rj),N.rj.vsub(c.position,N.rj),this.result.push(N),this.createFrictionEquationsFromContact(N,this.frictionResult)}}d.release(T,P,V,b,F)}planeBox(t,e,s,i,n,o,r,c,a,h,u){return e.convexPolyhedronRepresentation.material=e.material,e.convexPolyhedronRepresentation.collisionResponse=e.collisionResponse,e.convexPolyhedronRepresentation.id=e.id,this.planeConvex(t,e.convexPolyhedronRepresentation,s,i,n,o,r,c,t,e,u)}convexConvex(t,e,s,i,n,o,r,c,a,h,u,d,f){const p=Bo;if(!(s.distanceTo(i)>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()<v*v){if(u)return!0;w=!0;const E=this.createContactEquation(r,c,t,e,a,h);E.ri.copy(A),E.ri.normalize(),E.ni.copy(E.ri),E.ri.scale(v,E.ri),g.vsub(i,E.rj),E.ri.vadd(s,E.ri),E.ri.vsub(r.position,E.ri),E.rj.vadd(i,E.rj),E.rj.vsub(c.position,E.rj),this.result.push(E),this.createFrictionEquationsFromContact(E,this.frictionResult);return}}for(let y=0,x=p.length;y!==x&&w===!1;y++){const g=f[y],A=p[y],E=fo;o.vmult(g,E);const I=mo;o.vmult(m[A[0]],I),I.vadd(i,I);const _=vo;E.scale(-v,_),s.vadd(_,_);const q=yo;_.vsub(I,q);const T=q.dot(E),P=wo;if(s.vsub(I,P),T<0&&P.dot(E)>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*W<B.lengthSquared()&&O.lengthSquared()<v*v){if(u)return!0;const N=this.createContactEquation(r,c,t,e,a,h);L.vsub(i,N.rj),L.vsub(s,N.ni),N.ni.normalize(),N.ni.scale(v,N.ri),N.rj.vadd(i,N.rj),N.rj.vsub(c.position,N.rj),N.ri.vadd(s,N.ri),N.ri.vsub(r.position,N.ri),this.result.push(N),this.createFrictionEquationsFromContact(N,this.frictionResult);for(let Z=0,K=V.length;Z!==K;Z++)d.release(V[Z]);d.release(F),d.release(C),d.release(L),d.release(O),d.release(R);return}d.release(F),d.release(C),d.release(L),d.release(O),d.release(R)}for(let b=0,F=V.length;b!==F;b++)d.release(V[b])}}}planeConvex(t,e,s,i,n,o,r,c,a,h,u){const d=xo,f=go;f.set(0,0,1),n.vmult(f,f);let p=0;const m=bo;for(let v=0;v!==e.vertices.length;v++)if(d.copy(e.vertices[v]),o.vmult(d,d),i.vadd(d,d),d.vsub(s,m),f.dot(m)<=0){if(u)return!0;const y=this.createContactEquation(r,c,t,e,a,h),x=Ao;f.scale(f.dot(m),x),d.vsub(x,x),x.vsub(s,y.ri),y.ni.copy(f),d.vsub(i,y.rj),y.ri.vadd(s,y.ri),y.ri.vsub(r.position,y.ri),y.rj.vadd(i,y.rj),y.rj.vsub(c.position,y.rj),this.result.push(y),p++,this.enableFrictionReduction||this.createFrictionEquationsFromContact(y,this.frictionResult)}this.enableFrictionReduction&&p&&this.createFrictionFromAverage(p)}boxConvex(t,e,s,i,n,o,r,c,a,h,u){return t.convexPolyhedronRepresentation.material=t.material,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,this.convexConvex(t.convexPolyhedronRepresentation,e,s,i,n,o,r,c,t,e,u)}sphereHeightfield(t,e,s,i,n,o,r,c,a,h,u){const d=e.data,f=t.radius,p=e.elementSize,m=qo,v=Po;j.pointToLocalFrame(i,o,s,v);let w=Math.floor((v.x-f)/p)-1,y=Math.ceil((v.x+f)/p)+1,x=Math.floor((v.y-f)/p)-1,g=Math.ceil((v.y+f)/p)+1;if(y<0||g<0||w>d.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+f<E)return;const _=this.result;for(let q=w;q<y;q++)for(let T=x;T<g;T++){const P=_.length;let V=!1;if(e.getConvexTrianglePillar(q,T,!1),j.pointToWorldFrame(i,o,e.pillarOffset,m),s.distanceTo(m)<e.pillarConvex.boundingSphereRadius+t.boundingSphereRadius&&(V=this.sphereConvex(t,e.pillarConvex,s,m,n,o,r,c,t,e,u)),u&&V||(e.getConvexTrianglePillar(q,T,!0),j.pointToWorldFrame(i,o,e.pillarOffset,m),s.distanceTo(m)<e.pillarConvex.boundingSphereRadius+t.boundingSphereRadius&&(V=this.sphereConvex(t,e.pillarConvex,s,m,n,o,r,c,t,e,u)),u&&V))return!0;if(_.length-P>2)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+p<I))for(let q=y;q<x;q++)for(let T=g;T<A;T++){let P=!1;if(e.getConvexTrianglePillar(q,T,!1),j.pointToWorldFrame(i,o,e.pillarOffset,m),s.distanceTo(m)<e.pillarConvex.boundingSphereRadius+t.boundingSphereRadius&&(P=this.convexConvex(t,e.pillarConvex,s,m,n,o,r,c,null,null,u,v,null)),u&&P||(e.getConvexTrianglePillar(q,T,!0),j.pointToWorldFrame(i,o,e.pillarOffset,m),s.distanceTo(m)<e.pillarConvex.boundingSphereRadius+t.boundingSphereRadius&&(P=this.convexConvex(t,e.pillarConvex,s,m,n,o,r,c,null,null,u,v,null)),u&&P))return!0}}sphereParticle(t,e,s,i,n,o,r,c,a,h,u){const d=Fo;if(d.set(0,0,1),i.vsub(s,d),d.lengthSquared()<=t.radius*t.radius){if(u)return!0;const p=this.createContactEquation(c,r,e,t,a,h);d.normalize(),p.rj.copy(d),p.rj.scale(t.radius,p.rj),p.ni.copy(d),p.ni.negate(p.ni),p.ri.set(0,0,0),this.result.push(p),this.createFrictionEquationsFromContact(p,this.frictionResult)}}planeParticle(t,e,s,i,n,o,r,c,a,h,u){const d=zo;d.set(0,0,1),r.quaternion.vmult(d,d);const f=Co;if(i.vsub(r.position,f),d.dot(f)<=0){if(u)return!0;const m=this.createContactEquation(c,r,e,t,a,h);m.ni.copy(d),m.ni.negate(m.ni),m.ri.set(0,0,0);const v=So;d.scale(d.dot(i),v),i.vsub(v,v),m.rj.copy(v),this.result.push(m),this.createFrictionEquationsFromContact(m,this.frictionResult)}}boxParticle(t,e,s,i,n,o,r,c,a,h,u){return t.convexPolyhedronRepresentation.material=t.material,t.convexPolyhedronRepresentation.collisionResponse=t.collisionResponse,this.convexParticle(t.convexPolyhedronRepresentation,e,s,i,n,o,r,c,t,e,u)}convexParticle(t,e,s,i,n,o,r,c,a,h,u){let d=-1;const f=Mo,p=No;let m=null;const v=Io;if(v.copy(i),v.vsub(s,v),n.conjugate(Re),Re.vmult(v,v),t.pointIsInside(v)){t.worldVerticesNeedsUpdate&&t.computeWorldVertices(s,n),t.worldFaceNormalsNeedsUpdate&&t.computeWorldFaceNormals(n);for(let w=0,y=t.faces.length;w!==y;w++){const x=[t.worldVertices[t.faces[w][0]]],g=t.worldFaceNormals[w];i.vsub(x[0],Pe);const A=-g.dot(Pe);if(m===null||Math.abs(A)<Math.abs(m)){if(u)return!0;m=A,d=w,f.copy(g)}}if(d!==-1){const w=this.createContactEquation(c,r,e,t,a,h);f.scale(m,p),p.vadd(i,p),p.vsub(s,p),w.rj.copy(p),f.negate(w.ni),w.ri.set(0,0,0);const y=w.ri,x=w.rj;y.vadd(i,y),y.vsub(c.position,y),x.vadd(s,x),x.vsub(r.position,x),this.result.push(w),this.createFrictionEquationsFromContact(w,this.frictionResult)}else console.warn("Point found inside convex, but did not find penetrating face!")}}heightfieldCylinder(t,e,s,i,n,o,r,c,a,h,u){return this.convexHeightfield(e,t,i,s,o,n,c,r,a,h,u)}particleCylinder(t,e,s,i,n,o,r,c,a,h,u){return this.convexParticle(e,t,i,s,o,n,c,r,a,h,u)}sphereTrimesh(t,e,s,i,n,o,r,c,a,h,u){const d=Vn,f=jn,p=kn,m=On,v=Hn,w=Gn,y=Xn,x=Wn,g=qn,A=$n;j.pointToLocalFrame(i,o,s,v);const E=t.radius;y.lowerBound.set(v.x-E,v.y-E,v.z-E),y.upperBound.set(v.x+E,v.y+E,v.z+E),e.getTrianglesInAABB(y,A);const I=Ln,_=t.radius*t.radius;for(let b=0;b<A.length;b++)for(let F=0;F<3;F++)if(e.getVertex(e.indices[A[b]*3+F],I),I.vsub(v,g),g.lengthSquared()<=_){if(x.copy(I),j.pointToWorldFrame(i,o,x,I),I.vsub(s,g),u)return!0;let C=this.createContactEquation(r,c,t,e,a,h);C.ni.copy(g),C.ni.normalize(),C.ri.copy(C.ni),C.ri.scale(t.radius,C.ri),C.ri.vadd(s,C.ri),C.ri.vsub(r.position,C.ri),C.rj.copy(I),C.rj.vsub(c.position,C.rj),this.result.push(C),this.createFrictionEquationsFromContact(C,this.frictionResult)}for(let b=0;b<A.length;b++)for(let F=0;F<3;F++){e.getVertex(e.indices[A[b]*3+F],d),e.getVertex(e.indices[A[b]*3+(F+1)%3],f),f.vsub(d,p),v.vsub(f,w);const C=w.dot(p);v.vsub(d,w);let B=w.dot(p);if(B>0&&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)<t.radius)){if(u)return!0;const L=this.createContactEquation(r,c,t,e,a,h);w.vsub(v,L.ni),L.ni.normalize(),L.ni.scale(t.radius,L.ri),L.ri.vadd(s,L.ri),L.ri.vsub(r.position,L.ri),j.pointToWorldFrame(i,o,w,w),w.vsub(c.position,L.rj),j.vectorToWorldFrame(o,L.ni,L.ni),j.vectorToWorldFrame(o,L.ri,L.ri),this.result.push(L),this.createFrictionEquationsFromContact(L,this.frictionResult)}}const q=Dn,T=Un,P=Yn,V=Pn;for(let b=0,F=A.length;b!==F;b++){e.getTriangleVertices(A[b],q,T,P),e.getNormal(A[b],V),v.vsub(q,w);let C=w.dot(V);if(V.scale(C,w),v.vsub(w,w),C=w.distanceTo(v),Y.pointInTriangle(w,q,T,P)&&C<t.radius){if(u)return!0;let B=this.createContactEquation(r,c,t,e,a,h);w.vsub(v,B.ni),B.ni.normalize(),B.ni.scale(t.radius,B.ri),B.ri.vadd(s,B.ri),B.ri.vsub(r.position,B.ri),j.pointToWorldFrame(i,o,w,w),w.vsub(c.position,B.rj),j.vectorToWorldFrame(o,B.ni,B.ni),j.vectorToWorldFrame(o,B.ri,B.ri),this.result.push(B),this.createFrictionEquationsFromContact(B,this.frictionResult)}}A.length=0}planeTrimesh(t,e,s,i,n,o,r,c,a,h,u){const d=new l,f=_n;f.set(0,0,1),n.vmult(f,f);for(let p=0;p<e.vertices.length/3;p++){e.getVertex(p,d);const m=new l;m.copy(d),j.pointToWorldFrame(i,o,m,d);const v=Tn;if(d.vsub(s,v),f.dot(v)<=0){if(u)return!0;const y=this.createContactEquation(r,c,t,e,a,h);y.ni.copy(f);const x=Rn;f.scale(v.dot(f),x),d.vsub(x,x),y.ri.copy(x),y.ri.vsub(r.position,y.ri),y.rj.copy(d),y.rj.vsub(c.position,y.rj),this.result.push(y),this.createFrictionEquationsFromContact(y,this.frictionResult)}}}}const gt=new l,zt=new l,Ct=new l,Fn=new l,In=new l,Mn=new D,Nn=new D,_n=new l,Tn=new l,Rn=new l,Pn=new l,qn=new l;new l;const Ln=new l,Wn=new l,Vn=new l,jn=new l,kn=new l,On=new l,Hn=new l,Gn=new l,Dn=new l,Un=new l,Yn=new l,Xn=new G,$n=[],Ht=new l,Te=new l,Zn=new l,Kn=new l,Qn=new l;function Jn(z,t,e){let s=null;const i=z.length;for(let n=0;n!==i;n++){const o=z[n],r=Zn;z[(n+1)%i].vsub(o,r);const c=Kn;r.cross(t,c);const a=Qn;e.vsub(o,a);const h=c.dot(a);if(s===null||h>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(e<t){const s=e;e=t,t=s}return t<<16|e}set(t,e){const s=this.getKey(t,e),i=this.current;let n=0;for(;s>i[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;c<n;c++){let a=!1;const h=s[c];for(;h>i[r];)r++;a=h===i[r],a||Le(t,h)}r=0;for(let c=0;c<o;c++){let a=!1;const h=i[c];for(;h>s[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)=>z<t?`${z}-${t}`:`${t}-${z}`;class Lo{constructor(){this.data={keys:[]}}get(t,e){const s=Jt(t,e);return this.data[s]}set(t,e,s){const i=Jt(t,e);this.get(t,e)||this.data.keys.push(i),this.data[i]=s}delete(t,e){const s=Jt(t,e),i=this.data.keys.indexOf(s);i!==-1&&this.data.keys.splice(i,1),delete this.data[s]}reset(){const t=this.data,e=t.keys;for(;e.length>0;){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<e.length;s++){const i=e[s].shapes;for(let n=0;n<i.length;n++){const o=i[n];if(o.id===t)return o}}return null}addContactMaterial(t){this.contactmaterials.push(t),this.contactMaterialTable.set(t.materials[0].id,t.materials[1].id,t)}removeContactMaterial(t){const e=this.contactmaterials.indexOf(t);e!==-1&&(this.contactmaterials.splice(e,1),this.contactMaterialTable.delete(t.materials[0].id,t.materials[1].id))}fixedStep(t,e){t===void 0&&(t=1/60),e===void 0&&(e=10);const s=X.now()/1e3;if(!this.lastCallTime)this.step(t,void 0,e);else{const i=s-this.lastCallTime;this.step(t,i,e)}this.lastCallTime=s}step(t,e,s){if(s===void 0&&(s=10),e===void 0)this.internalStep(t),this.time+=t;else{this.accumulator+=e;const i=X.now();let n=0;for(;this.accumulator>=t&&n<s&&(this.internalStep(t),this.accumulator-=t,n++,!(X.now()-i>t*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<this.frictionEquations.length;y++)r.addEquation(this.frictionEquations[y]);const I=e.length;for(let b=0;b!==I;b++){const F=e[b],C=F.bi,B=F.bj,k=F.si,L=F.sj;let R;if(C.material&&B.material?R=this.getContactMaterial(C.material,B.material)||this.defaultContactMaterial:R=this.defaultContactMaterial,R.friction,C.material&&B.material&&(C.material.friction>=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;n<o;n+=2)Mt.bodyA=this.getBodyById(dt[n]),Mt.bodyB=this.getBodyById(dt[n+1]),this.dispatchEvent(Mt);Mt.bodyA=Mt.bodyB=null}if(e){for(let n=0,o=ut.length;n<o;n+=2)Nt.bodyA=this.getBodyById(ut[n]),Nt.bodyB=this.getBodyById(ut[n+1]),this.dispatchEvent(Nt);Nt.bodyA=Nt.bodyB=null}dt.length=ut.length=0;const s=this.hasAnyEventListener("beginShapeContact"),i=this.hasAnyEventListener("endShapeContact");if((s||i)&&this.shapeOverlapKeeper.getDiff(dt,ut),s){for(let n=0,o=dt.length;n<o;n+=2){const r=this.getShapeById(dt[n]),c=this.getShapeById(dt[n+1]);pt.shapeA=r,pt.shapeB=c,r&&(pt.bodyA=r.body),c&&(pt.bodyB=c.body),this.dispatchEvent(pt)}pt.bodyA=pt.bodyB=pt.shapeA=pt.shapeB=null}if(i){for(let n=0,o=ut.length;n<o;n+=2){const r=this.getShapeById(ut[n]),c=this.getShapeById(ut[n+1]);ft.shapeA=r,ft.shapeB=c,r&&(ft.bodyA=r.body),c&&(ft.bodyB=c.body),this.dispatchEvent(ft)}ft.bodyA=ft.bodyB=ft.shapeA=ft.shapeB=null}}clearForces(){const t=this.bodies,e=t.length;for(let s=0;s!==e;s++){const i=t[s];i.force,i.torque,i.force.set(0,0,0),i.torque.set(0,0,0)}}}new G;const te=new Y,X=globalThis.performance||{};if(!X.now){let z=Date.now();X.timing&&X.timing.navigationStart&&(z=X.timing.navigationStart),X.now=()=>Date.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};