xxxxxxxxxx
107
let grid = [];
let rows = 6;
let cols = 6;
let hitGround = false;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
for (let x = 0; x < rows; x++) {
for (let y = 0; y < cols; y++){
grid.push(new Square(x-cols/2,y-rows/2));
}
}
}
function draw() {
background(220);
// stroke(0);
rectMode(CORNER);
translate(width/2,height/2);
for (let i = 0; i < grid.length; i++){
grid[i].display();
grid[i].move();
}
}
class Square {
constructor(x,y){
this.x = x; // this.pos = createVector(); both x,y
this.y = y;
this.w = 100;
this.a = 0;
this.vx = 0;
this.vy = 0;
this.midVec = createVector(0,0);
this.g = 1;
this.fillCol = 255;
// this.speedPosition = random(-0.05,0.05); // this.vel = createVector(); both vx, vy
this.speedRotation = random(-3,3);
this.rig = 0.08
}
display(){
fill(this.fillCol)
rect(this.x*this.w,this.y*this.w,this.w,this.w);
}
move(){
this.w -= this.g;
this.g = this.g * 1.01;
if(!hitGround){
if(this.x <= this.midVec.x && this.y <= this.midVec.y ){
this.vx = random(-this.rig,0);
this.vy = random(-this.rig,0);
}
if(this.x >= this.midVec.x && this.y <= this.midVec.y){
this.vx = random(0,this.rig);
this.vy = random(-this.rig,0);
}
if(this.x <= this.midVec.x && this.y >= this.midVec.y){
this.vx = random(-this.rig,0);
this.vy = random(0,this.rig);
}
if(this.x >= this.midVec.x && this.y >= this.midVec.y){
this.vx = random(0,this.rig);
this.vy = random(0,this.rig);
}
}
if(this.w<20){
hitGround = true;
}
if(hitGround){
this.fillCol -= abs(this.vx*50);
this.g = 0;
this.a += this.speedRotation;
this.x += this.vx;
this.y += this.vy;
this.speedRotation = this.speedRotation * 0.1
this.vx = this.vx * 0.98;
this.vy = this.vy * 0.98;
}
translate((this.x + this.w)/2,(this.y + this.w)/2)
// rectMode(CENTER);
rotate(this.a);
translate(-(this.x + this.w)/2,-(this.y + this.w)/2)
}
}