-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBody.java
More file actions
79 lines (67 loc) · 2.42 KB
/
Body.java
File metadata and controls
79 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.awt.Color;
public class Body {
private static final double G = 6.67e-11; // gravational constant
private double rx, ry; // position
private double vx, vy; // velocity
private double fx, fy; // force
private double mass; // mass
private Color color; // color
// create and initialize a new Body
public Body(double rx, double ry, double vx, double vy, double mass, Color color) {
this.rx = rx;
this.ry = ry;
this.vx = vx;
this.vy = vy;
this.mass = mass;
this.color = color;
}
// update the velocity and position of the invoking Body
// uses leapfrom method, as in Assignment 2
public void update(double dt) {
vx += dt * fx / mass;
vy += dt * fy / mass;
rx += dt * vx;
ry += dt * vy;
}
// return the Euclidean distance bewteen the invoking Body and b
public double distanceTo(Body b) {
double dx = rx - b.rx;
double dy = ry - b.ry;
return Math.sqrt(dx*dx + dy*dy);
}
// reset the force of the invoking Body to 0
public void resetForce() {
fx = 0.0;
fy = 0.0;
}
// compute the net force acting between the invoking body a and b, and
// add to the net force acting on the invoking Body
public void addForce(Body b) {
Body a = this;
double EPS = 1; // softening parameter
double dx = b.rx - a.rx;
double dy = b.ry - a.ry;
double dist = Math.sqrt(dx*dx + dy*dy);
double F = (G * a.mass * b.mass) / (dist*dist + EPS*EPS);
a.fx += F * dx / dist;
a.fy += F * dy / dist;
}
// draw the invoking Body in Turtle graphics
public void draw() {
StdDraw.setPenColor(color);
StdDraw.point(rx, ry);
}
// convert to string representation formatted nicely
public String toString() {
return String.format("%10.3E %10.3E %10.3E %10.3E %10.3E", rx, ry, vx, vy, mass);
}
//Use the Quad class method to check bounds!
public boolean in(Quad q) {
return q.contains(this.rx,this.ry);
}
//add two bodies together, ie update center of mass and total mass of body, x is target
public Body add(Body x, Body b) {
Body xfinal = new Body((x.rx*x.mass + b.rx*b.mass)/(x.mass + b.mass), (x.ry*x.mass + b.ry*b.mass)/(x.mass + b.mass), x.vx, x.vy, (x.mass + b.mass), x.color);
return xfinal;
}
}