xxxxxxxxxx
80
let x,y,i,j;
let l=[];
let sys=[];
function setup() {
createCanvas(windowWidth, windowHeight);
x=windowWidth/2,y= windowHeight/2;
}
function draw() {
background(0);
for(i=0;i<l.length;i++){
l[i].move();
l[i].draw();
}for(i=0;i<sys.length;i++){
sys[i].update();
}
}
class ball {
constructor(tx,ty,txi,txj,tr,tg,tb) {
this.pos=createVector(tx,ty)
this.vel=createVector(txi,txj)
this.xi=txi;
this.xj=txj;
this.r = tr;
this.g = tg;
this.b = tb;
}
move(txi,txj){
if (this.pos.x<10 || this.pos.x>windowWidth-10){this.vel.x=-this.vel.x;}
if (this.pos.y<10 || this.pos.y>windowHeight-10){this.vel.y=-this.vel.y;}
this.pos.add(this.vel)
}
draw(){
stroke(this.r,this.g,this.b);
strokeWeight(2);
fill(this.b,this.r,this.g);
ellipse(this.pos.x,this.pos.y, 20, 20);
}
}
class System {
constructor(tball1, tball2) {
this.ball1=tball1;
this.ball2=tball2;
this.collide=0;
this.wait=10;
}
update(){
this.wait-=1;
if(p5.Vector.dist(this.ball1.pos,this.ball2.pos)<20){
this.collide=1;
if (this.collide && this.wait<0){
this.ball1.vel.rotate(HALF_PI).mult(-1);
this.ball2.vel.rotate(HALF_PI);
//this.ball2.vel.mult(-1);
this.collide=0;
this.wait=30;
}
}
}
}
function mouseClicked() {
b=new ball(mouseX,mouseY,random(1,5),random(1,5),random(50,200),random(50,200),random(50,200));
if(l.length>=1){
for(let i=0;i<l.length;i++){
let s=new System(b,l[i]);
sys.push(s);
}
}l.push(b);
// prevent default
return false;
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}