-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolleyballNet.java
More file actions
46 lines (32 loc) · 1.35 KB
/
VolleyballNet.java
File metadata and controls
46 lines (32 loc) · 1.35 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
import greenfoot.*;
public class VolleyballNet extends Actor {
private GreenfootImage netImage;
private int width;
private int height;
public VolleyballNet() {
netImage = new GreenfootImage("volleyball_net.png");
width = netImage.getWidth();
height = netImage.getHeight();
setImage(netImage);
}
public boolean isTouchingNet(Volleyball ball) {
int bx = ball.getX();
int by = ball.getY();
int nx = getX();
int ny = getY();
int halfW = width / 2;
int halfH = height / 2;
int sideThickness = 10;
int topThickness = 20;
int leftPoleX = nx - halfW;
int rightPoleX = nx + halfW;
int topY = ny - halfH;
boolean hitTop = by > topY - topThickness && by < topY + topThickness &&
bx > nx - halfW && bx < nx + halfW;
boolean hitLeft = bx > leftPoleX - sideThickness && bx < leftPoleX + sideThickness &&
by > ny - halfH && by < ny + halfH;
boolean hitRight = bx > rightPoleX - sideThickness && bx < rightPoleX + sideThickness &&
by > ny - halfH && by < ny + halfH;
return hitTop || hitLeft || hitRight;
}
}