From 2acbfffc5f77cf94017875e79e0b47c548fc4c68 Mon Sep 17 00:00:00 2001 From: root <> Date: Thu, 24 Apr 2025 12:26:57 +0000 Subject: [PATCH] bugfix: velocity unbounded accel not projected strange behaviour with velocity.mag() sometimes it is unbounded, sometimes it remains bounded the issue is `project_onto_plane` mutates its first argument (1) mutating fn yyyynnnn (2) call fn yynnyynn (3) use fn retval ynynynyn (1) & (2) yynnnnnn [(1) & (2)] | (3) yyynynyn bounded? yy-nyn-n the case that we want is (1) yes (2) yes (3) no or this one (1) no (2) yes (3) yes --- sketch.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/sketch.js b/sketch.js index e08bbe8..362e59f 100644 --- a/sketch.js +++ b/sketch.js @@ -38,18 +38,16 @@ function move_charges() { const acceleration_mag = 1 / displacement.mag() * 0.005; let ai = displacement.copy().normalize().mult(acceleration_mag); let aj = p5.Vector.mult(ai, -1); - let ai_norm = project_onto_plane(ai, charges[i].position); - - let aj_norm = project_onto_plane(aj, charges[j].position); - - charges[i].acceleration.add(ai_norm); - charges[j].acceleration.add(aj_norm); + project_onto_plane(ai, charges[i].position); + project_onto_plane(aj, charges[j].position); + charges[i].acceleration.add(ai); + charges[j].acceleration.add(aj); } } for (let i = 0; i < charges.length; i += 1) { let charge = charges[i]; charge.velocity = charge.velocity.add(charge.acceleration); - //charge.velocity = project_onto_plane(charge.velocity, charge.position); + //project_onto_plane(charge.velocity, charge.position); charge.position = charge.position.add(charge.velocity); charge.position.normalize(); } @@ -61,9 +59,10 @@ function project_onto_unit_vector(v, unit_vector) { } /// Project `v` onto the plane normal to `unit_vector` +/// Mutates `v` function project_onto_plane(v, unit_vector) { let v_proj = project_onto_unit_vector(v, unit_vector); - return v.sub(v_proj) + v.sub(v_proj) } function make_charges(n) {