xxxxxxxxxx
65
let rb;
let rb2;
function setup() {
createCanvas(400, 400);
rb = new RotatorBouncer(
width/2, // x
height/2, // y
50, // width
50, // height
2, // xspeed
3, // yspeed
1 // rspeed
);
rb2 = new RotatorBouncer(
width/4, // x
height/4, // y
150, // width
50, // height
3, // xspeed
4, // yspeed
2 // rspeed
);
}
function draw() {
background(220);
angleMode(DEGREES);
rectMode(CENTER);
rb.update();
rb2.update();
}
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);
rect(0,0,this.w,this.h);
pop();
this.x = this.x + this.xSpeed;
this.y = this.y + this.ySpeed;
this.a = this.a + this.rSpeed;
if(this.x >= width-this.w/2 || this.x <= this.w/2) {
this.xSpeed = this.xSpeed * -1;
}
if(this.y >= height-this.h/2 || this.y <= this.h/2) {
this.ySpeed = this.ySpeed * -1;
}
}
}