mirror of
https://github.com/christiaangoossens/Planetary-Orbit-Simulator
synced 2025-07-02 01:40:47 +00:00
Moved from Vector3f to Vector3d for precision + total calculations
This commit is contained in:
@ -5,10 +5,10 @@ import javax.vecmath.*;
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Object object1 = new Object(1000, new Vector3f(1,2,3), new Vector3f(0,4,3));
|
||||
Object object2 = new Object(200, new Vector3f(2,38,2), new Vector3f(3,4,5));
|
||||
Object object3 = new Object(200, new Vector3f(2,-20,2), new Vector3f(3,4,5));
|
||||
Object object4 = new Object(200, new Vector3f(2,4,2), new Vector3f(3,4,5));
|
||||
Object object1 = new Object(1000, new Vector3d(1,2,3), new Vector3d(0,4,3));
|
||||
Object object2 = new Object(200, new Vector3d(2,38,2), new Vector3d(3,4,5));
|
||||
Object object3 = new Object(200, new Vector3d(2,-20,2), new Vector3d(3,4,5));
|
||||
Object object4 = new Object(200, new Vector3d(2,4,2), new Vector3d(3,4,5));
|
||||
|
||||
// Make a list of all the objects
|
||||
Object[] objects = {object1, object2, object3, object4};
|
||||
|
@ -4,8 +4,8 @@ import java.lang.*;
|
||||
|
||||
public class Object {
|
||||
public double mass;
|
||||
public Vector3f position;
|
||||
public Vector3f speed;
|
||||
public Vector3d position;
|
||||
public Vector3d speed;
|
||||
private double gravitationalConstant = 6.67384E-11;
|
||||
|
||||
/**
|
||||
@ -14,7 +14,7 @@ public class Object {
|
||||
* @param position The position vector of the object
|
||||
* @param speed The speed vector of the object
|
||||
*/
|
||||
public Object(double mass, Vector3f position, Vector3f speed) {
|
||||
public Object(double mass, Vector3d position, Vector3d speed) {
|
||||
this.mass = mass;
|
||||
this.position = position;
|
||||
this.speed = speed;
|
||||
@ -24,7 +24,7 @@ public class Object {
|
||||
* Sets the speed vector of an object
|
||||
* @param speed Current speed vector
|
||||
*/
|
||||
public void setSpeed(Vector3f speed) {
|
||||
public void setSpeed(Vector3d speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ public class Object {
|
||||
* Sets the position vector of an object.
|
||||
* @param position Current position vector
|
||||
*/
|
||||
public void setPosition(Vector3f position) {
|
||||
public void setPosition(Vector3d position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
@ -47,22 +47,22 @@ public class Object {
|
||||
/**
|
||||
* Calculates the force of the passed object on the current object.
|
||||
* @param secondObject The passed object
|
||||
* @return Vector3f The gravitational force
|
||||
* @return Vector3d The gravitational force
|
||||
*/
|
||||
public Vector3f getForceOnObject(Object secondObject) {
|
||||
public Vector3d getForceOnObject(Object secondObject) {
|
||||
double scale = gravitationalConstant * ((this.mass * secondObject.mass) / Math.pow(getDistance(secondObject).length(), 3.0));
|
||||
Vector3f force = getDistance(secondObject);
|
||||
force.scale((float) scale);
|
||||
Vector3d force = getDistance(secondObject);
|
||||
force.scale(scale);
|
||||
return force;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the vector distance between the current position vector and the position vector of the passed object.
|
||||
* @param secondObject The passed object.
|
||||
* @return Vector3f The distance vector
|
||||
* @return Vector3d The distance vector
|
||||
*/
|
||||
private Vector3f getDistance(Object secondObject) {
|
||||
Vector3f distance = new Vector3f(0,0,0); // Empty
|
||||
private Vector3d getDistance(Object secondObject) {
|
||||
Vector3d distance = new Vector3d(0,0,0); // Empty
|
||||
distance.sub(this.position, secondObject.position);
|
||||
return distance;
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.verictas.pos.simulator;
|
||||
import javax.vecmath.*;
|
||||
import com.verictas.pos.simulator.mathUtils.Vector3fMatrix;
|
||||
import com.verictas.pos.simulator.mathUtils.Vector3dMatrix;
|
||||
|
||||
public class Simulator {
|
||||
public static void run(int rounds, Object[] objects) {
|
||||
@ -22,7 +22,7 @@ public class Simulator {
|
||||
/**
|
||||
* Define the forces matrix for this round
|
||||
*/
|
||||
Vector3fMatrix matrix = new Vector3fMatrix(objects.length,objects.length);
|
||||
Vector3dMatrix matrix = new Vector3dMatrix(objects.length,objects.length);
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
/**
|
||||
@ -36,7 +36,7 @@ public class Simulator {
|
||||
break;
|
||||
}
|
||||
|
||||
Vector3f force = objects[i].getForceOnObject(objects[o]);
|
||||
Vector3d force = objects[i].getForceOnObject(objects[o]);
|
||||
matrix.setPosition(force, i, o);
|
||||
System.out.println("Force " + (i + 1) + " on " + (o + 1) + " - " + force);
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
package com.verictas.pos.simulator.mathUtils;
|
||||
|
||||
import javax.vecmath.GMatrix;
|
||||
import javax.vecmath.Vector3f;
|
||||
import javax.vecmath.Vector3d;
|
||||
|
||||
public class Vector3fMatrix extends GMatrix {
|
||||
public class Vector3dMatrix extends GMatrix {
|
||||
/**
|
||||
* Creates a new matrix with some helper functions for use with Vector3f. The created matrix will be empty.
|
||||
* @param n The number of rows.
|
||||
* @param m The number of columns.
|
||||
*/
|
||||
public Vector3fMatrix(int n, int m) {
|
||||
public Vector3dMatrix(int n, int m) {
|
||||
// Change the size to suit Vector3f
|
||||
super(n, m * 3);
|
||||
this.setZero();
|
||||
@ -66,7 +66,7 @@ public class Vector3fMatrix extends GMatrix {
|
||||
* @param n The row to insert into
|
||||
* @param m The column to insert into
|
||||
*/
|
||||
public void setPosition(Vector3f settable, int n, int m) {
|
||||
public void setPosition(Vector3d settable, int n, int m) {
|
||||
int[] position = translatePosition(n, m);
|
||||
n = position[0];
|
||||
m = position[1];
|
||||
@ -80,16 +80,69 @@ public class Vector3fMatrix extends GMatrix {
|
||||
* Provides a way to get a vector from a certain position in the matrix
|
||||
* @param n The row to get from
|
||||
* @param m The column to get from
|
||||
* @return Vector3f The vector in that position
|
||||
* @return Vector3d The vector in that position
|
||||
*/
|
||||
public Vector3f getPosition(int n, int m) {
|
||||
public Vector3d getPosition(int n, int m) {
|
||||
int[] position = translatePosition(n, m);
|
||||
n = position[0];
|
||||
m = position[1];
|
||||
|
||||
float x = (float) this.getElement(n, m);
|
||||
float y = (float) this.getElement(n, m + 1);
|
||||
float z = (float) this.getElement(n, m + 2);
|
||||
return new Vector3f(x, y, z);
|
||||
double x = this.getElement(n, m);
|
||||
double y = this.getElement(n, m + 1);
|
||||
double z = this.getElement(n, m + 2);
|
||||
return new Vector3d(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a way to calculate the result vector of a certain row
|
||||
* @param row The row to calculate the total of
|
||||
* @return Vector3d
|
||||
*/
|
||||
public Vector3d getRowTotal(int row) {
|
||||
double[] rowTotal = new double[this.getNumCol()];
|
||||
this.getRow(row, rowTotal);
|
||||
|
||||
// Create an empty vector to store the result
|
||||
Vector3d resultVector = new Vector3d(0,0,0);
|
||||
|
||||
for(int i = 0; i < this.getNumCol(); i = i + 3) {
|
||||
// For every third entry (including 0).
|
||||
double x = this.getElement(row, i);
|
||||
double y = this.getElement(row, i + 1);
|
||||
double z = this.getElement(row, i + 2);
|
||||
resultVector.add(new Vector3d(x, y, z));
|
||||
}
|
||||
|
||||
System.out.println(resultVector);
|
||||
return resultVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a way to calculate the result vector of a certain column
|
||||
* @param column The column to calculate the total of
|
||||
* @return Vector3d
|
||||
*/
|
||||
public Vector3d getColumnTotal(int column) {
|
||||
double[] columnTotal = new double[this.getNumRow()];
|
||||
|
||||
// Translate the column number to the correct vector column
|
||||
int[] position = translatePosition(0, column);
|
||||
column = position[1];
|
||||
|
||||
this.getColumn(column, columnTotal);
|
||||
|
||||
// Create an empty vector to store the result
|
||||
Vector3d resultVector = new Vector3d(0,0,0);
|
||||
|
||||
for(int i = 0; i < this.getNumRow(); i++) {
|
||||
// For every entry (including 0).
|
||||
double x = this.getElement(i, column);
|
||||
double y = this.getElement(i, column + 1);
|
||||
double z = this.getElement(i, column + 2);
|
||||
resultVector.add(new Vector3d(x, y, z));
|
||||
}
|
||||
|
||||
System.out.println(resultVector);
|
||||
return resultVector;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user