diff --git a/simulator/.idea/artifacts/simulator_jar.xml b/simulator/.idea/artifacts/simulator_jar.xml new file mode 100644 index 0000000..373d32f --- /dev/null +++ b/simulator/.idea/artifacts/simulator_jar.xml @@ -0,0 +1,9 @@ + + + $PROJECT_DIR$/out/artifacts/simulator_jar + + + + + + \ No newline at end of file diff --git a/simulator/.idea/dictionaries/Christiaan.xml b/simulator/.idea/dictionaries/Christiaan.xml new file mode 100644 index 0000000..8da9fcd --- /dev/null +++ b/simulator/.idea/dictionaries/Christiaan.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/simulator/.idea/libraries/vecmath_1_5_1.xml b/simulator/.idea/libraries/vecmath_1_5_2.xml similarity index 50% rename from simulator/.idea/libraries/vecmath_1_5_1.xml rename to simulator/.idea/libraries/vecmath_1_5_2.xml index f1567b6..30d8f0a 100644 --- a/simulator/.idea/libraries/vecmath_1_5_1.xml +++ b/simulator/.idea/libraries/vecmath_1_5_2.xml @@ -1,7 +1,7 @@ - + - + diff --git a/simulator/.idea/uiDesigner.xml b/simulator/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/simulator/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/simulator/.idea/workspace.xml b/simulator/.idea/workspace.xml index 9645724..d04d1ab 100644 --- a/simulator/.idea/workspace.xml +++ b/simulator/.idea/workspace.xml @@ -1,5 +1,10 @@ + + + + + @@ -22,38 +27,52 @@ - + - - + + - + + + + + + + + + + + + + - - + + - - - - - - + + - - - + + + + + + + + + @@ -78,8 +97,13 @@ @@ -89,6 +113,22 @@ + + + + + + + + + + + Android + + + + + @@ -114,9 +154,6 @@ - - - @@ -148,22 +185,44 @@ + + + + + + + + - - + + - - - + + + + + + + + @@ -426,55 +485,30 @@ - + - + - - - - - - - + + + + + + + - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -498,36 +534,41 @@ - - + + - + - - - + + + + + - + - - + + - + - - + + + + + @@ -536,38 +577,100 @@ - + - - + + + + + + + + + + + + + + + + + + - - - - - + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + simulator:jar @@ -615,7 +718,6 @@ @@ -623,7 +725,7 @@ - vecmath-1.5.1 + vecmath-1.5.2 \ No newline at end of file diff --git a/simulator/src/META-INF/MANIFEST.MF b/simulator/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..3b9e3a9 --- /dev/null +++ b/simulator/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.verictas.pos.simulator.Main + diff --git a/simulator/src/com/verictas/pos/simulator/Main.java b/simulator/src/com/verictas/pos/simulator/Main.java index de78de5..22e59b7 100644 --- a/simulator/src/com/verictas/pos/simulator/Main.java +++ b/simulator/src/com/verictas/pos/simulator/Main.java @@ -1,16 +1,19 @@ package com.verictas.pos.simulator; + import javax.vecmath.*; public class Main { public static void main(String[] args) { - System.out.println("Hello World!"); + 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(10E8, new Vector3f(1,2,3), new Vector3f(0,4,3)); - Object object2 = new Object(20E4, new Vector3f(2,38,2), new Vector3f(3,4,5)); + // Make a list of all the objects + Object[] objects = {object1, object2, object3, object4}; - System.out.println(object1.toString()); - System.out.println(object2.toString()); - System.out.println(object1.getForceOnObject(object2)); + // Start the simulation + Simulator.run(2, objects); } } diff --git a/simulator/src/com/verictas/pos/simulator/Object.java b/simulator/src/com/verictas/pos/simulator/Object.java index abd2edf..9b7c965 100644 --- a/simulator/src/com/verictas/pos/simulator/Object.java +++ b/simulator/src/com/verictas/pos/simulator/Object.java @@ -8,24 +8,47 @@ public class Object { public Vector3f speed; private double gravitationalConstant = 6.67384E-11; + /** + * Constructs an object + * @param mass The mass of the 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) { this.mass = mass; this.position = position; this.speed = speed; } + /** + * Sets the speed vector of an object + * @param speed Current speed vector + */ public void setSpeed(Vector3f speed) { this.speed = speed; } + /** + * Sets the position vector of an object. + * @param position Current position vector + */ public void setPosition(Vector3f position) { this.position = position; } + /** + * Changes an object into readable form + * @return String + */ public String toString() { return "Mass: " + this.mass + " & Position: " + this.position + " & Speed: " + this.speed; } + /** + * Calculates the force of the passed object on the current object. + * @param secondObject The passed object + * @return Vector3f The gravitational force + */ public Vector3f getForceOnObject(Object secondObject) { double scale = gravitationalConstant * ((this.mass * secondObject.mass) / Math.pow(getDistance(secondObject).length(), 3.0)); Vector3f force = getDistance(secondObject); @@ -33,6 +56,11 @@ public class Object { 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 + */ private Vector3f getDistance(Object secondObject) { Vector3f distance = new Vector3f(0,0,0); // Empty distance.sub(this.position, secondObject.position); diff --git a/simulator/src/com/verictas/pos/simulator/Simulator.java b/simulator/src/com/verictas/pos/simulator/Simulator.java new file mode 100644 index 0000000..7ca08b3 --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/Simulator.java @@ -0,0 +1,64 @@ +package com.verictas.pos.simulator; +import javax.vecmath.*; +import com.verictas.pos.simulator.mathUtils.Vector3fMatrix; + +public class Simulator { + public static void run(int rounds, Object[] objects) { + + /** + * Log a debug message to the console to signal the simulation has started + */ + System.out.println("========== Simulation Started ==========\n"); + + /** + * Start the rounds loop + */ + for(int t = 0; t < rounds; t++) { + /** + * The round has started + */ + System.out.println("Round " + (t + 1) + " started!"); + + /** + * Define the forces matrix for this round + */ + Vector3fMatrix matrix = new Vector3fMatrix(objects.length,objects.length); + + for(int i = 0; i < objects.length; i++) { + /** + * For every object: calculate the force upon it. + */ + for (int o = 0; o < objects.length; o++) { + /** + * Loop through all other objects + */ + if (o == i) { + break; + } + + Vector3f force = objects[i].getForceOnObject(objects[o]); + matrix.setPosition(force, i, o); + System.out.println("Force " + (i + 1) + " on " + (o + 1) + " - " + force); + + /** + * Also put in the opposite force + */ + force.scale(-1); + matrix.setPosition(force, o, i); + System.out.println("Force " + (o + 1) + " on " + (i + 1) + " - " + force); + } + } + + /** + * Print the matrix for this round + */ + System.out.println("\n"); + System.out.println(matrix); + } + + /** + * Log that the simulation has finished + */ + System.out.println("========== Simulation Finished =========="); + } +} diff --git a/simulator/src/com/verictas/pos/simulator/mathUtils/Vector3fMatrix.java b/simulator/src/com/verictas/pos/simulator/mathUtils/Vector3fMatrix.java new file mode 100644 index 0000000..9730cd1 --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/mathUtils/Vector3fMatrix.java @@ -0,0 +1,95 @@ +package com.verictas.pos.simulator.mathUtils; + +import javax.vecmath.GMatrix; +import javax.vecmath.Vector3f; + +public class Vector3fMatrix 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) { + // Change the size to suit Vector3f + super(n, m * 3); + this.setZero(); + } + + /** + * Set the size of the matrix in the amount of vectors (e.g. a 1 x 3 vector matrix gets converted to a 1 x 9 storage matrix). + * @param n The amount of rows + * @param m The amount of columns expressed in vectors (1 vector = 3 values). + */ + public void setSizeInVectors(int n, int m) { + this.setSize(n, m * 3); + } + + /** + * Provides a function for putting the matrix into String form for visualisation. + * @return String + */ + public String toString() { + StringBuffer buffer = new StringBuffer(this.getNumRow() * this.getNumCol() * 8); + + for(int n = 0; n < this.getNumRow(); ++n) { + for(int m = 0; m < this.getNumCol(); ++m) { + if ((m + 1) == 1 || m % 3 == 0) { + // If m is 1 or a multiple of 4, begin the bracket. + buffer.append("(").append(this.getElement(n, m)).append(", "); + } else if ((m + 1) % 3 == 0) { + // If m is a multiple of 3, close the bracket + buffer.append(this.getElement(n, m)).append(")\t\t"); + } else { + buffer.append(this.getElement(n, m)).append(", "); + } + } + + buffer.append("\n"); + } + + return buffer.toString(); + } + + /** + * Provides a translator from the vector positions (e.g. the second vector starts at position 1) to the matrix positions (the second vector starts at position 3). + * @param n The vector positions row + * @param m The vector positions column + * @return void + */ + private int[] translatePosition(int n, int m) { + return new int[]{n, m * 3}; + } + + /** + * Provides a way to set a vector into a certain position in the matrix + * @param settable The vector you want to put in the matrix + * @param n The row to insert into + * @param m The column to insert into + */ + public void setPosition(Vector3f settable, int n, int m) { + int[] position = translatePosition(n, m); + n = position[0]; + m = position[1]; + + this.setElement(n, m, settable.x); + this.setElement(n, m + 1, settable.y); + this.setElement(n, m + 2, settable.z); + } + + /** + * 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 + */ + public Vector3f 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); + } +} \ No newline at end of file diff --git a/simulator/vecmath-1.5.1.jar b/simulator/vecmath-1.5.1.jar deleted file mode 100644 index 6d8b3a1..0000000 Binary files a/simulator/vecmath-1.5.1.jar and /dev/null differ diff --git a/simulator/vecmath-1.5.2.jar b/simulator/vecmath-1.5.2.jar new file mode 100644 index 0000000..58dfc51 Binary files /dev/null and b/simulator/vecmath-1.5.2.jar differ