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
This commit is contained in:
root 2025-04-24 12:26:57 +00:00
parent f70370d05e
commit 2acbfffc5f

View file

@ -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) {