xxxxxxxxxx
86
let rb;
let rb2;
let rbs = [];
let numBouncers= 10;
function setup() {
createCanvas(800, 800);
angleMode(DEGREES);
ellipseMode(CENTER);
for(let i = 0; i < numBouncers; i = i + 1) {
let w = random(10,90);
let h = w;
rbs.push(new RotatorBouncer(
rb = new RotatorBouncer(
random(w,width-w), // x
random(h,height-h), // y
w, // width
h, // height
random(-4,1), // xSpeed
random(-4,1), // ySpeed
random(1,1) // rSpeed
)));
rbs.push(rb);
}
}
function draw() {
//background(220);
for(let i = 0; i < rbs.length; i = i + 1) {
fill(mouseX,mouseY,random(255), random(0,80));
stroke(0,random(0,80));
rbs[i].update();
}
}
function mouseDragged() {
let w = random(10,60);
let h = w;
let rb = new RotatorBouncer(
random(0,800), //x
random(0,800), //y
w, //width
h, //height
random(-2,2), //xSpeed
random(-2,1), //ySpeed
random(1,1) //rSpeed
);
rbs.push(rb);
}
class RotatorBouncer {
constructor(x,y,w,h,xSpeed,ySpeed,rSpeed){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.rSpeed = rSpeed;
this.a=0;
}
update(){
push();
translate(this.x,this.y);
rotate(this.a);
ellipse(0,0,this.w,this.h);
pop();
this.a = this.a + this.rSpeed;
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
if(this.x >= width-this.w/2 || this.x <= 0-this.w/2) {
this.xSpeed = this.xSpeed * -1;
}
if(this.y >= height-this.h/2 || this.y <= 0-this.h/2) {
this.ySpeed = this.ySpeed * -1;
}
}
}