xxxxxxxxxx
131
let gCircleSizeMax = 60;
let gCircleSizeMin = 10;
let gXSpeed = 2;
let gYSpeed = 4;
let gZSpeed = 1;
let gCircleArray = [];
let circlesMade = 0;
class BouncingCircle {
constructor(xpos, ypos, xSpeed, ySpeed, zSpeed, circleSize, ID) {
this.x = xpos;
this.y = ypos;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.zSpeed = zSpeed;
this.circleSize = circleSize;
this.ID = ID;
}
//give ball speed
update() {
this.x += this.xSpeed;
this.y += this.ySpeed;
this.circleSize += this.zSpeed;
}
checkWallCollisions() {
let r = this.circleSize / 2;
if (this.x < r || this.x > width - r) {
this.xSpeed = -this.xSpeed;
}
if (this.y < r || this.y > height - r) {
this.ySpeed = -this.ySpeed;
}
}
checkBounce() {
if (this.circleSize <= gCircleSizeMin) {
if (this.xSpeed > 0 || this.ySpeed > 0 ) {
this.zSpeed = -99/100 * this.zSpeed;
this.xSpeed = 99/100 * this.xSpeed;
this.ySpeed = 99/100 * this.ySpeed;
if (this.zSpeed > gZSpeed/2) {
duplicate(this)
}
}
else {
//this.zSpeed = 0;
//gCircleArray.splice(circle.ID, 1);
}
}
if (this.circleSize >= gCircleSizeMax) {
this.zSpeed = -this.zSpeed;
}
}
drawSelf() {
fill(255);
ellipse(this.x, this.y, this.circleSize, this.circleSize);
}
}
function duplicate(circle) {
//if (circlesMade < 4) {
//if (random(100) > 50) {
print("Double Trouble")
let xpos = circle.x;
if (xpos > width - gCircleSizeMax)
xpos = width - gCircleSizeMax
if (xpos < gCircleSizeMax)
xpos = gCircleSizeMax
let ypos = circle.y;
if (ypos > height - gCircleSizeMax)
ypos = height - gCircleSizeMax;
if (ypos < gCircleSizeMax)
ypos = gCircleSizeMax;
let xSpeed = circle.ySpeed;
let ySpeed = circle.xSpeed;
let zSpeed = gZSpeed;
let circleSize = gCircleSizeMin + 5;
let ID = gCircleArray.length;
let newCircle = new BouncingCircle(xpos, ypos, xSpeed, ySpeed, zSpeed, circleSize, ID)
gCircleArray.push(newCircle);
print(gCircleArray);
circlesMade++
//}
//}
if (gCircleArray.length >= 25) {
gCircleArray.splice(0, gCircleArray.length / 2)
}
}
function setup() {
gCircleArray.push(new BouncingCircle(25, 72, gXSpeed, gYSpeed, gZSpeed, 15, 0));
createCanvas(400, 400);
smooth();
}
function draw() {
background(0);
for (var i = 0; i < gCircleArray.length; i++) {
gCircleArray[i].update();
gCircleArray[i].drawSelf();
gCircleArray[i].checkWallCollisions();
gCircleArray[i].checkBounce();
}
if (gCircleArray.length == 0) {
gCircleArray.push(new BouncingCircle(25, 72, gXSpeed, gYSpeed, gZSpeed, 15, 0));
}
circlesMade = 0
}