Lab 1 :: Applied OOP
<< back to Procedural Morphology homepage
Class notes
Example / Create a ball on mouseClick
// following array will hold our ball instances
// we'll keep track of how many balls there are with the int currentBall
Ball[] ballies = new Ball[500];
int currentBall = 0;
// setup
//........................................................
void setup() {
size(200, 200);
background(150);
smooth();
}
// draw
//........................................................
void draw() {
background(150);
// use a loop to cycle through and tell all the balls to update
for (int i=0; i<currentBall; i++) {
if (i == currentBall -1) { // check to see if this is the last ball
ballies[i].update(true);
} else {
ballies[i].update(false);
}
}
}
// mousePressed()
//........................................................
void mousePressed() {
ballies[currentBall] = new Ball(
mouseX, // x position
mouseY, // y postion
random(0.5,1.5), // x speed
random(0.5,1.5) // y speed
); // end of ball arguments
currentBall++;
}
// ball class
//........................................................
class Ball {
// ball fields
float x;
float y;
float speedX;
float speedY;
// ball constructor
Ball(float _x, float _y, float _speedX, float _speedY) {
x = _x;
y = _y;
speedX = _speedX;
speedY = _speedY;
}
// ball methods
void update(boolean isLast) {
position();
checkCollisions();
drawBall(isLast);
}
void position() {
x += speedX;
y += speedY;
}
void drawBall(boolean isLast) {
if (isLast) {
stroke (255,0,0);
} else {
stroke(0);
}
strokeWeight(5);
ellipse(x, y, 10, 10);
}
void checkCollisions() {
if(x > width || x < 0) {
speedX *= -1;
}
if(y > height || y < 0) {
speedY *= -1;
}
}
}
Example / Using push and popmatrix within the ball
// following array will hold our ball instances
Ball[] ballies = new Ball[15];
// setup
//........................................................
void setup() {
size(200, 200);
background(150);
smooth();
// use a loop to fill the array with instances of the ball object
for (int i=0; i<ballies.length; i++) {
ballies[i] = new Ball(
random(width), // x position
random(height), // y postion
random(0.5,1.5), // x speed
random(0.5,1.5) // y speed
); // end of ball arguments
}
}
// draw
//........................................................
void draw() {
background(150);
// use a loop to cycle through and tell all the balls to update
for (int i=0; i<ballies.length; i++) {
ballies[i].update();
}
}
// ball class (notice that it has not changed since previous example)
//........................................................
class Ball {
// ball fields
float x;
float y;
float speedX;
float speedY;
// ball constructor
Ball(float _x, float _y, float _speedX, float _speedY) {
x = _x;
y = _y;
speedX = _speedX;
speedY = _speedY;
}
// ball methods
void update() {
position();
checkCollisions();
drawBall();
}
void position() {
x += speedX;
y += speedY;
}
void drawBall() {
stroke(0);
// push the matrix so each ball can draw itself
pushMatrix();
translate(x,y);
rotate( atan2(speedY, speedX) ); // atan2 gives rotation based on an (Y, X) direction
strokeWeight(2);
line(0,0,20,0);
line(20,0, 18, 2);
line(20,0, 18, -2);
strokeWeight(5);
ellipse(0, 0, 10, 10);
popMatrix();
}
void checkCollisions() {
if(x > width || x < 0) {
speedX *= -1;
}
if(y > height || y < 0) {
speedY *= -1;
}
}
}