custom charge magnitude
also: s/charge/particle to reduce confusion
This commit is contained in:
parent
5e04ad107f
commit
642d1c5cdd
3 changed files with 54 additions and 48 deletions
|
@ -1,14 +1,18 @@
|
|||
function move_charges(charges, force_constant) {
|
||||
for (let charge of charges) {
|
||||
charge.acceleration.setMag(0);
|
||||
function move_particles(particles, force_constant) {
|
||||
for (let particle of particles) {
|
||||
particle.acceleration.setMag(0);
|
||||
}
|
||||
for (let i = 0; i < charges.length; i += 1) {
|
||||
for (let i = 0; i < particles.length; i += 1) {
|
||||
for (let j = 0; j < i; j += 1) {
|
||||
const displacement = p5.Vector.sub(
|
||||
charges[i].position,
|
||||
charges[j].position,
|
||||
particles[i].position,
|
||||
particles[j].position,
|
||||
);
|
||||
const force_mag = (
|
||||
particles[i].charge * particles[j].charge
|
||||
/ displacement.magSq()
|
||||
* force_constant
|
||||
);
|
||||
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;
|
||||
|
@ -23,16 +27,16 @@ function move_charges(charges, force_constant) {
|
|||
}
|
||||
ai.mult(ai_mag);
|
||||
aj.mult(aj_mag);
|
||||
project_onto_plane(ai, charges[i].position);
|
||||
project_onto_plane(aj, charges[j].position);
|
||||
charges[i].acceleration.add(ai);
|
||||
charges[j].acceleration.add(aj);
|
||||
project_onto_plane(ai, particles[i].position);
|
||||
project_onto_plane(aj, particles[j].position);
|
||||
particles[i].acceleration.add(ai);
|
||||
particles[j].acceleration.add(aj);
|
||||
}
|
||||
}
|
||||
for (let charge of charges) {
|
||||
charge.velocity = charge.velocity.add(charge.acceleration);
|
||||
charge.position = charge.position.add(charge.velocity);
|
||||
charge.position.normalize();
|
||||
for (let particle of particles) {
|
||||
particle.velocity = particle.velocity.add(particle.acceleration);
|
||||
particle.position = particle.position.add(particle.velocity);
|
||||
particle.position.normalize();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue