import { add , magnitude , normalize , scale , set , v2 } from "./v2" ; import { Integratable , VelocityDerivable } from "./common-types" ; const accel1 = v2 ( ) ; export function solveGravitation ( p1 , p1mass , p2 , p2mass , gravityConstant = 0.99 ) { if ( p1mass <= 0 || p2mass <= 0 ) return ; let mag ; let factor ; const diffx = p2 . cpos . x - p1 . cpos . x ; const diffy = p2 . cpos . y - p1 . cpos . y ; set ( accel1 , diffx , diffy ) ; mag = magnitude ( accel1 ) ; mag = mag === 0 ? 1 : mag ; factor = gravityConstant * ( ( p1mass * p2mass ) / ( mag * mag ) ) ; normalize ( accel1 , accel1 ) ; scale ( accel1 , accel1 , factor ) ; add ( p1 . acel , p1 . acel , accel1 ) ; }