-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
133 lines (111 loc) · 2.08 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
import java.awt.Color;
import java.awt.Graphics;
public class Ball {
public Ball b;
private int x = 40;
private int y = 25;
private int radius = 20;
private double dx = 10;
private double dy = 0;
private double gravity = 15;
private double energyloss = 1;
private double dt = .2;
private double xFriction = .9;
private int agility = 3;
private int maxSpeed = 20;
public int getAgility() {
return agility;
}
public void setAgility(int agility) {
this.agility = agility;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public Ball(int i, int j) {
// TODO Auto-generated constructor stub
x = i;
y = j;
}
public int getRadius() {
return radius;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double getDx() {
return dx;
}
public double getDy() {
return dy;
}
public void setDx(double dx) {
this.dx = dx;
}
public void setDy(double dy) {
this.dy = dy;
}
public double getGravity() {
return gravity;
}
public void setGravity(double gravity) {
this.gravity = gravity;
}
public Ball() {
// TODO Auto-generated constructor stub
}
public void moveRight() {
if (dx+ agility < maxSpeed){
dx += agility;
}
}
public void moveLeft() {
if (dx- agility > - maxSpeed){
dx -= agility;
}
}
public void update(Starthere sp){
if (x + dx>sp.getWidth()-radius-1){
x = sp.getWidth()-radius -1;
dx = -dx;
}else if(x + dx < 0 + radius){
x = 0+radius;
dx = -dx;
}
else{
x += dx;
}
if(y == sp.getHeight()-radius-1){
dx*= xFriction;
if (Math.abs(dx) <.8){
dx = 0;
}
}
if(y > sp.getHeight()-radius-1){
y = sp.getHeight()-radius-1;
dy *= energyloss;
dy = -dy;
}else{
//velocity formula
dy = dy + gravity *dt;
//position formula
y += dy*dt +.5*gravity*dt*dt;
}
}
public void paint(Graphics g){
g.setColor(Color.cyan);
g.fillOval(x-radius, y-radius, radius*2, radius*2);
}
}