Skip to content

Commit

Permalink
fix: Only apply forces to bodies (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
mflerackers authored Aug 18, 2024
1 parent 7a1f2c6 commit 2a8aedb
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/components/physics/effectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getGravity, getGravityDirection } from "../../game";
import { width } from "../../gfx";
import { debug } from "../../kaplay";
import { getGravityDirection } from "../../game";
import { Polygon, Vec2, vec2 } from "../../math";
import type { Comp, GameObj } from "../../types";
import type { PosComp } from "../transform";
Expand Down Expand Up @@ -29,7 +27,7 @@ export function surfaceEffector(
speedVariation: opts.speedVariation ?? 0,
forceScale: opts.speedVariation ?? 0.9,
add(this: GameObj<AreaComp | SurfaceEffectorComp>) {
this.onCollideUpdate((obj, col) => {
this.onCollideUpdate("body", (obj, col) => {
const dir = col?.normal.normal();
const currentVel = obj.vel.project(dir);
const wantedVel = dir?.scale(this.speed);
Expand Down Expand Up @@ -69,7 +67,7 @@ export function areaEffector(opts: AreaEffectorCompOpt): AreaEffectorComp {
linearDrag: opts.linearDrag ?? 0,
// angularDrag: opts.angularDrag ?? 0,
add(this: GameObj<AreaComp | AreaEffectorComp>) {
this.onCollideUpdate((obj, col) => {
this.onCollideUpdate("body", (obj, col) => {
const dir = Vec2.fromAngle(this.forceAngle);
const force = dir.scale(this.forceMagnitude);
obj.addForce(force);
Expand Down Expand Up @@ -112,15 +110,15 @@ export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp {
linearDrag: opts.linearDrag ?? 0,
// angularDrag: opts.angularDrag ?? 0,
add(this: GameObj<PointEffectorComp | AreaComp | PosComp>) {
this.onCollideUpdate((obj, col) => {
this.onCollideUpdate("body", (obj, col) => {
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,
);
Expand Down Expand Up @@ -221,7 +219,7 @@ export function buoyancyEffector(
flowMagnitude: opts.flowMagnitude ?? 0,
flowVariation: opts.flowVariation ?? 0,
add(this: GameObj<AreaComp | BuoyancyEffectorComp>) {
this.onCollideUpdate((obj, col) => {
this.onCollideUpdate("body", (obj, col) => {
const o = obj as GameObj<BodyComp | AreaComp>;
const polygon = o.worldArea();
const [submergedArea, _] = polygon.cut(
Expand Down

0 comments on commit 2a8aedb

Please sign in to comment.