CRITICAL: Vec2 mutation bugs in math.js and PhysicsEngine corrupt data #4

Closed
opened 2026-05-11 06:16:42 +00:00 by Mark · 0 comments
Owner

Description

Several functions mutate Vec2 objects in-place that should remain unchanged, corrupting gravity well data.

Specific cases

  1. math.js line 210: bodyPos.sub(shipPos) mutates bodyPos (the gravity well's position)
  2. math.js line 215: diff.normalize().mul(forceMag) chains mutate the diff vector
  3. PhysicsEngine.js line 33: well.pos.sub(ship.pos) mutates the well's stored position
  4. PhysicsEngine.js lines 40-41: diff.normalize() then totalForce.add(dir.mul(forceMag)) mutates dir (which is diff)

Impact

After each frame, gravity well positions shift, becoming garbage data over time. Checkpoint positions and track elements may also be affected.

Fix

Use Vec2.sub() as a static method returning a new Vec2 (like Vec2.dist()), or clone vectors before mutating them.

## Description Several functions mutate Vec2 objects in-place that should remain unchanged, corrupting gravity well data. ## Specific cases 1. `math.js` line 210: `bodyPos.sub(shipPos)` mutates `bodyPos` (the gravity well's position) 2. `math.js` line 215: `diff.normalize().mul(forceMag)` chains mutate the diff vector 3. `PhysicsEngine.js` line 33: `well.pos.sub(ship.pos)` mutates the well's stored position 4. `PhysicsEngine.js` lines 40-41: `diff.normalize()` then `totalForce.add(dir.mul(forceMag))` mutates `dir` (which is `diff`) ## Impact After each frame, gravity well positions shift, becoming garbage data over time. Checkpoint positions and track elements may also be affected. ## Fix Use `Vec2.sub()` as a static method returning a new Vec2 (like `Vec2.dist()`), or clone vectors before mutating them.
Mark closed this issue 2026-05-11 18:18:23 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Mark/gravity-racer#4
No description provided.