physics oopsie: coulomb's law is 1/r^2 not 1/r

This commit is contained in:
root 2025-04-27 07:57:42 +00:00
parent 2a29ee0c51
commit 5e04ad107f
2 changed files with 12 additions and 6 deletions

View file

@ -70,7 +70,7 @@ function draw() {
camera.centerZ = 0; camera.centerZ = 0;
make_lights(); make_lights();
if (physics) move_charges(charges); if (physics) move_charges(charges, 8e-4);
draw_charges(sphere_radius); draw_charges(sphere_radius);
if (skeleton) draw_skeleton(sphere_radius); if (skeleton) draw_skeleton(sphere_radius);

View file

@ -1,4 +1,4 @@
function move_charges(charges) { function move_charges(charges, force_constant) {
for (let charge of charges) { for (let charge of charges) {
charge.acceleration.setMag(0); charge.acceleration.setMag(0);
} }
@ -8,15 +8,21 @@ function move_charges(charges) {
charges[i].position, charges[i].position,
charges[j].position, charges[j].position,
); );
let acceleration_mag = 1 / displacement.mag() * 0.001; const force_mag = 1 / displacement.magSq() * force_constant;
// XXX possible extension: divide by charge's mass
const ai_mag = force_mag;
const aj_mag = force_mag;
let ai; let ai;
if (acceleration_mag === Infinity) { let aj;
if (force_mag === Infinity) {
ai = p5.Vector.random3D(); ai = p5.Vector.random3D();
aj = p5.Vector.random3D();
} else { } else {
ai = displacement.copy().normalize(); ai = displacement.copy().normalize();
aj = ai.copy().mult(-1);
} }
ai.mult(acceleration_mag); ai.mult(ai_mag);
let aj = p5.Vector.mult(ai, -1); aj.mult(aj_mag);
project_onto_plane(ai, charges[i].position); project_onto_plane(ai, charges[i].position);
project_onto_plane(aj, charges[j].position); project_onto_plane(aj, charges[j].position);
charges[i].acceleration.add(ai); charges[i].acceleration.add(ai);