AP Computer Science Final
For this simulation, I used 3 main classes to achieve a functional and logically sound simulation.
Since this simulation was technically built inside a game engine, we need a class to take care of the current state of the universe. These are things like the camera, windows, gui, time, gravitational constant, etc.
This class is used a to compute and update the state of the universe. It also stores the ArrayLists of celestial bodies and updates them.
The most essential class. This classes purpose is to be used as an object to store a body's mass, velocity, and geometry.
Simulating a universe with gravity can be done using one formula: Newton's law of universal gravitation.
Converting this formula into code can also be quite simple. I've done this by iterating through the ArrayList of celestial bodies and calculating the total effect of gravitational pull from each body on eachother. As seen in code here:
for (CelestialBody other : allBodies) {
if (other != this) {
Vector3f direction = other.getGeometry().getLocalTranslation().subtract(this.getGeometry().getLocalTranslation());
Vector3f force = direction.mult(UniverseState.G * other.getMass() / direction.lengthSquared()); // Newton's Law of Universal Gravitation
Vector3f acceleration = force.divide(this.getMass());
this.currentVelocity = this.currentVelocity.add(acceleration.mult(timeStep));
}
}
Here is the first example instance of me getting gravity to work. There is initaly 3 bodies with
similar mass but different
starting postitions and velocity. Tweaking with some numbers, this was able to produce two
bodies that dance together as a binary pair and the other body to orbit them.
This is whats know as an "circumbinary system" and is pretty cool to watch. If you'd like to
create your own, id highly recommend it as this teached me multiple valuable learning lessons in
my software engineering journey. Next, you can try to experiment with different starting
positions, velocities, and masses for the bodies in your
simulation. This will allow you to see how different initial conditions affect the final outcome
of the simulation. You
can also try adjusting the gravitational constant to see how it affects the motion of the
bodies.
You can also try simulating more complex scenarios, such as a system of multiple planets
orbiting a star or a galaxy
with multiple stars. These types of simulations can be quite challenging to create, but they can
also be very rewarding.
As you continue to develop your gravity simulator, you can also research and read about the
latest advances in the field
of celestial mechanics. This will help you to better understand the physics behind your
simulation and to improve its
accuracy. Overall, developing a gravity simulator can be a fun and educational experience, and
can also be a great way to learn
about the complexities of the universe. Specifically, how difficult it is to get planets to
orbit.
On the right is one of my final versions of my simulator. This includes various controls for
things like time, gravity, camera, and options to create custom celestial bodies.
You can browse the project on my Github: