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