From 48653ae07d6f6943d8548cd980996bb2db1ea921 Mon Sep 17 00:00:00 2001 From: Marc Flerackers Date: Tue, 10 Dec 2024 13:36:32 +0900 Subject: [PATCH] Fixes effectors --- src/components/physics/effectors.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/components/physics/effectors.ts b/src/components/physics/effectors.ts index 11aecdba..30b5c671 100644 --- a/src/components/physics/effectors.ts +++ b/src/components/physics/effectors.ts @@ -26,7 +26,8 @@ export function surfaceEffector( speedVariation: opts.speedVariation ?? 0, forceScale: opts.speedVariation ?? 0.9, add(this: GameObj) { - this.onCollideUpdate("body", (obj, col) => { + this.onCollideUpdate((obj, col) => { + if (!obj.has("body")) return; const dir = col?.normal.normal(); const currentVel = obj.vel.project(dir); const wantedVel = dir?.scale(this.speed); @@ -57,7 +58,8 @@ export function areaEffector(opts: AreaEffectorCompOpt): AreaEffectorComp { linearDrag: opts.linearDrag ?? 0, useGlobalAngle: opts.useGlobalAngle ?? true, add(this: GameObj) { - this.onCollideUpdate("body", obj => { + this.onCollideUpdate(obj => { + if (!obj.has("body")) return; obj.addForce( this.useGlobalAngle ? this.force @@ -97,15 +99,16 @@ export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp { linearDrag: opts.linearDrag ?? 0, // angularDrag: opts.angularDrag ?? 0, add(this: GameObj) { - this.onCollideUpdate("body", (obj, col) => { + this.onCollideUpdate((obj, col) => { + if (!obj.has("body")) return; const dir = this.pos.sub(obj.pos); const length = dir.len(); const distance = length * this.distanceScale / 10; const forceScale = this.forceMode === "constant" ? 1 : this.forceMode === "inverseLinear" - ? 1 / distance - : 1 / distance ** 2; + ? 1 / distance + : 1 / distance ** 2; const force = dir.scale( this.forceMagnitude * forceScale / length, ); @@ -249,7 +252,8 @@ export function buoyancyEffector( flowMagnitude: opts.flowMagnitude ?? 0, flowVariation: opts.flowVariation ?? 0, add(this: GameObj) { - this.onCollideUpdate("body", (obj, col) => { + this.onCollideUpdate((obj, col) => { + if (!obj.has("body")) return; const o = obj as GameObj; const shape = o.worldArea(); const polygon: Polygon = shape instanceof Polygon