-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanet.java
More file actions
118 lines (100 loc) · 2.74 KB
/
Planet.java
File metadata and controls
118 lines (100 loc) · 2.74 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
public class Planet {
public double xxPos; //Its current x position
public double yyPos; //Its current y position
public double xxVel; //Its current velocity in the x direction
public double yyVel; //Its current velocity in the y direction
public double mass; //Its mass
public String imgFileName; //The name of an image in the images directory that depicts the planet
public Planet(double xP, double yP, double xV,
double yV, double m, String img) {
xxPos = xP;
yyPos = yP;
xxVel = xV;
yyVel = yV;
mass = m;
imgFileName = img;
}
public Planet(Planet p) {
xxPos = p.xxPos;
yyPos = p.yyPos;
xxVel = p.xxVel;
yyVel = p.yyVel;
mass = p.mass;
imgFileName = p.imgFileName;
}
public double calcDistance(Planet p){
double d;
d= (xxPos-p.xxPos)*(xxPos-p.xxPos)+(yyPos-p.yyPos)*(yyPos-p.yyPos);
d= java.lang.Math.sqrt(d);
return d;
}
public double calcForceExertedBy( Planet p)
{
double G = 6.67*Math.pow(10.0,-11.0);
double d = this.calcDistance(p);
double F = (G*mass*p.mass)/(d*d);
return F;
}
public double calcForceExertedByX (Planet p)
{
double F = this.calcForceExertedBy(p);
double r = this.calcDistance(p);
double dx = p.xxPos - xxPos;
double Fx= F*(dx/r);
return Fx;
}
public double calcForceExertedByY (Planet p)
{
double F = this.calcForceExertedBy(p);
double r = this.calcDistance(p);
double dy = p.yyPos - yyPos;
double Fy= F*(dy/r);
return Fy;
}
public double calcNetForceExertedByX(Planet[] mix)
{
double NetFx = 0;
for (int i = 0; i< mix.length;i++)
{
if (this.equal(mix[i])!=true) {
NetFx += this.calcForceExertedByX(mix[i]);
}
}
return NetFx;
}
public double calcNetForceExertedByY(Planet[] mix)
{
double NetFy = 0;
for (int i = 0; i< mix.length;i++)
{
if (this.equal(mix[i])!=true) {
NetFy += this.calcForceExertedByY(mix[i]);
}
}
return NetFy;
}
public boolean equal(Planet p)
{
if (this == p)
{
return true;
}
else{
return false;
}
}
public void update (double dt,double fX, double fY)
{
double ax = fX/this.mass;
double ay = fY/this.mass;
xxVel = xxVel + ax*dt;
yyVel = yyVel + ay*dt;
xxPos = xxPos + xxVel*dt;
yyPos = yyPos + yyVel*dt;
}
public void draw ()
{
String file_loc = "images/"+imgFileName;
StdDraw.picture(xxPos,yyPos,file_loc);
}
}