-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shell.java
108 lines (89 loc) · 2.9 KB
/
Shell.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
96
97
98
99
100
101
102
103
104
105
106
107
108
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Shell here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Shell extends Actor
{
/**
* Act - do whatever the Shell wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Earth earth;
private Ground ground;
/** Physical quantities that will change over time*/
private int angle;
private int xPos;
private int yPos;
/** X-direction Projectile motion quantities that are constant*/
private double v_x;
private double x_i;
/** Y-direction Projectile motion quantities that are constant*/
private double a_y = 9.81; // need to flip sign because y is inverted
private double v_iy1;
private double v_iy2;
private double y_i;
/** Projectile motion quantities that are changing over time*/
private double v_y;
private int direction = 1;
/** Flight time*/
private int t = 0;
private boolean is_right;
public Shell(Ground ground,int angle,boolean is_right) {
this.ground = ground;
this.angle = angle;
this.is_right = is_right;
v_iy1 = -65*Math.sin(Math.toRadians(angle));
v_iy2 = -75*Math.sin(Math.toRadians(angle));
v_x = -65*Math.cos(Math.toRadians(angle));
//System.out.println("V_iy:" + ((angle > 50) ? v_iy2 : v_iy1));
if (is_right) {
v_x *= -1;
getImage().mirrorHorizontally();
}
turn(angle);
}
public void act()
{
if (t == 0) System.out.println("Angle:" + angle + " Slope:" + getSlope());
if (!checkCollision()) {
move();
t++;
}
}
public boolean checkCollision() {
//System.out.println("Y:" + getY() + " Rel Y:" + theGround.getRelativeY(getX()));
if (getY() >= ground.getRelativeY(getX()) || getX() == 0 || getX() == 599) {
earth.addObject(new Boom(), getX(),getY());
earth.removeObject(this);
return true;
}
else return false;
}
public void move() {
System.out.println(getX());
setRotation((int)getSlope());
setLocation((int)calcXPos(),(int)calcYPos());
}
public double calcYPos() {
return 0.5*a_y*Math.pow(t/30.0,2) + v_iy1*(t/30.0) + y_i;
}
public double calcXPos() {
return v_x*(t/30.0) + x_i;
}
public double getSlope() {
v_y = a_y*(t/30.0) + v_iy1; //need to flip b/c y is flipped
if(!is_right) v_y *= -1;
return v_y;
}
public void addedToWorld(World world)
{
xPos = getX();
x_i = getX();
yPos = getY();
y_i = getY();
earth = (Earth) world;
}
}