Bending with particle/springs?
in
Contributed Library Questions
•
2 years ago
I am trying to use the Traer physics library for simulating the behaviour of a bending stick. That is, a stick which is fixed in one end and has a direction (e.g. along the x-axis) that then starts to bend with gravity.
I had the idea that, additional to springs between neighbouring particles, I could achieve the bending effect by having a spring between every particle and the particle after the neighbour as well. Does not seem to work though?
I have attached the non-working code below.
Any ideas would be greatly appreciated!
- //traer test simple
import traer.physics.*;
ParticleSystem physics;
Particle[] particles;
int TOTAL = 20; //total amount of particles
int SCALE = 20; //distance between particles
float SPRING_STRENGTH = 0.2;
float SPRING_DAMPING = 0.1;
void setup(){
size(500,500);
stroke(255);
physics = new ParticleSystem(0.1, 0.01);
particles = new Particle[TOTAL];
for (int i = 0; i < TOTAL; i++){
particles[i] = physics.makeParticle(.3, i * SCALE + 50, 50, 0.0);
if(i!=0) physics.makeSpring(particles[i-1], particles[i], SPRING_STRENGTH, SPRING_DAMPING, SCALE);
if(i > 2) physics.makeSpring(particles[i-2], particles[i], SPRING_STRENGTH*2, SPRING_DAMPING, SCALE*2);
if(i < 2) particles[i].makeFixed();
}
}
void draw(){
background(0);
physics.tick();
for (int i = 0; i < TOTAL-1; i++){
line(particles[i].position().x(), particles[i].position().y(), particles[i+1].position().x(), particles[i+1].position().y());
}
}
1