Monday, June 20, 2011

Bacterium

// just type here
// and click "save" when your done


//this is going to be the main stage - the black blob biological asteroid game
// tribute game to Ira Greenberg and Springy Dude, from Creative Coding

Hero myHero1;

Asteroid[] asteroid;
int totalAsteroids = 0;


void setup() {

  size (500, 500);
  colorMode(HSB);

  asteroid = new Asteroid[40];

  myHero1 = new Hero(color(255,0,0), 0, 100);
}

void draw() {
  background(255,255,255);
  myHero1.drawShape();
  myHero1.moveShape();


  for (int i=0; i<asteroid.length; i++) {
    asteroid[totalAsteroids] = new Asteroid(color(i, i*50, i*50, i*10), i*1);
  }
  totalAsteroids++;
  if (totalAsteroids >= asteroid.length) {
    totalAsteroids = 0;
  }

  for (int i=0; i<totalAsteroids; i++) {
    asteroid[i].drawAsteroid();
    asteroid[i].moveAsteroid();

    if (myHero1.intersect(asteroid[i])) {
      myHero1.highlight(0, 0, 200);
    }
  }
}

class Asteroid {


  float ellipseSpeed;
  float x, y;
  color ellipseCol;

  float r;


  Asteroid(color tempC, int tempEllipseSpeed) {

    ellipseCol = (tempC);
    ellipseSpeed = tempEllipseSpeed;
    x = width;
    y = random(0, height);
    r = 8;
  }

  void drawAsteroid() {
    for (int i = 2; i < r; i++) {
      smooth();
      fill(ellipseCol);
      pushMatrix();
      ellipse(x+random(0,6)*4, y+i*4, r, r);
      popMatrix();
    }
  }


  void moveAsteroid() {

    x -= ellipseSpeed;
    ellipseSpeed = ellipseSpeed * 0.75;
  }
}

// void explodeAsteroid()

class Hero {
  float centerX, centerY;
  float radius, rotAngle;
  float accelX, accelY;
  float springing, damping;
  int nodes;
  float nodeStartX[];
  float nodeStartY[];
  float nodeX[];
  float nodeY[];
  float angle[];
  float frequency[];
  float organicConstant;
  color c;

  Hero(color tempC, float tempXpos, float tempYpos) {

    c = tempC;

    radius = 30;
    rotAngle = -90;
    accelX = 5;
    accelY = 5;
    springing = .0085;
    damping = .98;
    //corner nodes
    nodes = 5;
    nodeStartX = new float[nodes];
    nodeStartY = new float[nodes];
    nodeX = new float[nodes];
    nodeY = new float[nodes];
    angle = new float[nodes];
    frequency = new float[nodes];
    //soft body dynamics
    organicConstant = 1;
    centerX = tempXpos;
    centerY = tempYpos;
    for (int i=0; i<nodes; i++) {
      frequency[i] = random(5,12);
    }
  }

  void drawShape() {
    //calculate node starting points
    for (int i=0; i<nodes; i++) {
      nodeStartX[i] = centerX+cos(radians(rotAngle))*radius;
      nodeStartY[i] = centerY+sin(radians(rotAngle))*radius;
      rotAngle += 360.0/nodes;
    }

    //draw polygon
    curveTightness(organicConstant);
    fill(c);
    beginShape();
    for (int i=0; i<nodes; i++) {
      curveVertex(nodeX[i], nodeY[i]);
    }
    for (int i=0; i<nodes-1; i++) {
      curveVertex(nodeX[i], nodeY[i]);
    }
    endShape();
  }

  void moveShape() {
    //move center point
    float deltaX = mouseX-centerX;
    float deltaY = mouseY-centerY;

    //create springing effect
    deltaX *= springing;
    deltaY *= springing;
    accelX += deltaX;
    accelY += deltaY;

    //move polygon center
    centerX += accelX;
    centerY += accelY;

    //slow down springing
    accelX *= damping;
    accelY *= damping;

    //change curve tightness
    organicConstant = 1-((abs(accelX)+abs(accelY))*.1);

    //move nodes
    for (int i=0; i<nodes; i++) {
      nodeX[i] = nodeStartX[i]+sin(radians(angle[i]))*(accelX*2);
      nodeY[i] = nodeStartY[i]+sin(radians(angle[i]))*(accelY*2);
      angle[i] += frequency[i];
    }
  }

  void highlight(int hX, int hY, int hR) {
    int highColor = color(120, 150, 180);
    hX = int(centerX);
    hY = int(centerY);

    noFill();
    ellipse(hX, hY, hR, hR);
    ellipse(hX, hY, hX - hR/2, hX - hR/2);
    ellipse(hX, hY, hX - hR/4, hX - hR/4);
    ellipse(hX, hY, hX - hR/8, hX - hR/8);
    //ellipse(hX, hY , hR/2, hR/2);

    //if (hR > 8){
    //  highlight(hX + hR/2, hY, hR/2);
    //  highlight(hX - hR/2, hY, hR/2);
    //  highlight(hX, hY + hR/2, hR/2);
    //  highlight(hX, hY - hR/2, hR/2);
    //}
  }

  boolean intersect(Asteroid d) {
    float distance = dist(centerX, centerY, d.x, d.y);
    if (distance < radius + d.r) {
      return true;
    } 
    else {
      return false;
    }
  }
}

info info

submitted by: sevenspiral1
views: 1303
A tribute to Ira Greenberg, written mostly in a Peterborough coffee shop, so perhaps should also be dedicated to coffee

Tags:

comments comment

loading loading...

 

Add a comment: