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.