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:
parent
f70370d05e
commit
2acbfffc5f
1 changed files with 7 additions and 8 deletions
15
sketch.js
15
sketch.js
|
@ -38,18 +38,16 @@ function move_charges() {
|
||||||
const acceleration_mag = 1 / displacement.mag() * 0.005;
|
const acceleration_mag = 1 / displacement.mag() * 0.005;
|
||||||
let ai = displacement.copy().normalize().mult(acceleration_mag);
|
let ai = displacement.copy().normalize().mult(acceleration_mag);
|
||||||
let aj = p5.Vector.mult(ai, -1);
|
let aj = p5.Vector.mult(ai, -1);
|
||||||
let ai_norm = project_onto_plane(ai, charges[i].position);
|
project_onto_plane(ai, charges[i].position);
|
||||||
|
project_onto_plane(aj, charges[j].position);
|
||||||
let aj_norm = project_onto_plane(aj, charges[j].position);
|
charges[i].acceleration.add(ai);
|
||||||
|
charges[j].acceleration.add(aj);
|
||||||
charges[i].acceleration.add(ai_norm);
|
|
||||||
charges[j].acceleration.add(aj_norm);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let i = 0; i < charges.length; i += 1) {
|
for (let i = 0; i < charges.length; i += 1) {
|
||||||
let charge = charges[i];
|
let charge = charges[i];
|
||||||
charge.velocity = charge.velocity.add(charge.acceleration);
|
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 = charge.position.add(charge.velocity);
|
||||||
charge.position.normalize();
|
charge.position.normalize();
|
||||||
}
|
}
|
||||||
|
@ -61,9 +59,10 @@ function project_onto_unit_vector(v, unit_vector) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Project `v` onto the plane normal to `unit_vector`
|
/// Project `v` onto the plane normal to `unit_vector`
|
||||||
|
/// Mutates `v`
|
||||||
function project_onto_plane(v, unit_vector) {
|
function project_onto_plane(v, unit_vector) {
|
||||||
let v_proj = project_onto_unit_vector(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) {
|
function make_charges(n) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue