Monday, June 20, 2011

springScratch

int numSprings = 5;
Spring2D[] s = new Spring2D[numSprings];
float gravity = 5.0;
float mass = 3.0;

float x,y;
float amp, h, b, a;

void setup() {

  size (600,600);
  //smooth();
  fill(255);
  stroke(255);
}

void draw() {
  background(0);
  for (int i = 0; i < numSprings; i++) {
    s[i] = new Spring2D(width/2, i*(height/numSprings), mass, gravity);
  }

  //not sure...
  for(int i = 0; i < numSprings; i++) {     
    amp = sqrt(1/random(0,150))*150;
    h = i * 100/amp;

    //b = amp/3 * 100;
    //a = 100;  


    x = amp + 10;
    y = amp + 10;


    for (int j=1; j < numSprings; j++) {
      //println(amp);


      s[0].update(x, y, (amp/10));  //(amp/10)
      s[0].display(x,y, (amp/10));


      s[j].update(s[j-1].x, s[j-1].y, (amp/10));

      s[j].display(mouseX-s[j-1].x, mouseY-s[j-1].y, (amp/10));
    }
  }


  //setup();
  //stroke(255);
}

class Spring2D {
  float vx, vy;
  float x, y;
  float gravity;
  float mass;
  float radius = 5;
  float stiffness = 0.8;
  float damping = 0.9;
  //color c = color(0, 126, 255, 250);


  Spring2D(float xpos, float ypos, float m, float g) {
    x = xpos;
    y = ypos;
    mass = m;
    gravity = g;
  }

  void update(float targetX, float targetY, float incomingMass) {

    mass = incomingMass;
    float forceX = (targetX - x) * stiffness;
    float ax = forceX / mass;
    vx = damping * (vx + ax);
    x += vx;
    float forceY = (targetY - y) * stiffness;
    forceY += gravity;
    float ay = forceY / mass;
    vy = damping * (vy + ay);
    y += vy;
  }

  void display(float nx, float ny, float targetSize) {

    radius = targetSize;
    //resetMatrix();
    pushMatrix();
    float mx = constrain(x, 0, width);
    float my = constrain(y, 0, height);
    ellipse(x, y, radius*2, radius*2);


    translate(radius, radius);
    popMatrix();
    // float mx = constrain(x, 0, width);
    //float my = constrain(y, 0, height);
    //strokeWeight(random(10,30));
    line(mx, my, nx, ny);
  }
}

info info

submitted by: sevenspiral1
views: 564
A set of jangling sharp pins disturbed by the magnet of your mouse!

Tags:

comments comment

loading loading...

 

Add a comment: