Why bodies seems broken on second loop example, am trying to optimize my planetary system to support more bodies.
for(Body body : bodies){
PVector totalForce = new PVector();
for(Body other : bodies){
if(body != other){
PVector fxy = body.attraction(other);
totalForce.x += fxy.x;
totalForce.y += fxy.y;
}
}
body.vel.x += totalForce.x / body.mass * timestep;
body.vel.y += totalForce.y / body.mass * timestep;
body.pos.x += body.vel.x * timestep;
body.pos.y += body.vel.y * timestep;
}
second loop where just one body is moving and it is moving in wrong directions
PVector totalForce = new PVector();
PVector fxy = new PVector();
for(int i = 0; i + 1 < bodies.size(); i++){
Body body = bodies.get(i);
Body other = bodies.get(i + 1);
System.out.println(body + " " + other);
fxy = body.attraction(other);
totalForce.x += fxy.x;
totalForce.y += fxy.y;
body.vel.x += totalForce.x / body.mass * timestep;
body.vel.y += totalForce.y / body.mass * timestep;
body.pos.x += body.vel.x * timestep;
body.pos.y += body.vel.y * timestep;
}