Object Oriented Programming With 3D Tile
// Object Oriented example in 3D Tile t1; void setup() { size (300, 300, P3D); noStroke(); t1 = new Tile(50, 50, 40); } void draw() { background(80); fill(200); t1.checkMouse(); t1.animate(); t1.display(); } 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() { translate(x,y); rotateY(thetaY); rectMode(CENTER); rect(0,0, wh, wh); } 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.01; // but not make the rotationSpeed less than 0 if (rotationSpeed < 0) { rotationSpeed = 0; } } } }
page revision: 1, last edited: 25 Nov 2009 23:12