// Manager object for particles moved with Verlet Integration methods // Methods are kept separate for optimisation // Call verlet(), then springs(), then attractions(), then pin particles, then constrain any springs, then manage collisions // this order is usually fairly stable - instability explosions can happen but this ain't RK4 so live with it - plus check for typos, they are a common cause of instability class Physics{ float gravityX, gravityY, damping; ArrayList p; ArrayList s; ArrayList g; // Constructor Physics(){ p = new ArrayList(); s = new ArrayList(); g = new ArrayList(); damping = 0.99f; gravityX = 0.0f; gravityY = 0.0f; } // Move particles void verlet(){ for(int i = 0; i < p.size(); i++){ Particle temp = (Particle)p.get(i); temp.verlet(); } } // Update springs void springs(){ for(int i = 0; i < s.size(); i++){ Spring temp = (Spring)s.get(i); temp.updateSpring(); } } // Add a particle to the system and return a reference Particle addParticle(float x, float y){ Particle temp = new Particle(x, y, this); p.add(temp); return temp; } // Add a spring to the system and return a reference Spring addSpring(Point a, Point b){ Spring temp = new Spring(a, b); s.add(temp); return temp; } // Debug drawings void drawParticles(){ for(int i = 0; i < p.size(); i++){ Particle temp = (Particle)p.get(i); ellipse(temp.x, temp.y, 10, 10); } } void drawSprings(){ for(int i = 0; i < s.size(); i++){ Spring temp = (Spring)s.get(i); line(temp.a.x, temp.a.y, temp.b.x, temp.b.y); } } }