Woking (sort of)
This commit is contained in:
@ -39,6 +39,7 @@ public class Body {
|
|||||||
public Vector gravitationalForce(Body b) {
|
public Vector gravitationalForce(Body b) {
|
||||||
Vector direction = b.massCenter.minus(massCenter);
|
Vector direction = b.massCenter.minus(massCenter);
|
||||||
double distance = direction.length();
|
double distance = direction.length();
|
||||||
|
if (distance == 0) return new Vector();
|
||||||
direction.normalize();
|
direction.normalize();
|
||||||
double force = Simulation.G * mass * b.mass / (distance * distance);
|
double force = Simulation.G * mass * b.mass / (distance * distance);
|
||||||
return direction.times(force);
|
return direction.times(force);
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import codedraw.CodeDraw;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* + -> >= 0
|
* + -> >= 0
|
||||||
@ -44,6 +47,11 @@ public class Octree {
|
|||||||
bodies[i].move(forces[i]);
|
bodies[i].move(forces[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void draw(CodeDraw cd) {
|
||||||
|
if (root == null) return;
|
||||||
|
root.draw(cd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class OctreeItem {
|
abstract class OctreeItem {
|
||||||
@ -62,13 +70,16 @@ abstract class OctreeItem {
|
|||||||
abstract protected void preCalc();
|
abstract protected void preCalc();
|
||||||
|
|
||||||
abstract protected Vector getForcesOnBody(Body body, double t);
|
abstract protected Vector getForcesOnBody(Body body, double t);
|
||||||
|
|
||||||
|
abstract protected void draw(CodeDraw cd);
|
||||||
}
|
}
|
||||||
|
|
||||||
class OctreeNode extends OctreeItem {
|
class OctreeNode extends OctreeItem {
|
||||||
private final OctreeItem[] children = new OctreeItem[8];;
|
private final OctreeItem[] children;
|
||||||
|
|
||||||
protected OctreeNode(Vector center, double size) {
|
protected OctreeNode(Vector center, double size) {
|
||||||
super(center, size);
|
super(center, size);
|
||||||
|
children = new OctreeItem[8];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -77,7 +88,7 @@ class OctreeNode extends OctreeItem {
|
|||||||
if (num < 0) return this;
|
if (num < 0) return this;
|
||||||
OctreeItem oct = children[num];
|
OctreeItem oct = children[num];
|
||||||
if (oct == null) {
|
if (oct == null) {
|
||||||
children[num] = new OctreeLeaf(getOctantCenter(num), size / 2.0, body);
|
children[num] = new OctreeLeaf(getOctantCenter(num), size / 2, body);
|
||||||
} else {
|
} else {
|
||||||
children[num] = oct.add(body);
|
children[num] = oct.add(body);
|
||||||
}
|
}
|
||||||
@ -116,9 +127,21 @@ class OctreeNode extends OctreeItem {
|
|||||||
return force;
|
return force;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void draw(CodeDraw cd) {
|
||||||
|
for (OctreeItem child : children) {
|
||||||
|
if (child == null) continue;
|
||||||
|
child.draw(cd);
|
||||||
|
}
|
||||||
|
Vector p1 = center.minus(new Vector(size / 2));
|
||||||
|
Vector p2 = new Vector(size).minus(new Vector(Simulation.SECTION_SIZE / 2));
|
||||||
|
cd.setColor(Color.CYAN);
|
||||||
|
cd.drawRectangle(p1.getScreenX(cd), p1.getScreenY(cd), p2.getScreenX(cd), p2.getScreenY(cd));
|
||||||
|
}
|
||||||
|
|
||||||
private int getOctantNum(Vector bodyPos) {
|
private int getOctantNum(Vector bodyPos) {
|
||||||
Vector pos = bodyPos.minus(this.center);
|
|
||||||
if (!isInOctant(bodyPos)) return -1;
|
if (!isInOctant(bodyPos)) return -1;
|
||||||
|
Vector pos = bodyPos.minus(this.center);
|
||||||
return ((pos.getX() < 0) ? 1 : 0) | ((pos.getY() < 0) ? 2 : 0) | ((pos.getZ() < 0) ? 4 : 0);
|
return ((pos.getX() < 0) ? 1 : 0) | ((pos.getY() < 0) ? 2 : 0) | ((pos.getZ() < 0) ? 4 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,10 +154,10 @@ class OctreeNode extends OctreeItem {
|
|||||||
|
|
||||||
private Vector getOctantCenter(int octNum) {
|
private Vector getOctantCenter(int octNum) {
|
||||||
return this.center.plus(new Vector(
|
return this.center.plus(new Vector(
|
||||||
((octNum & 1) != 0) ? -0.5 : 0.5,
|
((octNum & 1) != 0) ? -1 : 1,
|
||||||
((octNum & 2) != 0) ? -0.5 : 0.5,
|
((octNum & 2) != 0) ? -1 : 1,
|
||||||
((octNum & 4) != 0) ? -0.5 : 0.5)
|
((octNum & 4) != 0) ? -1 : 1)
|
||||||
.times(this.size));
|
.times(this.size / 4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,15 +171,11 @@ class OctreeLeaf extends OctreeItem {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OctreeNode add(Body body) {
|
protected OctreeNode add(Body body) {
|
||||||
OctreeNode replace = new OctreeNode(this.center, this.size);
|
return new OctreeNode(this.center, this.size).add(this.body).add(body);
|
||||||
replace.add(this.body);
|
|
||||||
replace.add(body);
|
|
||||||
return replace;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void preCalc() {
|
protected void preCalc() {
|
||||||
System.out.println(this.body);
|
|
||||||
this.pseudoBody = new Body(this.body);
|
this.pseudoBody = new Body(this.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,4 +183,12 @@ class OctreeLeaf extends OctreeItem {
|
|||||||
protected Vector getForcesOnBody(Body body, double t) {
|
protected Vector getForcesOnBody(Body body, double t) {
|
||||||
return body.gravitationalForce(pseudoBody);
|
return body.gravitationalForce(pseudoBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void draw(CodeDraw cd) {
|
||||||
|
Vector p1 = center.minus(new Vector(size / 2));
|
||||||
|
Vector p2 = new Vector(size).minus(new Vector(Simulation.SECTION_SIZE / 2));
|
||||||
|
cd.setColor(Color.GREEN);
|
||||||
|
cd.drawRectangle(p1.getScreenX(cd), p1.getScreenY(cd), p2.getScreenX(cd), p2.getScreenY(cd));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ public class Simulation {
|
|||||||
}
|
}
|
||||||
bodies = mergedBodies;
|
bodies = mergedBodies;
|
||||||
*/
|
*/
|
||||||
Octree octree = new Octree(new Vector(AU), SECTION_SIZE);
|
Octree octree = new Octree(new Vector(0, 0, 0), SECTION_SIZE);
|
||||||
for (Body body : bodies) {
|
for (Body body : bodies) {
|
||||||
octree.add(body);
|
octree.add(body);
|
||||||
}
|
}
|
||||||
@ -73,6 +73,7 @@ public class Simulation {
|
|||||||
for (Body body : bodies) {
|
for (Body body : bodies) {
|
||||||
body.draw(cd);
|
body.draw(cd);
|
||||||
}
|
}
|
||||||
|
octree.draw(cd);
|
||||||
cd.show();
|
cd.show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user