-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBody.java
More file actions
152 lines (126 loc) · 4.46 KB
/
Body.java
File metadata and controls
152 lines (126 loc) · 4.46 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**********************************************************************************
*
* GOAL: complete all methods below!
*
**********************************************************************************/
import java.util.Scanner;
public class Body {
public final double SCALE = 0.042; // scalar for star size
protected double rx; // x position
protected double ry; // y position
protected double mass; // mass
protected double size; // size
protected double maxSize; // max size any object can be - use for Nova selector
protected boolean removed = false; // removed status
private double vx; // x velocity
private double vy; // y velocity
private String image; // png image
private double fx; // x force
private double fy; // y force
/**********************************************************************************
* Constructors
**********************************************************************************/
// create and init a new object with input parameters scanned from a .txt file
public Body(Scanner scan, double R) {
rx = scan.nextDouble();
ry = scan.nextDouble();
vx = scan.nextDouble();
vy = scan.nextDouble();
mass = scan.nextDouble();
size = Math.random() * SCALE * R;
image = scan.next();
if (image.equals("blackhole.gif")) {
size = SCALE * R;
}
maxSize = SCALE * R;
}
// create and (re)init a new object with inputs gathered from a Nova'd Star
public Body(double[] arr, double R) {
rx = arr[0];
ry = arr[1];
vx = arr[2];
vy = arr[3];
mass = arr[4];
fx = arr[5];
fy = arr[6];
size = SCALE * R;
maxSize = SCALE * R;
}
/**********************************************************************************
* Modifiers
**********************************************************************************/
// set fx & fy to 0
public void zeroF() {
fx = 0;
fy = 0;
}
// update fx && fy with the additive gravitational force
public void updateF(double dx, double dy, double other_mass, double G) {
double rad = Math.sqrt(dx * dx + dy * dy);
double Force = 0;
if (rad > 0) {
Force = G * mass * other_mass / (rad * rad);
fx += Force * dx / rad;
fy += Force * dy / rad;
}
}
// update fx && fy with the additive gravitational force
public void updateF(Body obj, double G) {
double dx = obj.rx - rx;
double dy = obj.ry - ry;
double rad = Math.sqrt(dx * dx + dy * dy);
double Force = 0;
if (rad > 0) {
Force = G * mass * obj.mass / (rad * rad);
fx += Force * dx / rad;
fy += Force * dy / rad;
}
}
// update this object's position and velocity
public void step(double dt) {
vx += dt * fx / mass;
vy += dt * fy / mass;
rx += dt * vx;
ry += dt * vy;
}
// update image
public void newImage(String imageName) {
image = imageName;
}
// update size
public void newSize(double multiplier) {
size *= multiplier;
}
/**********************************************************************************
* Accessors
**********************************************************************************/
// return an array with all necessary parameters to reinit as a new celestial object
public double[] params() {
double[] arr = {rx, ry, vx, vy, mass, fx, fy};
return arr;
}
// return image
public String getImage() {
return image;
}
// return the mass of this object
public double getMass() {
return mass;
}
// return the rx of this object
public double getRx() {
return rx;
}
// return the ry of this object
public double getRy() {
return ry;
}
// return removed status
public boolean isRemoved() {
return removed;
}
// draw this object using it's rx, ry, and image data
public void draw() {;
StdDraw.picture(rx, ry, image, size, size);
}
}