xxxxxxxxxx
28
let bod1 = {};
let bod2 = [];
let G = 0.0001;
function setup() {
createCanvas(windowWidth, windowHeight);
bod1.pos = createVector(width / 2, height / 2);
bod1.vel = createVector(2, 1);
for (let i = 0; i < 3; i++) {
bod2[i] = createVector(random(0, width), random(0, height));
}
}
function draw() {
background(220);
ellipse(bod1.pos.x, bod1.pos.y, 30, 30);
for (let bod = 0; bod < bod2.length; bod++) {
ellipse(bod2[bod].x, bod2[bod].y, 30, 30);
}
for (let i = 0; i < bod2.length; i++) {
bod1.vel.sub(p5.Vector.add(bod2[i], bod1.pos).mult(G));
}
bod1.pos.add(bod1.vel);
}