Finish AB 2

This commit is contained in:
2022-03-28 10:13:19 +02:00
parent 5158aa2cbe
commit dca869995a
4 changed files with 151 additions and 32 deletions

View File

@ -41,14 +41,14 @@ public class Simulation {
public static void main(String[] args) {
// simulation
CodeDraw cd = new CodeDraw();
Body[] bodies = new Body[NUMBER_OF_BODIES];
Vector3[] forceOnBody = new Vector3[bodies.length];
BodyQueue bodies = new BodyQueue();
BodyForceMap forceOnBody = new BodyForceMap();
Random random = new Random(2022);
for (int i = 0; i < bodies.length; i++) {
bodies[i] = new Body(
Math.abs(random.nextGaussian()) * OVERALL_SYSTEM_MASS / bodies.length, // kg
for (int i = 0; i < NUMBER_OF_BODIES; i++) {
bodies.add(new Body(
Math.abs(random.nextGaussian()) * OVERALL_SYSTEM_MASS / NUMBER_OF_BODIES, // kg
new Vector3(
0.2 * random.nextGaussian() * AU,
0.2 * random.nextGaussian() * AU,
@ -59,15 +59,21 @@ public class Simulation {
0 + random.nextGaussian() * 5e3,
0 + random.nextGaussian() * 5e3
)
);
));
}
double seconds = 0;
// simulation loop
while (true) {
BodyQueue newBodies = new BodyQueue(bodies);
Body[] tmp = new Body[bodies.size()];
for (int i = 0; bodies.size() > 0; i++) {
tmp[i] = bodies.poll();
}
seconds++; // each iteration computes the movement of the celestial bodies within one second.
/*
// merge bodies that have collided
for (int i = 0; i < bodies.length; i++) {
for (int j = i + 1; j < bodies.length; j++) {
@ -86,22 +92,23 @@ public class Simulation {
}
}
}
*/
// for each body (with index i): compute the total force exerted on it.
for (int i = 0; i < bodies.length; i++) {
forceOnBody[i] = new Vector3(); // begin with zero
for (int j = 0; j < bodies.length; j++) {
if (i != j) {
Vector3 forceToAdd = bodies[i].gravitationalForce(bodies[j]);
forceOnBody[i] = forceOnBody[i].plus(forceToAdd);
for (Body b1 : tmp) {
Vector3 force = new Vector3(); // begin with zero
for (Body b2 : tmp) {
if (b1 != b2) {
force = force.plus(b1.gravitationalForce(b2));
}
}
forceOnBody.put(b1, force);
}
// now forceOnBody[i] holds the force vector exerted on body with index i.
// for each body (with index i): move it according to the total force exerted on it.
for (int i = 0; i < bodies.length; i++) {
bodies[i].move(forceOnBody[i]);
for (Body body : tmp) {
body.move(forceOnBody.get(body));
}
// show all movements in the canvas only every hour (to speed up the simulation)
@ -110,7 +117,7 @@ public class Simulation {
cd.clear(Color.BLACK);
// draw new positions
for (Body body : bodies) {
for (Body body : tmp) {
body.draw(cd);
}
@ -118,6 +125,7 @@ public class Simulation {
cd.show();
}
bodies = newBodies;
}
}