Improving efficiency in Particle Systems
in
Programming Questions
•
3 years ago
I've been doing a bit of experimenting with applying some simple forces to particle systems and arraylists. I made this sketch:
http://wwww.openprocessing.org/visuals/?visualID=13048 but it becomes quite slow a bit too quick for my liking. I have a feeling the problem lies in the attached bit of code below - is there a faster way to compare the coordinates of a single particle against all the others, that is also not a complete pain to implement?
- void applyAttraction(ArrayList parts){
- for(int i=0; i<parts.size();i++){
- Particle p1 = (Particle)parts.get(i);
- for(int j=0;j<parts.size();j++){
- if(j!=i){
- Particle p2 = (Particle)parts.get(j);
- PVector dir = PVector.sub(p1.loc, p2.loc);
- float d = dir.mag();
- dir.normalize();
- float force = (g * p1.mass * p2.mass) / (d*d);
- dir.mult(force);
- if(!p2.dead()){
- p2.applyForce(dir);
- }
- if(p1.dead()){
- dir.mult(-1f);
- p1.applyForce(dir);
- }
- }
- }
- }
- }
- void applyForce(PVector force){
- force.div(mass);
- acc.add(force);
- }
1