Skip to content

Commit 0da1ff1

Browse files
committed
kinda working
1 parent a7df883 commit 0da1ff1

File tree

9 files changed

+464
-0
lines changed

9 files changed

+464
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>kompfys_planeedid</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

src/ee/liiser/siim/galaxies/Main.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package ee.liiser.siim.galaxies;
2+
3+
import javax.vecmath.Point3d;
4+
import javax.vecmath.Vector3d;
5+
import javax.vecmath.Vector3f;
6+
7+
import ee.liiser.siim.galaxies.calculations.Calculator;
8+
import ee.liiser.siim.galaxies.data.Core;
9+
import ee.liiser.siim.galaxies.data.Star;
10+
import ee.liiser.siim.galaxies.drawing.DrawUtil;
11+
import ee.liiser.siim.galaxies.drawing.Drawable;
12+
13+
public class Main {
14+
15+
static Star[] stars;
16+
static Core[] cores;
17+
18+
static float[] starDistances = new float[] { 2, 3, 4, 5, 6 };
19+
static int[] starCounts = new int[] { 12, 18, 24, 30, 36 };
20+
21+
public static void main(String[] args) {
22+
Core core = new Core(new Vector3f());
23+
Core core2 = new Core(new Vector3f(0, 10, 30));
24+
core2.setVelocity(new Vector3f(0, 0, -0.3f));
25+
cores = new Core[] { core, core2 };
26+
27+
int totalCount = 0;
28+
for (int i : starCounts)
29+
totalCount += i;
30+
stars = new Star[totalCount*cores.length];
31+
int index = 0;
32+
for (int i = 0; i < starDistances.length; i++) {
33+
for (int j = 0; j < starCounts[i]; j++) {
34+
for( Core c : cores){
35+
stars[index++] = new Star(c, starDistances[i], Math.random()
36+
* 2 * Math.PI);
37+
}
38+
}
39+
}
40+
41+
Drawable[] points = new Drawable[stars.length + cores.length];
42+
System.arraycopy(cores, 0, points, 0, cores.length);
43+
System.arraycopy(stars, 0, points, cores.length, stars.length);
44+
DrawUtil.draw(points);
45+
46+
Calculator calc = new Calculator(cores, stars);
47+
long frametime = 0;
48+
long start = 0;
49+
long maxFrame = 0;
50+
while (true) {
51+
// if (frametime > maxFrame) {
52+
// System.out.println(frametime);
53+
// } else {
54+
//
55+
// try {
56+
// Thread.sleep(maxFrame - frametime);
57+
// } catch (InterruptedException e) {
58+
// throw new RuntimeException();
59+
// }
60+
// }
61+
// start = System.currentTimeMillis();
62+
calc.step();
63+
DrawUtil.update(stars);
64+
DrawUtil.update(cores);
65+
DrawUtil.lookAt(
66+
new Point3d(cores[1].getPosition().x + 30, cores[1]
67+
.getPosition().y, cores[1].getPosition().z),
68+
new Point3d(cores[1].getPosition()), new Vector3d(0, 0, -1));
69+
// frametime = System.currentTimeMillis() - start;
70+
71+
}
72+
73+
}
74+
75+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package ee.liiser.siim.galaxies.calculations;
2+
3+
import javax.vecmath.Vector3f;
4+
5+
import ee.liiser.siim.galaxies.data.Core;
6+
import ee.liiser.siim.galaxies.data.Star;
7+
import ee.liiser.siim.galaxies.drawing.Drawable;
8+
9+
public class Calculator {
10+
11+
private Core[] cores;
12+
private Star[] stars;
13+
public static float dt = 0.001f;
14+
15+
public Calculator(Core[] cores, Star[] stars) {
16+
this.cores = cores;
17+
this.stars = stars;
18+
}
19+
20+
public void step() {
21+
for (Star star : stars) {
22+
updatePos(star);
23+
}
24+
for (Core core : cores) {
25+
updatePos(core);
26+
}
27+
}
28+
29+
private void updatePos(Drawable point) {
30+
31+
// // Basic Verlet':
32+
// Vector3f newPos = new Vector3f();
33+
// newPos.scale(2, point.getPosition());
34+
// newPos.sub(point.getOldPosition());
35+
// Vector3f a = acc(point);
36+
// a.scale(dt * dt);
37+
// newPos.add(a);
38+
// // newPos = 2*pos - oldPos + acc(pos)*dt^2
39+
// point.updatePosition(newPos);
40+
41+
//Velocity Verlet'
42+
Vector3f v05 = new Vector3f();
43+
v05.scale(dt/2, acc(point));
44+
v05.add(point.getVelocity());
45+
//v(t+dt/2) = v + a*dt/2
46+
47+
Vector3f newPos = new Vector3f();
48+
newPos.scale(dt, v05);
49+
newPos.add(point.getPosition());
50+
point.updatePosition(newPos);
51+
//x(t+dt) = x + v05*dt
52+
53+
Vector3f newVel = new Vector3f();
54+
newVel.scale(dt/2, acc(point));
55+
newVel.add(v05);
56+
point.updateVel(newVel);
57+
//v(t+dt) = v05 + a(t+dt)*dt/2
58+
59+
}
60+
61+
private Vector3f acc(Drawable point) {
62+
Vector3f a = new Vector3f();
63+
for (Core core : cores) {
64+
if (core == point)
65+
continue;
66+
Vector3f relPos = new Vector3f();
67+
// r.12
68+
relPos.sub(core.getPosition(), point.getPosition());
69+
70+
// m * r12 / len(r12)^3
71+
relPos.scale((float) (core.getMass() / Math.pow(relPos.length(), 3)));
72+
73+
// a +=
74+
a.add(relPos);
75+
}
76+
return a;
77+
}
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package ee.liiser.siim.galaxies.calculations;
2+
3+
import javax.vecmath.Vector3f;
4+
5+
import ee.liiser.siim.galaxies.data.Core;
6+
import ee.liiser.siim.galaxies.drawing.Drawable;
7+
8+
public class WorkerThread extends Thread{
9+
10+
Drawable[] stars;
11+
Core[] cores;
12+
int min;
13+
int max;
14+
Object notifyObject;
15+
public static float dt = 0.001f;
16+
17+
public WorkerThread(Drawable[] stars, Core[] cores, int min, int max, Object notifyObject) {
18+
this.stars = stars;
19+
this.cores = cores;
20+
this.min = min;
21+
this.max = max;
22+
this.notifyObject = notifyObject;
23+
}
24+
25+
@Override
26+
public void run() {
27+
for(; min <max; min++){
28+
updatePos(stars[min]);
29+
}
30+
}
31+
32+
33+
void updatePos(Drawable point) {
34+
35+
// // Basic Verlet':
36+
// Vector3f newPos = new Vector3f();
37+
// newPos.scale(2, point.getPosition());
38+
// newPos.sub(point.getOldPosition());
39+
// Vector3f a = acc(point);
40+
// a.scale(dt * dt);
41+
// newPos.add(a);
42+
// // newPos = 2*pos - oldPos + acc(pos)*dt^2
43+
// point.updatePosition(newPos);
44+
45+
//Velocity Verlet'
46+
Vector3f v05 = new Vector3f();
47+
v05.scale(dt/2, acc(point));
48+
v05.add(point.getVelocity());
49+
//v(t+dt/2) = v + a*dt/2
50+
51+
Vector3f newPos = new Vector3f();
52+
newPos.scale(dt, v05);
53+
newPos.add(point.getPosition());
54+
point.updatePosition(newPos);
55+
//x(t+dt) = x + v05*dt
56+
57+
Vector3f newVel = new Vector3f();
58+
newVel.scale(dt/2, acc(point));
59+
newVel.add(v05);
60+
point.updateVel(newVel);
61+
//v(t+dt) = v05 + a(t+dt)*dt/2
62+
63+
}
64+
65+
private Vector3f acc(Drawable point) {
66+
Vector3f a = new Vector3f();
67+
for (Core core : cores) {
68+
if (core == point)
69+
continue;
70+
Vector3f relPos = new Vector3f();
71+
// r.12
72+
relPos.sub(core.getPosition(), point.getPosition());
73+
74+
// m * r12 / len(r12)^3
75+
relPos.scale((float) (core.getMass() / Math.pow(relPos.length(), 3)));
76+
77+
// a +=
78+
a.add(relPos);
79+
}
80+
return a;
81+
}
82+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ee.liiser.siim.galaxies.data;
2+
3+
import javax.vecmath.Vector3f;
4+
5+
import ee.liiser.siim.galaxies.calculations.Calculator;
6+
import ee.liiser.siim.galaxies.drawing.Drawable;
7+
8+
public class Core extends Drawable {
9+
10+
private static final float CORE_SIZE = 0.3f;
11+
private float mass = 1;
12+
13+
public Core() {
14+
this(0,0,0);
15+
}
16+
17+
public Core(int x, int y, int z) {
18+
this(new Vector3f(x,y,z));
19+
}
20+
21+
public Core(Vector3f position) {
22+
this(position, position);
23+
}
24+
25+
public Core(Vector3f position, Vector3f oldPosition) {
26+
this.position = position;
27+
this.oldPosition = oldPosition;
28+
this.setVelocity(new Vector3f());
29+
this.getVelocity().sub(position, oldPosition);
30+
getVelocity().scale(1/Calculator.dt);
31+
}
32+
33+
@Override
34+
public float getSize() {
35+
return CORE_SIZE;
36+
}
37+
38+
public float getMass() {
39+
return mass;
40+
}
41+
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ee.liiser.siim.galaxies.data;
2+
3+
import javax.vecmath.Vector3f;
4+
5+
import ee.liiser.siim.galaxies.calculations.Calculator;
6+
import ee.liiser.siim.galaxies.drawing.Drawable;
7+
8+
public class Star extends Drawable {
9+
10+
private static final float STAR_SIZE = 0.1f;
11+
12+
public Star(float x, float y, float z, float xOld, float yOld, float zOld) {
13+
this(new Vector3f(x, y, z), new Vector3f(xOld, yOld, zOld));
14+
}
15+
16+
public Star(Vector3f position, Vector3f oldPosition) {
17+
this.position = position;
18+
this.oldPosition = oldPosition;
19+
}
20+
21+
public Star(Core core, float distance) {
22+
this(distance, 0, 0, distance, (float) (Calculator.dt / Math.sqrt(distance)), 0);
23+
}
24+
25+
public Star(Core core, float distance, double d) {
26+
float x = (float) (distance * Math.cos(d));
27+
float y = (float) (distance * Math.sin(d));
28+
double theta = Calculator.dt / (distance * Math.sqrt(distance));
29+
this.position = new Vector3f(core.getPosition().x + x, core.getPosition().y + y, 0);
30+
this.oldPosition = new Vector3f(core.getPosition().x + (float) (x * Math.cos(theta) + y
31+
* Math.sin(theta)), core.getPosition().y + (float) (y * Math.cos(theta) - x
32+
* Math.sin(theta)), 0);
33+
this.setVelocity(new Vector3f());
34+
this.getVelocity().sub(position, oldPosition);
35+
this.getVelocity().scale(1/Calculator.dt);
36+
}
37+
38+
@Override
39+
public float getSize() {
40+
return STAR_SIZE;
41+
}
42+
}

0 commit comments

Comments
 (0)