Skip to content

Instantly share code, notes, and snippets.

@nervoussystem
Created May 20, 2010 23:01
Show Gist options
  • Select an option

  • Save nervoussystem/408241 to your computer and use it in GitHub Desktop.

Select an option

Save nervoussystem/408241 to your computer and use it in GitHub Desktop.
//save previous particle forces and velocities
ArrayList prevForces = new ArrayList();
ArrayList prevVelocities = new ArrayList();
for(int i=0;i<particles.size();++i) {
Particle p = (Particle) particles.get(i);
prevVelocities.add(p.velocity);
prevForces.add(p.force);
}
//compute estimate
for(int i=0;i<particles.size();++i) {
Particle p = (Particle) particles.get(i);
p.position += deltaT*p.velocity;
p.velocity += deltaT*p.force/p.mass;
}
computeForces();
for(int i=0;i<particles.size();++i) {
Particle p = (Particle) particles.get(i);
PVector prevV = (PVector) prevVelocities.get(i);
PVector prevF = (PVector) prevForces.get(i);
p.position += deltaT*(p.velocity+prevVelocity)/2.0;
p.velocity += deltaT*(p.force+prevForce)/p.mass/2.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment