UNPKG

@dominicstop/utils

Version:

Yet another event emitter written in typescript.

30 lines (29 loc) 863 B
/** * Applies a central attractive force pulling the particle toward * a fixed center (e.g., like a spring). */ export class CentralAttractionForce { constructor(center, strength, isActive = true) { this.center = center; this.strength = strength; this.isActive = isActive; } ; /** * Applies a force to the particle: * `F = -strength * (particle.position - center)` * * This simulates a harmonic potential pulling particles inward. * * @param particle The particle to apply the force to. */ apply(particle) { if (!this.isActive || particle.isStatic) return; const displacement = particle.position.subtractedWithOther(this.center); const force = displacement.multipliedByScalar(-this.strength); particle.applyForce(force); } ; } ;