-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.java
197 lines (162 loc) · 3.89 KB
/
Ball.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.awt.*;
public class Ball
{
public double radius, mass, bounce, angle;
public Color color;
public double velocityX, velocityY, velocity, keY, keX;
public int x, y;
public Ball(double radius, double mass, double bounce, double angle, Color color)
{
this.radius = radius;
this.mass = mass;
this.bounce = bounce;
this.angle = angle;
this.color = color;
}
public Ball(double radius, Color color)
{
this.radius = radius;
this.color = color;
}
public void findVelocity(double distanceX, double distanceY, double time)
{
velocityX = distanceX / time;
velocityY = distanceY / time;
velocity = (Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2))) / time;
kineticEnergy();
}
public void ballMiddle(int x, int y)
{
this.x = x;
this.y = y;
}
public void addGravity(double gravity, double FPS)
{
velocityY += (gravity * (1000/(FPS)));
kineticEnergy();
}
public void move()
{
x += velocityX;
y += velocityY;
}
public void kineticEnergy()
{
keY = (0.5 * mass * (Math.pow(velocityY, 2)));
keX = (0.5 * mass * (Math.pow(velocityX, 2)));
}
public void hitLeft() //remove when wall object made
{
kineticEnergy();
keX *= bounce;
velocityX = (Math.sqrt(keX / (0.5 * mass)));
kineticEnergy();
}
public void hitRight() //remove when wall object made
{
kineticEnergy();
keX *= bounce;
velocityX = (Math.sqrt(keX / (0.5 * mass)));
velocityX *= -1;
kineticEnergy();
}
public void hitTop() //remove when wall object made
{
kineticEnergy();
keY *= bounce;
velocityY = (Math.sqrt(keY / (0.5 * mass)));
kineticEnergy();
}
public void hitBottom() //remove when wall object made
{
kineticEnergy();
keY *= bounce;
velocityY = (Math.sqrt(keY / (0.5 * mass)));
velocityY *= -1;
kineticEnergy();
}
public void frictionLeft(double friction) //remove when wall object made
{
velocityX = velocityX + ((friction / mass) * 0.01);
}
public void frictionRight(double friction) //remove when wall object made
{
velocityX = velocityX + (((friction * -1) / mass) * 0.01);
}
public void zeroVelocityX()
{
velocityX = 0;
}
public void fixBottom(int screenY)
{
y = screenY - (int)radius;
}
public void fixTop()
{
y = 35 + (int)radius;
}
public void fixLeft()
{
x = (int)radius;
}
public void fixRight(int screenX)
{
x = screenX - (int)radius;
}
public double getRadius()
{
return radius;
}
public double getDiameter()
{
return (radius * 2);
}
public double getMass()
{
return mass;
}
public double getBounce()
{
return bounce;
}
public double getAngle()
{
return angle;
}
public Color getColor()
{
return color;
}
public double getVelocityX()
{
return velocityX;
}
public double getVelocityY()
{
return velocityY;
}
public double getVelocity()
{
return velocity;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public double getKEY()
{
return keY;
}
public double getKEX()
{
return keX;
}
public void paint(Graphics buffer)
{
buffer.fillOval((int)getX() - (int)getRadius(), (int)getY() - (int)getRadius(), (int)getDiameter(), (int)getDiameter());
}
}