-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanet.java
95 lines (85 loc) · 2.44 KB
/
Planet.java
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
public class Planet {
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
public static final double G = 6.67e-11;
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)
{
return Math.sqrt(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
}
public double calcForceExertedBy(Planet p)
{
return G*mass*p.mass/(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
}
public double calcForceExertedByX(Planet p)
{
double r = Math.sqrt(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
return (p.xxPos-xxPos)*G*mass*p.mass/Math.pow(r,3);
}
public double calcForceExertedByY(Planet p)
{
double r = Math.sqrt(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
return (p.yyPos-yyPos)*G*mass*p.mass/Math.pow(r,3);
}
public double calcNetForceExertedByX(Planet [] ps)
{
double net = 0;
for(Planet p : ps)
{
if(!this.equals(p))
{
double r = Math.sqrt(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
net += (p.xxPos-xxPos)*G*mass*p.mass/Math.pow(r,3);
}
}
return net;
}
public double calcNetForceExertedByY(Planet [] ps)
{
double net = 0;
for(Planet p : ps)
{
if(!this.equals(p))
{
double r = Math.sqrt(Math.pow(p.xxPos - xxPos, 2) + Math.pow(p.yyPos - yyPos, 2));
net += (p.yyPos-yyPos)*G*mass*p.mass/Math.pow(r,3);
}
}
return net;
}
public void update(double dt, double xForce, double yForce)
{
double xA = xForce/mass, yA = yForce/mass;
xxVel += xA*dt;
yyVel += yA*dt;
xxPos += xxVel*dt;
yyPos += yyVel*dt;
}
public void draw()
{
StdDraw.picture(xxPos, yyPos, "images/" + imgFileName);
StdDraw.show();
}
};