move charge repelling stuff to its own file
This commit is contained in:
parent
fb6b8aa33c
commit
6cbdb04786
3 changed files with 46 additions and 41 deletions
|
@ -16,6 +16,7 @@ body {
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
</main>
|
</main>
|
||||||
|
<script src="thomson-problem.js"></script>
|
||||||
<script src="sketch.js"></script>
|
<script src="sketch.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
42
sketch.js
42
sketch.js
|
@ -19,52 +19,12 @@ function draw() {
|
||||||
|
|
||||||
make_lights();
|
make_lights();
|
||||||
if (physics) {
|
if (physics) {
|
||||||
move_charges();
|
move_charges(charges);
|
||||||
}
|
}
|
||||||
draw_charges(sphere_radius);
|
draw_charges(sphere_radius);
|
||||||
draw_sphere(sphere_radius, 25);
|
draw_sphere(sphere_radius, 25);
|
||||||
}
|
}
|
||||||
|
|
||||||
function move_charges() {
|
|
||||||
for (charge of charges) {
|
|
||||||
charge.acceleration.setMag(0);
|
|
||||||
}
|
|
||||||
for (let i = 0; i < charges.length; i += 1) {
|
|
||||||
for (let j = 0; j < i; j += 1) {
|
|
||||||
const displacement = p5.Vector.sub(
|
|
||||||
charges[i].position,
|
|
||||||
charges[j].position,
|
|
||||||
);
|
|
||||||
const acceleration_mag = 1 / displacement.mag() * 0.001;
|
|
||||||
let ai = displacement.copy().normalize().mult(acceleration_mag);
|
|
||||||
let aj = p5.Vector.mult(ai, -1);
|
|
||||||
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);
|
|
||||||
//project_onto_plane(charge.velocity, charge.position);
|
|
||||||
charge.position = charge.position.add(charge.velocity);
|
|
||||||
charge.position.normalize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function project_onto_unit_vector(v, unit_vector) {
|
|
||||||
let size = p5.Vector.dot(v, unit_vector);
|
|
||||||
return p5.Vector.mult(unit_vector, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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);
|
|
||||||
v.sub(v_proj)
|
|
||||||
}
|
|
||||||
|
|
||||||
function make_charges(n) {
|
function make_charges(n) {
|
||||||
charges = [];
|
charges = [];
|
||||||
for (let i = 0; i < n; i += 1) {
|
for (let i = 0; i < n; i += 1) {
|
||||||
|
|
44
thomson-problem.js
Normal file
44
thomson-problem.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
function move_charges(charges) {
|
||||||
|
for (charge of charges) {
|
||||||
|
charge.acceleration.setMag(0);
|
||||||
|
}
|
||||||
|
for (let i = 0; i < charges.length; i += 1) {
|
||||||
|
for (let j = 0; j < i; j += 1) {
|
||||||
|
const displacement = p5.Vector.sub(
|
||||||
|
charges[i].position,
|
||||||
|
charges[j].position,
|
||||||
|
);
|
||||||
|
let acceleration_mag = 1 / displacement.mag() * 0.001;
|
||||||
|
let ai;
|
||||||
|
if (acceleration_mag === Infinity) {
|
||||||
|
ai = createVector(random(-1, 1), random(-1, 1), random(-1, 1));
|
||||||
|
} else {
|
||||||
|
ai = displacement.copy();
|
||||||
|
}
|
||||||
|
ai = ai.normalize().mult(acceleration_mag);
|
||||||
|
let aj = p5.Vector.mult(ai, -1);
|
||||||
|
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.position = charge.position.add(charge.velocity);
|
||||||
|
charge.position.normalize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function project_onto_unit_vector(v, unit_vector) {
|
||||||
|
let size = p5.Vector.dot(v, unit_vector);
|
||||||
|
return p5.Vector.mult(unit_vector, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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);
|
||||||
|
v.sub(v_proj)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue