-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoon.java
More file actions
52 lines (43 loc) · 1.61 KB
/
Moon.java
File metadata and controls
52 lines (43 loc) · 1.61 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Moon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Moon extends RigidBody
{
private final static double M = 5.972e24;
private final static double G = 6.674e-11;
private final static double m = 7.34767e22;
private final static double r = 3.48e8;
private final double earthX = 600;
private final double earthY = 340;
private double massInMoons;
private double radiusMultiple;
private double radius;
public Moon(double x, double y, double massInMoons, double radiusMultiple) {
super(x, y, massInMoons * m);
this.massInMoons = massInMoons;
this.force = new Force(0, 0);
this.radiusMultiple = radiusMultiple;
this.radius = radiusMultiple * r;
createImage();
}
public void createImage() {
getImage().scale((int) massInMoons * 50, (int) massInMoons * 50);
}
@Override
protected void updatePosition() {
double theta = Math.atan2(position.getY() - earthY, position.getX() - earthX);
double magnitude = G * M * mass / (radius * radius);
force = new Force(magnitude * Math.cos(theta), magnitude * Math.sin(theta));
acceleration = force.getAcceleration(mass);
velocity.updateWithAcceleration(acceleration, dt);
position.updateWithVelocity(velocity, dt);
}
public void setPosition(double mass, double radius) {
this.mass = mass * m;
this.radius = radius * r;
}
}