Wednesday, 15 September 2010

Day 15 - back to processing

I seem to be doing my best work hacking other people's code lately, so here's my minor alterations to Lee Freeman's bouncing balls. I've added some small changes to the balls' appearance over time.



I really wanted to make the balls path curve but that didn't turn out to be so easy, but I did make something kind mental instead, today I might try to work out why it's so bonkers. That code is below, after the normal code.


Variation on Lee's balls


Point[] myPoint = new Point[100];

void setup() {

 size(400, 400);

 for (int i= 0; i < myPoint.length; i++) {

myPoint[i] = new Point(0, 100, random(0.1, 2), random(0.1, 2));

 }

}

void draw() {

  background(mouseY, mouseY, mouseX);

  for (int i= 0; i < myPoint.length; i++) {

  myPoint[i].bounce();

  myPoint[i].display();

  }

}

class Point {

  color c;

  float xpos;

  float ypos;

  float xspeed;

  float yspeed;

  int[] bigs = {0, 5, 10, 15, 20, 15, 10, 5};

  int i;
  int t;

  Point(float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {

    xpos = tempXpos;

    ypos = tempYpos;

    xspeed = tempXspeed;

    yspeed = tempYspeed;

  }

  void display() {

    int k = (t*10) % 255;
    int l = abs(255-t*10);
    stroke(k, l);

    fill((10 + t*7)%255, (10 + t*11)%255, abs(255 - t*5), abs(255-t*10));

    smooth();

    ellipse(xpos, ypos, 10 + bigs[i], 10 + bigs[i]);

  }

  void bounce() {

    xpos = xpos + xspeed;

    if (xpos > width) {

      xspeed = xspeed * -1;
    
      t++;
      if (t%5 == 0) {
      i = (i+1)%8;
      }

    }

    if (xpos < 0) {

      xspeed = xspeed * -1;
    
      t++;
      if (t%5 == 0) {
      i = (i+1)%8;
      }
    }

    ypos = ypos - yspeed;

    if (ypos > height) {

      yspeed = yspeed * -1;
    
      t++;
      if (t%5 == 0) {
      i = (i+1)%8;
      }

    }

    if (ypos < 0) {

      yspeed = yspeed * -1;
    
      t++;
      if (t%5 == 0) {
      i = (i+1)%8;
      }
    
    }

  }

}


******************************************************

Bonkers but nice 


Point[] myPoint = new Point[100];

void setup() {

 size(400, 400);
 frameRate(20);

 for (int i= 0; i < myPoint.length; i++) {

myPoint[i] = new Point(0, 100, random(0.1, 2), random(0.1, 2));

 }

}

void draw() {

  background(mouseY, mouseY, mouseX);

  for (int i= 0; i < myPoint.length; i++) {

  myPoint[i].bounce();

  myPoint[i].display();

  }

}

class Point {

  color c;

  float xpos;

  float ypos;

  float xspeed;

  float yspeed;
  
  int[] bigs = {0, 5, 10, 15, 20, 15, 10, 5};
  
  int i;
  int t;
  
  Point(float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {

    xpos = tempXpos;

    ypos = tempYpos;

    xspeed = tempXspeed;

    yspeed = tempYspeed;

  }

  void display() {

    int k = (t*10) % 255;
    int l = abs(255-t*10);
    stroke(k, l);

    fill((10 + t*7)%255, (10 + t*11)%255, abs(255 - t*5), abs(255-t*10));

    smooth();

    ellipse(xpos, ypos, 10 + bigs[i], 10 + bigs[i]);

  }

  void bounce() {

    xpos = abs(10*(tan(xpos))) + xspeed;

    if (xpos > width) {

      xspeed = xspeed * -1;
      
      t++; 
      if (t%5 == 0) {
      i = (i+1)%8;
      }

    }

    if (xpos < 0) {

      xspeed = xspeed * -1;
      
      t++; 
      if (t%5 == 0) {
      i = (i+1)%8;
      }
    }

    ypos = ypos - yspeed;

    if (ypos > height) {

      yspeed = yspeed * -1;
      
      t++; 
      if (t%5 == 0) {
      i = (i+1)%8;
      }

    }

    if (ypos < 0) {

      yspeed = yspeed * -1;
      
      t++; 
      if (t%5 == 0) {
      i = (i+1)%8;
      }
      
    }

  }

}

No comments:

Post a Comment