You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
1.5 KiB

class Ball{
constructor(x, y, r, c){
this.maxAcc = 0.5;
this.r = r;
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.c = c;
}
attracted(){
let att = createVector(0, 0), G = 50, g = 3;
for (let a of attractions){
let distV = p5.Vector.sub(a.pos, this.pos);
distV.setMag(G * this.r * a.r / pow(this.pos.dist(a.pos), 2));
att.add(distV.div(this.r));
}
if (gravity) att.y += g;
this.acc = att;
this.acc.limit(this.maxAcc);
}
move(){
this.vel.add(this.acc);
this.pos.add(this.vel);
}
show(){
noStroke();
fill(this.c);
ellipse(this.pos.x, this.pos.y, this.r * 2);
}
debugInformation(){
let velX = p5.Vector.mult(this.vel, 10).x;
let velY = p5.Vector.mult(this.vel, 10).y;
let accX = velX + p5.Vector.mult(this.acc, 100).x;
let accY = velY + p5.Vector.mult(this.acc, 100).y;
push();
translate(this.pos.x, this.pos.y);
strokeWeight(2);
stroke(255, 0, 0);
fill(255, 0, 0);
line(0, 0, velX, velY);
translate(velX, velY);
rotate(this.vel.heading());
beginShape();
vertex(0, 0);
vertex(-10, 10);
vertex(-10, -10);
endShape();
rotate(-this.vel.heading());
translate(-velX, -velY);
stroke(0, 255, 0);
fill(0, 255, 0);
line(velX, velY, accX, accY);
translate(accX, accY);
rotate(this.acc.heading());
beginShape();
vertex(0, 0);
vertex(-10, 10);
vertex(-10, -10);
endShape();
rotate(-this.acc.heading());
translate(-accX, -accY);
pop();
}
}