UNPKG

@downpourdigital/physics

Version:

Animation physics!

2 lines (1 loc) 4.79 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("gl-matrix"),s=require("./cf94038a.js");const e=new Float32Array(4),i=new Float32Array(4);function a(t,s,a,r,h,c){s.sub(e,a[0],r),s.scale(e,e,-h),s.scale(i,a[1],c),s.sub(t,e,i)}class r extends s.Stepper{constructor(t={}){var{value:e,velocity:i,vector:a,mass:r=1,restDelta:h=5e-4}=t;super(s.__rest(t,["value","velocity","vector","mass","restDelta"])),this.v=a,this.mass=r,this.restDelta=h,this.state=[this.v.create(),this.v.create()],this.initial=this.v.create(),e&&(this.v.copy(this.state[0],e),this.v.copy(this.initial,e)),i&&this.v.copy(this.state[1],i)}computeForce(t,s){}checkResting(){return this.v.len(this.state[1])<this.restDelta}get(t){return this.v.copy(t,this.state[0]),t}read(){return this.state[0]}getVelocity(t){this.v.copy(t,this.state[1])}readVelocity(){return this.state[1]}setVelocity(t){this.v.copy(this.state[1],t)}reset(){this.resetTo(this.initial)}resetTo(t){this.v.copy(this.state[0],t),this.v.zero(this.state[1])}stop(){s.defaultWorld.remove(this)}}const h=new Float32Array(4);class c extends r{integrate(t){const s=t/1e3;this.computeForce(h,this.state),this.v.scale(h,h,1/this.mass),this.v.scaleAndAdd(this.state[1],this.state[1],h,s),this.v.scaleAndAdd(this.state[0],this.state[0],this.state[1],s),this.isResting=this.checkResting(),this.isResting&&this.v.zero(this.state[1])}}class n extends c{constructor(t={}){var{stiffness:e=250,damping:i=15}=t,a=s.__rest(t,["stiffness","damping"]);super(a),this.stiffness=e,this.damping=i,this.target=this.v.create(),a.value&&this.v.copy(this.target,a.value)}step(t){super.step(t),this.isResting&&this.v.copy(this.state[0],this.target)}computeForce(t,s){a(t,this.v,s,this.target,this.stiffness,this.damping)}checkResting(){return this.v.len(this.state[1])<this.restDelta&&this.v.dist(this.state[0],this.target)<this.restDelta}set(t){this.v.equals(this.target,t)||(this.v.copy(this.target,t),this.isResting=!1)}resetTo(t){super.resetTo(t),this.v.copy(this.target,t)}}const o=new Float32Array(4),l=new Float32Array(4),v=new Float32Array(4),g=new Float32Array(4),u=[new Float32Array(4),new Float32Array(4)],p=[new Float32Array(4),new Float32Array(4)],d=[new Float32Array(4),new Float32Array(4)],y=[new Float32Array(4),new Float32Array(4)],A=[new Float32Array(4),new Float32Array(4)];class m extends r{integrate(t){const s=t/1e3;this.evaluate(u,0,[g,g]),this.evaluate(p,.5*s,u),this.evaluate(d,.5*s,p),this.evaluate(y,s,d),this.v.add(l,p[0],d[0]),this.v.scaleAndAdd(l,u[0],l,2),this.v.add(l,l,y[0]),this.v.scale(l,l,1/6),this.v.add(v,p[1],d[1]),this.v.scaleAndAdd(v,u[1],v,2),this.v.add(v,v,y[1]),this.v.scale(v,v,1/6),this.v.scaleAndAdd(this.state[1],this.state[1],v,s),this.v.scaleAndAdd(this.state[0],this.state[0],l,s),this.isResting=this.checkResting(),this.isResting&&this.v.zero(this.state[1])}evaluate(t,s,e){this.v.scaleAndAdd(A[0],this.state[0],e[0],s),this.v.scaleAndAdd(A[1],this.state[1],e[1],s),this.computeForce(o,A),this.v.copy(t[0],A[1]),this.v.scale(t[1],o,1/this.mass)}}class F extends m{constructor(t={}){var{stiffness:e=250,damping:i=15}=t,a=s.__rest(t,["stiffness","damping"]);super(a),this.stiffness=e,this.damping=i,this.target=this.v.create(),a.value&&this.v.copy(this.target,a.value)}step(t){super.step(t),this.isResting&&this.v.copy(this.state[0],this.target)}computeForce(t,s){a(t,this.v,s,this.target,this.stiffness,this.damping)}checkResting(){return this.v.len(this.state[1])<this.restDelta&&this.v.dist(this.state[0],this.target)<this.restDelta}set(t){this.v.equals(this.target,t)||(this.v.copy(this.target,t),this.isResting=!1)}resetTo(t){super.resetTo(t),this.v.copy(this.target,t)}}exports.EulerSpring2D=class extends n{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec2}))}},exports.EulerSpring3D=class extends n{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec3}))}},exports.EulerSpringQuat=class extends n{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec4})),t.quat.identity(this.state[0])}step(s){t.quat.dot(this.state[0],this.target)<0&&t.quat.set(this.target,-this.target[0],-this.target[1],-this.target[2],-this.target[3]),super.step(s),t.quat.normalize(this.state[0],this.state[0])}},exports.RK4Spring2D=class extends F{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec2}))}},exports.RK4Spring3D=class extends F{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec3}))}},exports.RK4SpringQuat=class extends F{constructor(s){super(Object.assign(Object.assign({},s),{vector:t.vec4})),t.quat.identity(this.state[0])}step(s){t.quat.dot(this.state[0],this.target)<0&&t.quat.set(this.target,-this.target[0],-this.target[1],-this.target[2],-this.target[3]),super.step(s),t.quat.normalize(this.state[0],this.state[0])}};