Skip to content

Commit

Permalink
Fixes effectors
Browse files Browse the repository at this point in the history
  • Loading branch information
mflerackers committed Dec 10, 2024
1 parent b89b97c commit 48653ae
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/components/physics/effectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function surfaceEffector(
speedVariation: opts.speedVariation ?? 0,
forceScale: opts.speedVariation ?? 0.9,
add(this: GameObj<AreaComp | SurfaceEffectorComp>) {
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);
Expand Down Expand Up @@ -57,7 +58,8 @@ export function areaEffector(opts: AreaEffectorCompOpt): AreaEffectorComp {
linearDrag: opts.linearDrag ?? 0,
useGlobalAngle: opts.useGlobalAngle ?? true,
add(this: GameObj<AreaComp | AreaEffectorComp>) {
this.onCollideUpdate("body", obj => {
this.onCollideUpdate(obj => {
if (!obj.has("body")) return;
obj.addForce(
this.useGlobalAngle
? this.force
Expand Down Expand Up @@ -97,15 +99,16 @@ export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp {
linearDrag: opts.linearDrag ?? 0,
// angularDrag: opts.angularDrag ?? 0,
add(this: GameObj<PointEffectorComp | AreaComp | PosComp>) {
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,
);
Expand Down Expand Up @@ -249,7 +252,8 @@ export function buoyancyEffector(
flowMagnitude: opts.flowMagnitude ?? 0,
flowVariation: opts.flowVariation ?? 0,
add(this: GameObj<AreaComp | BuoyancyEffectorComp>) {
this.onCollideUpdate("body", (obj, col) => {
this.onCollideUpdate((obj, col) => {
if (!obj.has("body")) return;
const o = obj as GameObj<BodyComp | AreaComp>;
const shape = o.worldArea();
const polygon: Polygon = shape instanceof Polygon
Expand Down

0 comments on commit 48653ae

Please sign in to comment.