openobject.org

Processing project week 9

From Physical Programming

//haydns idea.

int numCircles = 10; //this is not yet used by the code but i may want to callthis step later. Circles circle1 = new Circles(50, 50, 1, 5, 50); Circles circle2 = new Circles(50, 50, 1, 4, 35); Circles circle3 = new Circles(50, 50, 1, 5, 20); //sets up the amount of cirlces and their names that will be called when they are referecnced.

//this next section sets up the screen.

void setup() {

 size(800, 800);  //size of screen 800x800 pixals
 smooth();        //smooths the visual apearencenessicarry for any movement.
 frameRate(30);   //assigns a frame rate to the animation.
 fillpositionCircles(); //calls the radomisation function for the x and y starting points.
 velocitychange();      // calls the randomisation function for the x velocity and the y velocity.

}

//this section draws the functions to screen. //this animantion loops through this draw funtion, this means that the movement that is shown on screen - //is simply the screen refreshing and the circles being drawn is a new position along a vector equation, - //giving the imoression of movement.

void draw() {

 background(125);   //draws the background using the grayscale value 125
 noStroke();        //removes the outlines from all the objects
 drawCircles();     //calls the drawCirlcles function
 moveCircles();     //calls the moveCircles function
 bounceCircles();   //calls the bounceCircles function

}

//this class is sets up the bassis for all the objects, as they all have the same basic //propertise with varring values.

class Circles {

 int xpos, ypos, xvel, yvel, radius; //outlines what the numbers are going to relate to
 Circles() {  
 } 
 Circles(int x, int y, int xv, int yv, int r) {  
   xpos = x;
   ypos = y;
   xvel = xv;
   yvel = yv;
   radius = r;
 }
 void fillpositionvalues() {    //called earlier this function replaces the starting x 
                                //and y co-ords with randomly generated numbers that 
                                //fall within the maximum size of the window.
   xpos = int(random(width));   //writing the function this way allows the function to 
                                //update if any changes are made to the windows makeup.
   ypos = int(random(height)); 
 } 
 void randomvelocity() {        //called earlier this function replaces the starting x  
                                //and y velocity with randomly generated numbers that 
                                //fall within the (-5,5) size of the window.
   xvel = int(random(-5, 5));
   yvel = int(random(-5, 5));   //as the finctions are writen with a int relation they only return whole numbers.
 }
 void moveCircle() {   //this function simply adds the x and y velocities to the x and y co-ords.
                       //giving a new point for the circles once the screen is refreshed (see void draw)
   xpos += xvel;
   ypos += yvel; 
 } 
 void drawCircle() {   //this function gives the object the shape in ths case a ellipse
   ellipse(xpos, ypos, radius, radius);
 }

//the following function makes the circles not exceded the boundries of the screen and inturn changes //their direction. //special note, the objects may exceded the screen on 1 side and bounce short of the edge on the other, //this is due to the fact that their is 2 radiuses(see above ellipse) 1 in the x and 1 in the y direction.

 void bounceCircle() {
   if (xpos > width - (radius / 2) || xpos < radius / 2) {   //the || sign means or
     xvel *= -1;
   }
   if (ypos > height - (radius / 2) || ypos < radius / 2) {
     yvel *= -1;
   }
 }  

}

//all the following functions are called by the void draw function and all relate to a different aspect //of the class structure

void fillpositionCircles() { //calls the random generator for co-ords for all the circles.

 circle1.fillpositionvalues();
 circle2.fillpositionvalues();
 circle3.fillpositionvalues();

}

void velocitychange() { //calls the random generator for velocties for all the circles.

 circle1.randomvelocity();
 circle2.randomvelocity();
 circle3.randomvelocity();

}

void drawCircles() { //calls the ellipse function that gives them their appearence.

 circle1.drawCircle();
 circle2.drawCircle();
 circle3.drawCircle();

}

void moveCircles() { //calls the change in postition function.

 circle1.moveCircle();
 circle2.moveCircle();
 circle3.moveCircle(); 

}

void bounceCircles() { //calls the bounce function.

 circle1.bounceCircle();
 circle2.bounceCircle();
 circle3.bounceCircle();

}

//by writing the code in this way the program seeimingly runs many small aspects simultanously and therefor //problems are far easier to identify.