xxxxxxxxxx
117
let x,y,i,j;
let l=[];
let sys=[];
let windowWidth=100,windowHeight=100;
function setup() {
createCanvas(windowWidth, windowHeight);
x=windowWidth/2,y= windowHeight/2;
for(let i=0;i<10;i++){
b=new ball(random(10,90),random(10,90),random(0.5,1.2),random(0.5,1.2),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);
}
}
function draw() {
if (frameCount===1){
capturer.start()
}
if (frameCount<100){
capturer.capture(canvas)
}
else if (frameCount===100){
capturer.save();
capturer.stop();
}
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<5 || this.pos.x>windowWidth-5){this.vel.x=-this.vel.x;}
if (this.pos.y<5 || this.pos.y>windowHeight-5){this.vel.y=-this.vel.y;}
this.pos.add(this.vel)
}
draw(){
stroke(this.r,this.g,this.b);
strokeWeight(1);
fill(this.b,this.r,this.g);
ellipse(this.pos.x,this.pos.y, 10, 10);
}
}
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,2),random(1,2),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);
}
/*function draw() {
if (frameCount===1){
capturer.start()
}
if (frameCount<100){
capturer.capture(canvas)
}
else if (frameCount===100){
capturer.save();
capturer.stop();
}
}
*/