mirror of
https://github.com/christiaangoossens/Planetary-Orbit-Simulator
synced 2025-07-03 18:10:47 +00:00
Add aphelion & perihelion calculations
This commit is contained in:
@ -21,19 +21,20 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/**
|
||||
* Object definitions (in 1990)
|
||||
* Object definitions
|
||||
*/
|
||||
Object sun = new Object("Sun", 1.988544E30, AU.convertToMeter(new Vector3d(3.621484938699030E-03,3.203347049968909E-03,-1.609087138389905E-04)), AU.convertToMetersPerSecond(new Vector3d(-1.730306264794065E-06,6.909301960615850E-06,3.332250766613383E-08)));
|
||||
//Object venus = new Object("Venus", 48.685E23, AU.convertToMeter(new Vector3d(-3.786926829662159E-01,-6.122709221027441E-01,1.346180701578967E-02)), AU.convertToMetersPerSecond(new Vector3d(1.703979708314098E-02,-1.075790617185284E-02,-1.130972411646143E-03)));
|
||||
Object earth = new Object("Earth", 5.97219E24, AU.convertToMeter(new Vector3d(1.000272608326749E+00,-1.305632418724720E-01,-1.614384880329670E-04)), AU.convertToMetersPerSecond(new Vector3d(2.003180730888720E-03,1.698793770993201E-02,5.869001824818362E-08)));
|
||||
//Object mars = new Object("Mars", 6.4185E23, AU.convertToMeter(new Vector3d(8.638055532014732E-01,-1.094520306989018E+00,-4.427515002554464E-02)), AU.convertToMetersPerSecond(new Vector3d(1.154235320339802E-02,9.839355267552327E-03,-7.723750026136471E-05)));
|
||||
//Object jupiter = new Object("Jupiter", 1898.13E24, AU.convertToMeter(new Vector3d(-5.440309619306835E+00,-2.383659935837559E-01,1.226571001615609E-01)), AU.convertToMetersPerSecond(new Vector3d(2.422143907277735E-04,-7.182284468246539E-03,2.440789748210396E-05)));
|
||||
// Object mars = new Object("Mars", 6.4185E23, AU.convertToMeter(new Vector3d(8.638055532014732E-01,-1.094520306989018E+00,-4.427515002554464E-02)), AU.convertToMetersPerSecond(new Vector3d(1.154235320339802E-02,9.839355267552327E-03,-7.723750026136471E-05)));
|
||||
// Object jupiter = new Object("Jupiter", 1898.13E24, AU.convertToMeter(new Vector3d(-5.440309619306835E+00,-2.383659935837559E-01,1.226571001615609E-01)), AU.convertToMetersPerSecond(new Vector3d(2.422143907277735E-04,-7.182284468246539E-03,2.440789748210396E-05)));
|
||||
Object moon = new Object("The Moon", 734.9E20, AU.convertToMeter(new Vector3d(1.002390058141768E+00,-1.318677081380600E-01,-1.051759034600983E-04)), AU.convertToMetersPerSecond(new Vector3d(2.294349896503608E-03,1.752303034437222E-02,-5.522655228080146E-05)));
|
||||
|
||||
/**
|
||||
* Object listing
|
||||
*/
|
||||
|
||||
Object[] objects = {sun, earth};
|
||||
Object[] objects = {sun, earth, moon};
|
||||
|
||||
/**
|
||||
* Run the simulator for the specified objects
|
||||
|
@ -10,6 +10,11 @@ public class Object {
|
||||
public Vector3d acceleration;
|
||||
public Vector3d oldAcceleration;
|
||||
|
||||
public Vector3d aphelion;
|
||||
public Vector3d perihelion;
|
||||
public double aphelionDistance = 0;
|
||||
public double perihelionDistance = 0;
|
||||
|
||||
public String name;
|
||||
|
||||
private double gravitationalConstant = 6.67384E-11;
|
||||
@ -197,4 +202,38 @@ public class Object {
|
||||
acceleration.scale(factor);
|
||||
this.acceleration = acceleration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the aphelion & perihelion
|
||||
* @param sun
|
||||
*/
|
||||
public void processAphelionAndPerihelion(Object sun) {
|
||||
double sunDistance = this.getDistance(sun).length();
|
||||
|
||||
/**
|
||||
* Set the defaults
|
||||
*/
|
||||
|
||||
if (this.aphelionDistance == 0) {
|
||||
this.aphelionDistance = sunDistance;
|
||||
}
|
||||
|
||||
if (this.perihelionDistance == 0) {
|
||||
this.perihelionDistance = sunDistance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the aphelion or perihelion should be changed
|
||||
*/
|
||||
|
||||
if (sunDistance > aphelionDistance) {
|
||||
this.aphelion = position;
|
||||
this.aphelionDistance = sunDistance;
|
||||
}
|
||||
|
||||
if (sunDistance < perihelionDistance) {
|
||||
this.perihelion = position;
|
||||
this.perihelionDistance = sunDistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class Simulator {
|
||||
*/
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
writer.write(objects[i].name, objects[i].position, objects[i].speed, objects[i].oldAcceleration, objects[i].acceleration, objects[i].mass);
|
||||
writer.write(objects[i], objects[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +65,13 @@ public class Simulator {
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
objects[i].updateSpeed(time);
|
||||
writer.write(objects[i].name, objects[i].position, objects[i].speed, objects[i].oldAcceleration, objects[i].acceleration, objects[i].mass);
|
||||
writer.write(objects[i], objects[0]);
|
||||
|
||||
/**
|
||||
* Do some processing to get the aphelion & perihelion
|
||||
*/
|
||||
|
||||
objects[i].processAphelionAndPerihelion(objects[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,6 +85,15 @@ public class Simulator {
|
||||
writer.save();
|
||||
System.out.println("========== Simulation Finished ==========");
|
||||
|
||||
// TEST
|
||||
|
||||
System.out.println("\n\n============== Simulation data =============");
|
||||
System.out.println("Position during aphelion: " + objects[1].aphelion);
|
||||
System.out.println("Distance from the sun during aphelion in km: " + objects[1].aphelionDistance);
|
||||
System.out.println("Position during perihelion: " + objects[1].perihelion);
|
||||
System.out.println("Distance from the sun during perihelion in km: " + objects[1].perihelionDistance);
|
||||
System.out.println("===========================================\n\n");
|
||||
|
||||
/**
|
||||
* Display information about the program runtime
|
||||
*/
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
import com.verictas.pos.simulator.Object;
|
||||
import com.verictas.pos.simulator.SimulatorConfig;
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
|
||||
@ -47,7 +48,7 @@ public class DataWriter {
|
||||
* Open a file to write to and write the header
|
||||
*/
|
||||
this.writer = new FileWriter(path);
|
||||
this.writer.write("Object" + DELIMITER + "Position (m)" + DELIMITER + "Position (AU)" + DELIMITER + "Speed (m/s)" + DELIMITER + "Speed (AU/day)" + DELIMITER + "Old Acceleration" + DELIMITER + "Acceleration" + DELIMITER + "Mass" + NEW_LINE);
|
||||
this.writer.write("Object" + DELIMITER + "Position (m)" + DELIMITER + "Position (AU)" + DELIMITER+ "Distance from the sun (m)" + DELIMITER + "Speed (m/s)" + DELIMITER + "Speed (AU/day)" + DELIMITER + "Old Acceleration" + DELIMITER + "Acceleration" + DELIMITER + "Mass" + NEW_LINE);
|
||||
this.counter++;
|
||||
} catch(IOException e) {
|
||||
throw new WritingException("The destination file couldn't be created.");
|
||||
@ -78,22 +79,29 @@ public class DataWriter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes some data about the current object to the file
|
||||
* @param id String
|
||||
* @param position Vector3d
|
||||
* @param speed Vector3d
|
||||
* @param oldAcceleration Vector3d
|
||||
* @param acceleration Vector3d
|
||||
* @param mass double
|
||||
*
|
||||
* @param object The object you want to write data about
|
||||
* @param reference The system's star
|
||||
* @throws WritingException
|
||||
*/
|
||||
public void write(String id, Vector3d position, Vector3d speed, Vector3d oldAcceleration, Vector3d acceleration, double mass) throws WritingException {
|
||||
public void write(Object object, Object reference) throws WritingException {
|
||||
String id = object.name;
|
||||
Vector3d position = object.position;
|
||||
Vector3d speed = object.speed;
|
||||
Vector3d oldAcceleration = object.oldAcceleration;
|
||||
Vector3d acceleration = object.acceleration;
|
||||
double mass = object.mass;
|
||||
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
if (this.counter % SimulatorConfig.skipLines == 0) {
|
||||
this.writer.append(id + DELIMITER + position.toString() + DELIMITER + AU.convertFromMeter(position).toString() + DELIMITER + speed.toString() + DELIMITER + AU.convertFromMetersPerSecond(speed).toString() + DELIMITER + oldAcceleration.toString() + DELIMITER + acceleration.toString() + DELIMITER + String.valueOf(mass) + NEW_LINE);
|
||||
|
||||
// Calculate the distance to the sun
|
||||
double sunDistance = object.getDistance(reference).length();
|
||||
|
||||
this.writer.append(id + DELIMITER + position.toString() + DELIMITER + AU.convertFromMeter(position).toString() + DELIMITER + String.valueOf(sunDistance) + DELIMITER + speed.toString() + DELIMITER + AU.convertFromMetersPerSecond(speed).toString() + DELIMITER + oldAcceleration.toString() + DELIMITER + acceleration.toString() + DELIMITER + String.valueOf(mass) + NEW_LINE);
|
||||
}
|
||||
this.counter++;
|
||||
} catch (Exception e) {
|
||||
|
Reference in New Issue
Block a user