Removed preCalc
This commit is contained in:
@ -4,7 +4,7 @@ import codedraw.CodeDraw;
|
||||
* This class represents celestial bodies like stars, planets, asteroids, etc...
|
||||
*/
|
||||
public class Body {
|
||||
private final double mass;
|
||||
private double mass;
|
||||
private Vector massCenter; // position of the mass center.
|
||||
private Vector currentMovement;
|
||||
|
||||
@ -83,6 +83,12 @@ public class Body {
|
||||
return massCenter;
|
||||
}
|
||||
|
||||
public void update(Body b) {
|
||||
double combinedMass = this.mass + b.mass;
|
||||
this.massCenter = this.massCenter.times(this.mass / combinedMass).plus(b.massCenter.times(b.mass / combinedMass));
|
||||
this.mass = combinedMass;
|
||||
}
|
||||
|
||||
public boolean collidesWith(Body body) {
|
||||
return this.distanceTo(body) < this.radius() + body.radius();
|
||||
}
|
||||
|
@ -38,7 +38,6 @@ public class Octree {
|
||||
|
||||
public void applyForces(Body[] bodies, double t) {
|
||||
if (root == null) return;
|
||||
root.preCalc();
|
||||
Vector[] forces = new Vector[bodies.length];
|
||||
for (int i = 0; i < bodies.length; i++) {
|
||||
forces[i] = root.getForcesOnBody(bodies[i], t);
|
||||
@ -67,8 +66,6 @@ abstract class OctreeItem {
|
||||
|
||||
abstract protected OctreeNode add(Body body);
|
||||
|
||||
abstract protected void preCalc();
|
||||
|
||||
abstract protected Vector getForcesOnBody(Body body, double t);
|
||||
|
||||
abstract protected void draw(CodeDraw cd);
|
||||
@ -90,6 +87,11 @@ class OctreeNode extends OctreeItem {
|
||||
protected OctreeNode add(Body body) {
|
||||
int num = getOctantNum(body.massCenter());
|
||||
if (num < 0) return this;
|
||||
if (this.pseudoBody == null) {
|
||||
this.pseudoBody = new Body(body);
|
||||
} else {
|
||||
this.pseudoBody.update(body);
|
||||
}
|
||||
OctreeItem oct = children[num];
|
||||
if (oct == null) {
|
||||
children[num] = new OctreeLeaf(getOctantCenter(num), size / 2, body);
|
||||
@ -99,22 +101,6 @@ class OctreeNode extends OctreeItem {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void preCalc() {
|
||||
double mass = 0;
|
||||
for (OctreeItem oct : children) {
|
||||
if (oct == null) continue;
|
||||
oct.preCalc();
|
||||
mass += oct.pseudoBody.mass();
|
||||
}
|
||||
Vector massCenter = new Vector();
|
||||
for (OctreeItem oct : children) {
|
||||
if (oct == null) continue;
|
||||
massCenter.add(oct.pseudoBody.massCenter().times(oct.pseudoBody.mass() / mass));
|
||||
}
|
||||
this.pseudoBody = new Body(mass, massCenter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector getForcesOnBody(Body body, double t) {
|
||||
double r = pseudoBody.massCenter().distanceTo(body.massCenter());
|
||||
@ -171,6 +157,7 @@ class OctreeLeaf extends OctreeItem {
|
||||
public OctreeLeaf(Vector center, double size, Body body) {
|
||||
super(center, size);
|
||||
this.body = body;
|
||||
this.pseudoBody = new Body(this.body);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -178,11 +165,6 @@ class OctreeLeaf extends OctreeItem {
|
||||
return new OctreeNode(this.center, this.size).add(this.body).add(body);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void preCalc() {
|
||||
this.pseudoBody = new Body(this.body);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Vector getForcesOnBody(Body body, double t) {
|
||||
return body.gravitationalForce(pseudoBody);
|
||||
|
@ -22,7 +22,7 @@ public class Simulation {
|
||||
|
||||
// set some system parameters
|
||||
public static final double SECTION_SIZE = 2 * AU; // the size of the square region in space
|
||||
public static final int NUMBER_OF_BODIES = 22;
|
||||
public static final int NUMBER_OF_BODIES = 220;
|
||||
public static final double OVERALL_SYSTEM_MASS = 20 * SUN_MASS; // kilograms
|
||||
|
||||
public static void main(String[] args) {
|
||||
@ -68,7 +68,7 @@ public class Simulation {
|
||||
}
|
||||
octree.applyForces(bodies, 1);
|
||||
|
||||
if ((seconds % 3600) == 0) {
|
||||
if ((seconds % 60) == 0) {
|
||||
cd.clear(Color.BLACK);
|
||||
for (Body body : bodies) {
|
||||
body.draw(cd);
|
||||
|
Reference in New Issue
Block a user