xxxxxxxxxx
71
let p1;
let p2;
let sc = 150;
let v1;
let v2;
let dT = 0.0001;
const TERM_LIM = 10;
function setup() {
createCanvas(400, 400);
p1 = createVector(0.3);
p2 = createVector(0);
v1 = createVector(0);
v2 = createVector(0.5);
}
function draw() {
background(250);
translate(width / 2, height / 2);
noFill();
stroke(0);
strokeWeight(2);
ellipse(0, 0, 2*sc, 2*sc);
noStroke();
fill(255, 0, 0);
ellipse(cos(p1.x*TWO_PI) * sc, sin(p1.x*TWO_PI) * sc, 15, 15);
fill(0, 255, 0);
ellipse(cos(p2.x*TWO_PI) * sc, sin(p2.x*TWO_PI) * sc, 15, 15);
for(let mi = 0;mi<100;mi++){
//stroke(0,2);
//line(cos(p1.x*TWO_PI) * sc, sin(p1.x*TWO_PI) * sc,cos(p2.x*TWO_PI) * sc, sin(p2.x*TWO_PI) * sc)
/*
line(0, 0, sc, 0);
noStroke();
fill(255, 0, 0);
ellipse(p1.x * sc, 0, 15, 15);
fill(0, 255, 0);
ellipse(p2.x * sc, 0, 15, 15);
*/
let f = fullForce(p2, p1);
f.mult(dT)
v1.add(f);
v2.sub(f);
p1.add(v1.copy().mult(dT));
p2.add(v2.copy().mult(dT));
if (p1.x<0){
p1.add(createVector(1));
} else if (p1.x>1){
p1.add(createVector(-1));
}
if (p2.x<0){
p2.add(createVector(1));
} else if (p2.x>1){
p2.add(createVector(-1));
}
}
}
function fullForce(pa, pb) {
let f = p5.Vector.sub(pa, pb).setMag(1 / (pow(p5.Vector.sub(pa, pb).mag(), 2) + 1));
for (let ni = 1;ni<TERM_LIM;ni++){
f.add(p5.Vector.sub(pa, pb).setMag(1 / (pow(p5.Vector.sub(pa, pb).mag()+ni, 2) + 1)));
f.sub(p5.Vector.sub(pa, pb).setMag(1 / (pow(p5.Vector.sub(pa, pb).mag()-ni, 2) + 1)));
}
return f;
}