@dominicstop/utils
Version:
Yet another event emitter written in typescript.
34 lines (33 loc) • 1.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CentralAttractionForce = void 0;
/**
* Applies a central attractive force pulling the particle toward
* a fixed center (e.g., like a spring).
*/
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);
}
;
}
exports.CentralAttractionForce = CentralAttractionForce;
;