Files
EP2-Team/src/Simulation.java
2022-06-07 22:27:26 +02:00

71 lines
2.4 KiB
Java

import codedraw.CodeDraw;
import java.awt.*;
import java.util.Random;
public class Simulation {
// gravitational constant
public static final double G = 6.6743e-11;
// one astronomical unit (AU) is the average distance of earth to the sun.
public static final double AU = 150e9; // meters
// one light year
public static final double LY = 9.461e15; // meters
// some further constants needed in the simulation
public static final double SUN_MASS = 1.989e30; // kilograms
public static final double SUN_RADIUS = 696340e3; // meters
public static final double EARTH_MASS = 5.972e24; // kilograms
public static final double EARTH_RADIUS = 6371e3; // meters
// set some system parameters
public static final double SECTION_SIZE = 2 * AU; // the size of the square region in space
public static final int NUMBER_OF_BODIES = 10000;
public static final double OVERALL_SYSTEM_MASS = 2000 * SUN_MASS; // kilograms
public static void main(String[] args) {
CodeDraw cd = new CodeDraw();
Body[] bodies = new Body[NUMBER_OF_BODIES];
Random random = new Random(2022);
for (int i = 0; i < Simulation.NUMBER_OF_BODIES; i++) {
bodies[i] = new Body(
Math.abs(random.nextGaussian()) * Simulation.OVERALL_SYSTEM_MASS / Simulation.NUMBER_OF_BODIES,
new Vector(
0.2 * random.nextGaussian() * Simulation.AU,
0.2 * random.nextGaussian() * Simulation.AU,
0.2 * random.nextGaussian() * Simulation.AU
),
new Vector(
0 + random.nextGaussian() * 5e3,
0 + random.nextGaussian() * 5e3,
0 + random.nextGaussian() * 5e3
)
);
}
long seconds = 0;
while (true) {
seconds++;
Octree octree = new Octree(new Vector(0, 0, 0), SECTION_SIZE);
for (Body body : bodies) {
octree.add(body);
}
octree.applyForces(bodies, 100);
if ((seconds % 60) == 0) {
cd.clear(Color.BLACK);
for (Body body : bodies) {
body.draw(cd);
}
octree.draw(cd);
cd.show();
}
}
}
}