We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Processing Monsters - CALL FOR SUBMISSIONS
Pages: 1 2 
Processing Monsters - CALL FOR SUBMISSIONS (Read 2703 times)
Re: Processing Monsters - CALL FOR SUBMISSIONS
Reply #15 - Dec 31st, 2008, 2:15pm
 
Post here and be judged I say.

And I mentioned something about some simple springs didn't I?

Quote:
Spring s0, s1;

void setup(){
 size(400, 400);
 s0 = new Spring(new PVector(0, 0), new PVector(100, 100));
 s1 = new Spring(s0.b, new PVector(200, 200));
}

void draw(){
 background(255);
 if(mousePressed){
   s1.b.x = mouseX;
   s1.b.y = mouseY;
 }
 s0.updateSpring();
 s1.updateSpring();
 line(s0.a.x, s0.a.y, s0.b.x, s0.b.y);
 line(s1.a.x, s1.a.y, s1.b.x, s1.b.y);
 ellipse(s0.a.x, s0.a.y, 20, 20);
 ellipse(s0.b.x, s0.b.y, 20, 20);
 ellipse(s1.a.x, s1.a.y, 20, 20);
 ellipse(s1.b.x, s1.b.y, 20, 20);
}

class Spring{
 float restLength;
 float length;
 float stiffness;
 float vx, vy;
 PVector moveBy;
 PVector a, b;
 Spring(PVector a, PVector b){
   this.a = a;
   this.b = b;
   stiffness = 0.5f;
   restLength = length = a.dist(b);
   moveBy = new PVector();
 }
 void updateSpring(){
   vx = b.x - a.x;
   vy = b.y - a.y;
   length = a.dist(b);
   float diff = 0;
   if(length > 0f){
     diff = (length - restLength) / length;
   }
   float mul = diff * stiffness;
   moveBy.x = -vx*mul;
   moveBy.y = -vy*mul;
   a.x -= moveBy.x;
   a.y -= moveBy.y;
   b.x += moveBy.x;
   b.y += moveBy.y;
 }
}



You can use these Spring objects to build tentacles and ragdolls. Be careful about the order you update their behaviour though - springs have a tendency to explode if not updated properly.
Pages: 1 2