import { add , dot , magnitude , normalize , scale , set , copy , sub , v2 , Vector2 } from "./v2" ; const basis = v2 ( ) ; const basisNeg = v2 ( ) ; const vel1 = v2 ( ) ; const vel1x = v2 ( ) ; const vel1y = v2 ( ) ; const vel2 = v2 ( ) ; const vel2x = v2 ( ) ; const vel2y = v2 ( ) ; const newVel1 = v2 ( ) ; const newVel2 = v2 ( ) ; const t1 = v2 ( ) ; const t2 = v2 ( ) ; const u1 = v2 ( ) ; const u2 = v2 ( ) ; const EPSILON = 0.0001 ; param cpos1 param ppos1 param mass1 param restitution1 param staticFriction1 param dynamicFriction1 param cpos2 param ppos2 param mass2 param restitution2 param staticFriction2 param dynamicFriction2 param collisionNormal param vel1out param vel2out export const collisionResponseAABB = ( cpos1 , ppos1 , mass1 , restitution1 , staticFriction1 , dynamicFriction1 , cpos2 , ppos2 , mass2 , restitution2 , staticFriction2 , dynamicFriction2 , collisionNormal , vel1out , vel2out ) => { set ( basis , 0 , 0 ) ; set ( basisNeg , 0 , 0 ) ; set ( vel1 , 0 , 0 ) ; set ( vel1x , 0 , 0 ) ; set ( vel1y , 0 , 0 ) ; set ( vel2 , 0 , 0 ) ; set ( vel2x , 0 , 0 ) ; set ( vel2y , 0 , 0 ) ; set ( newVel1 , 0 , 0 ) ; set ( newVel2 , 0 , 0 ) ; set ( t1 , 0 , 0 ) ; set ( t2 , 0 , 0 ) ; set ( u1 , 0 , 0 ) ; set ( u2 , 0 , 0 ) ; if ( collisionNormal && ( collisionNormal . x !== 0 || collisionNormal . y !== 0 ) ) { set ( basis , collisionNormal . x , collisionNormal . y ) ; } else { sub ( basis , cpos1 , cpos2 ) ; normalize ( basis , basis ) ; } scale ( basisNeg , basis , - 1 ) ; const restitution = restitution1 > restitution2 ? restitution1 : restitution2 ; const massTotal = mass1 + mass2 ; const e = 1 + restitution ; sub ( vel1 , cpos1 , ppos1 ) ; const x1 = dot ( basis , vel1 ) ; scale ( vel1x , basis , x1 ) ; sub ( vel1y , vel1 , vel1x ) ; sub ( vel2 , cpos2 , ppos2 ) ; const x2 = dot ( basisNeg , vel2 ) ; scale ( vel2x , basisNeg , x2 ) ; sub ( vel2y , vel2 , vel2x ) ; scale ( t1 , vel1x , ( mass1 - mass2 ) / massTotal ) ; scale ( t2 , vel2x , ( e * mass2 ) / massTotal ) ; add ( newVel1 , t1 , t2 ) ; add ( newVel1 , newVel1 , vel1y ) ; scale ( u1 , vel1x , ( e * mass1 ) / massTotal ) ; scale ( u2 , vel2x , ( mass2 - mass1 ) / massTotal ) ; add ( newVel2 , u1 , u2 ) ; add ( newVel2 , newVel2 , vel2y ) ; const rv = add ( v2 ( ) , newVel1 , newVel2 ) ; const reg1 = v2 ( ) ; scale ( reg1 , basis , dot ( rv , basis ) ) ; const tangent = sub ( v2 ( ) , rv , reg1 ) ; normalize ( tangent , tangent ) ; let jt = - dot ( rv , tangent ) ; jt /= 1 / ( mass1 + mass2 ) ; const jtMag = Math . abs ( jt ) ; if ( jtMag > EPSILON ) { const vel1ymag = magnitude ( vel1y ) ; const vel2ymag = magnitude ( vel2y ) ; const frictionImpulse1 = v2 ( ) ; const frictionImpulse2 = v2 ( ) ; if ( jtMag < vel1ymag * staticFriction1 ) { scale ( frictionImpulse1 , tangent , staticFriction1 ) ; } else { scale ( frictionImpulse1 , tangent , - vel1ymag * dynamicFriction1 ) ; } if ( jtMag < vel2ymag * staticFriction2 ) { scale ( frictionImpulse2 , tangent , staticFriction2 ) ; } else { scale ( frictionImpulse2 , tangent , - vel2ymag * dynamicFriction2 ) ; } add ( newVel1 , newVel1 , frictionImpulse1 ) ; add ( newVel2 , newVel2 , frictionImpulse2 ) ; } copy ( vel1out , newVel1 ) ; copy ( vel2out , newVel2 ) ; } ;