More Object Oriented Programming With More 3D Tiles

Main sketch

// Object Oriented example in 3D
 
Tile t1;
Tile t2;
Tile t3;
Tile t4;
Tile t5;
Tile t6;
Tile t7;
Tile t8;
Tile t9;
Tile t10;
Tile t11;
Tile t12;
Tile t13;
Tile t14;
Tile t15;
Tile t16;
 
void setup() {
  size (300, 300, P3D);
  noStroke();
 
t1 =  new Tile(60, 60, 50);
t2 =  new Tile(120, 60, 50);
t3 =  new Tile(180, 60, 50);
t4 =  new Tile(240, 60, 50);
t5 =  new Tile(60, 120, 50);
t6 =  new Tile(120, 120, 50);
t7 =  new Tile(180, 120, 50);
t8 =  new Tile(240, 120, 50);
t9 =  new Tile(60, 180, 50);
t10 = new Tile(120, 180, 50);
t11 = new Tile(180, 180, 50);
t12 = new Tile(240, 180, 50);
t13 = new Tile(60, 240, 50);
t14 = new Tile(120, 240, 50);
t15 = new Tile(180, 240, 50);
t16 = new Tile(240, 240, 50);
 
}
 
void draw() {
  background(80);
  fill(200);
 
  t1.checkMouse();
  t1.animate();
  t1.display();
 
  t2.checkMouse();
  t2.animate();
  t2.display();
 
  t3.checkMouse();
  t3.animate();
  t3.display();
 
  t4.checkMouse();
  t4.animate();
  t4.display();
 
  t5.checkMouse();
  t5.animate();
  t5.display();
 
  t6.checkMouse();
  t6.animate();
  t6.display();
 
  t7.checkMouse();
  t7.animate();
  t7.display();
 
  t8.checkMouse();
  t8.animate();
  t8.display();
 
  t9.checkMouse();
  t9.animate();
  t9.display();
 
  t10.checkMouse();
  t10.animate();
  t10.display();
 
  t11.checkMouse();
  t11.animate();
  t11.display();
 
  t12.checkMouse();
  t12.animate();
  t12.display();
 
  t13.checkMouse();
  t13.animate();
  t13.display();
 
  t14.checkMouse();
  t14.animate();
  t14.display();
 
  t15.checkMouse();
  t15.animate();
  t15.display();
 
  t16.checkMouse();
  t16.animate();
  t16.display();
}

Tile.pde

class Tile {
  int x, y, wh;
  float thetaY, rotationSpeed;
 
  Tile(int centerX, int centerY, int squareSize) {
    thetaY = 0.0;
    rotationSpeed = 0.0;
    x = centerX;
    y = centerY;
    wh = squareSize;
  }
 
  void animate() {
    thetaY += rotationSpeed;
  }
 
  void display() {
    pushMatrix();
 
    translate(x,y);
    rotateY(thetaY);
    rectMode(CENTER);
    rect(0,0, wh, wh);
 
    popMatrix();
  }
 
  void checkMouse() {
    // Is the mouse within my original region?
    int h = wh / 2; // h = halfsize
    if (((mouseX > x-h) && (mouseX < x+h)) && ((mouseY > y-h) && (mouseY < y+h))) {
      // increase the rotationSpeed
      rotationSpeed += 0.02;
    } else {
      // if not, decrease the rotationSpeed (less than the increase)
      rotationSpeed -= 0.002;
      // but not make the rotationSpeed less than 0
      if (rotationSpeed < 0) {
        rotationSpeed = 0;
      }
    }
  }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License