CRITICAL: Vec2 mutation bugs in math.js and PhysicsEngine corrupt data #4
Labels
No labels
blocked:needs-decision
blocked:no-changes
feature-request
quickfix
test-coverage
triage-approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Mark/gravity-racer#4
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
Several functions mutate Vec2 objects in-place that should remain unchanged, corrupting gravity well data.
Specific cases
math.jsline 210:bodyPos.sub(shipPos)mutatesbodyPos(the gravity well's position)math.jsline 215:diff.normalize().mul(forceMag)chains mutate the diff vectorPhysicsEngine.jsline 33:well.pos.sub(ship.pos)mutates the well's stored positionPhysicsEngine.jslines 40-41:diff.normalize()thentotalForce.add(dir.mul(forceMag))mutatesdir(which isdiff)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 (likeVec2.dist()), or clone vectors before mutating them.