-
Notifications
You must be signed in to change notification settings - Fork 2
/
BasketballCourt.pde
284 lines (237 loc) · 9.94 KB
/
BasketballCourt.pde
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
public class BasketballCourt {
private final static float nbaLength = 94;
private final static float nbaWidth = 50;
private final static float camHeight = 25.45932;
private final static int numPartsCircle = 100;
private int x;
private int y;
private float ratio;
private float courtLength;
private float courtWidth;
private float centreCircleDiam;
private float laneLength;
private float laneInnerWidth;
private float laneOutterWidth;
private float threePtRectLength;
private float threePtRectWidth;
private float threePtArcWidth;
private float basketDiam;
private float basketArc;
private float backboardWidth;
private float backboardOffX;
private float playerDiam;
private float ballDiam;
private PImage floorTexture;
private PImage floorTexture2;
private PImage floorTexture3;
private PlayerPosition[] homeTeam;
private PlayerPosition[] visitingTeam;
private GameEvent gameEvent;
BasketballCourt() {
float ratio = 400 / nbaLength;
init(ratio);
}
BasketballCourt(float ratio) {
init(ratio);
}
BasketballCourt(float cLength, float cWidth, GameEvent gameEvent) {
init(min(cLength/nbaLength, cWidth/nbaWidth));
this.gameEvent = gameEvent;
}
public float getHeight() {
return this.courtWidth;
}
public float getWidth() {
return this.courtLength;
}
private void init(float ratio) {
this.x = (int) modelX(0, 0, 0);
this.y = (int) modelY(0, 0, 0);
this.ratio = ratio;
courtLength = nbaLength * ratio;
courtWidth = nbaWidth * ratio;
centreCircleDiam = 12 * ratio;
laneLength = 19 * ratio;
laneInnerWidth = 12 * ratio;
laneOutterWidth = 16 * ratio;
threePtRectLength = 14 * ratio;
threePtRectWidth = 44 * ratio;
threePtArcWidth = 23.75 * ratio;
basketDiam = 1.5 * ratio;
basketArc = 4 * ratio;
backboardWidth = 6 * ratio;
backboardOffX = 4 * ratio;
playerDiam = 2.8*ratio;
ballDiam = playerDiam*0.6;
}
public void setup() {
this.floorTexture = loadImage("images/floor-texture.jpg");
this.floorTexture2 = loadImage("images/floor-texture2.jpg");
this.floorTexture3 = loadImage("images/floor-texture3.jpg");
}
public void draw() {
this.x = (int) modelX(0, 0, 0);
this.y = (int) modelY(0, 0, 0);
stroke(255);
strokeWeight(3);
beginShape(); // Court
texture(this.floorTexture);
textureWrap(REPEAT);
vertex(0, 0, 0, 0);
vertex(courtLength, 0, courtLength, 0);
vertex(courtLength, courtWidth, courtLength, courtWidth);
vertex(0, courtWidth, 0, courtWidth);
vertex(0, 0, 0, 0);
endShape();
line (courtLength/2, 0, courtLength/2, courtWidth); // Division line
float theta = TWO_PI / numPartsCircle;
beginShape(); // Centre outter circle
texture(this.floorTexture2);
textureWrap(REPEAT);
for (int i=0; i<numPartsCircle; i++) {
float angle = theta * i;
float x = cos(angle);
float y = sin(angle);
vertex(courtLength/2 + x * centreCircleDiam/2,
courtWidth/2 + y * centreCircleDiam/2, courtLength/2 + x * centreCircleDiam/2,
courtWidth/2 + y * centreCircleDiam/2);
}
endShape();
beginShape(); // Centre inner circle
texture(this.floorTexture3);
textureWrap(REPEAT);
for (int i=0; i<numPartsCircle; i++) {
float angle = theta * i;
float x = cos(angle);
float y = sin(angle);
vertex(courtLength/2 + x * centreCircleDiam/6,
courtWidth/2 + y * centreCircleDiam/6, courtLength/2 + x * centreCircleDiam/6,
courtWidth/2 + y * centreCircleDiam/6);
}
endShape();
// Left side
noFill();
line(0, courtWidth/2-threePtRectWidth/2,
threePtRectLength, courtWidth/2-threePtRectWidth/2); // Three point line 1
line(0, courtWidth/2-threePtRectWidth/2+threePtRectWidth,
threePtRectLength, courtWidth/2-threePtRectWidth/2+threePtRectWidth); // Three point line 2
arc(backboardOffX+basketDiam/2, courtWidth/2, // Three point arc
threePtArcWidth*2, threePtRectWidth * 1.084, radians(-67), radians(67));
ellipse(laneLength, courtWidth/2, laneInnerWidth, laneInnerWidth); // Free throw circle
beginShape(); // Outter lane
texture(this.floorTexture2);
textureWrap(REPEAT);
vertex(0, courtWidth/2-laneOutterWidth/2, 0, courtWidth/2-laneOutterWidth/2);
vertex(laneLength, courtWidth/2-laneOutterWidth/2, laneLength, courtWidth/2-laneOutterWidth/2);
vertex(laneLength, courtWidth/2+laneOutterWidth/2, laneLength, courtWidth/2+laneOutterWidth/2);
vertex(0, courtWidth/2+laneOutterWidth/2, 0, courtWidth/2+laneOutterWidth/2);
vertex(0, courtWidth/2-laneOutterWidth/2, 0, courtWidth/2-laneOutterWidth/2);
endShape();
beginShape(); // Inner lane
texture(this.floorTexture3);
textureWrap(REPEAT);
vertex(0, courtWidth/2-laneInnerWidth/2, 0, courtWidth/2-laneInnerWidth/2);
vertex(laneLength, courtWidth/2-laneInnerWidth/2, laneLength, courtWidth/2-laneInnerWidth/2);
vertex(laneLength, courtWidth/2+laneInnerWidth/2, laneLength, courtWidth/2+laneInnerWidth/2);
vertex(0, courtWidth/2+laneInnerWidth/2, 0, courtWidth/2+laneInnerWidth/2);
vertex(0, courtWidth/2-laneInnerWidth/2, 0, courtWidth/2-laneInnerWidth/2);
endShape();
line(backboardOffX, courtWidth/2-backboardWidth/2,
backboardOffX, courtWidth/2+backboardWidth/2); // Backboard
arc(backboardOffX+basketDiam/2, courtWidth/2, // Basket arc
basketArc*2, basketArc*2, radians(-90), radians(90));
ellipse(backboardOffX+basketDiam/2, courtWidth/2, basketDiam, basketDiam); // Basket
// Right side
line(courtLength-threePtRectLength, courtWidth/2-threePtRectWidth/2,
courtLength, courtWidth/2-threePtRectWidth/2); // Three point line 1
line(courtLength-threePtRectLength, courtWidth/2+threePtRectWidth/2,
courtLength, courtWidth/2+threePtRectWidth/2); // Three point line 2
arc(courtLength-backboardOffX-basketDiam/2, courtWidth/2, // Three point arc
threePtArcWidth*2, threePtRectWidth * 1.084, radians(113), radians(247));
ellipse(courtLength-laneLength, courtWidth-courtWidth/2,
laneInnerWidth, laneInnerWidth); // Free throw circle
beginShape(); // Outter lane
texture(this.floorTexture2);
textureWrap(REPEAT);
vertex(courtLength-laneLength, courtWidth/2-laneOutterWidth/2,
courtLength-laneLength, courtWidth/2-laneOutterWidth/2);
vertex(courtLength, courtWidth/2-laneOutterWidth/2,
courtLength, courtWidth/2-laneOutterWidth/2);
vertex(courtLength, courtWidth/2+laneOutterWidth/2,
courtLength, courtWidth/2+laneOutterWidth/2);
vertex(courtLength-laneLength, courtWidth/2+laneOutterWidth/2,
courtLength-laneLength, courtWidth/2+laneOutterWidth/2);
vertex(courtLength-laneLength, courtWidth/2-laneOutterWidth/2,
courtLength-laneLength, courtWidth/2-laneOutterWidth/2);
endShape();
beginShape(); // Inner lane
texture(this.floorTexture3);
textureWrap(REPEAT);
vertex(courtLength-laneLength, courtWidth/2-laneInnerWidth/2,
courtLength-laneLength, courtWidth/2-laneInnerWidth/2);
vertex(courtLength, courtWidth/2-laneInnerWidth/2,
courtLength, courtWidth/2-laneInnerWidth/2);
vertex(courtLength, courtWidth/2+laneInnerWidth/2,
courtLength, courtWidth/2+laneInnerWidth/2);
vertex(courtLength-laneLength, courtWidth/2+laneInnerWidth/2,
courtLength-laneLength, courtWidth/2+laneInnerWidth/2);
vertex(courtLength-laneLength, courtWidth/2-laneInnerWidth/2,
courtLength-laneLength, courtWidth/2-laneInnerWidth/2);
endShape();
line(courtLength-backboardOffX, courtWidth/2-backboardWidth/2,
courtLength-backboardOffX, courtWidth/2+backboardWidth/2); // Backboard
arc(courtLength-backboardOffX-basketDiam/2, courtWidth/2, // Basket arc
basketArc*2, basketArc*2, radians(90), radians(270));
ellipse(courtLength-backboardOffX-basketDiam/2, courtWidth/2, basketDiam, basketDiam); // Basket
}
public boolean mouseOver(PlayerPosition p) {
if(dist(mouseX, mouseY, x+p.getX()*ratio, y+p.getY()*ratio) < playerDiam*0.5)
return true;
return false;
}
public void mouseEvent(int eventType) {
if(eventType == MOUSE_CLICKED) {
for(PlayerPosition p : homeTeam) {
if(dist(mouseX, mouseY, x+p.getX()*ratio, y+p.getY()*ratio) < playerDiam*0.5)
SWITCH_WINDOW(new PlayerWindow(p.getPlayer(), this.gameEvent));
}
for(PlayerPosition p : visitingTeam) {
if(dist(mouseX, mouseY, x+p.getX()*ratio, y+p.getY()*ratio) < playerDiam*0.5)
SWITCH_WINDOW(new PlayerWindow(p.getPlayer(), this.gameEvent));
}
}
}
private void drawPlayersAndBall(PlayerPosition[] homeTeam, PlayerPosition[] visitingTeam, float[] ball) {
this.homeTeam = homeTeam;
this.visitingTeam = visitingTeam;
noStroke();
float newBallDiam = camHeight/(camHeight-ball[2])*ballDiam;
// Ball
fill(212*ball[2]*0.6, 113*ball[2]*0.6, 15*ball[2]*0.6);
ellipse(ball[0]*ratio, ball[1]*ratio, newBallDiam, newBallDiam);
// Home team
for(PlayerPosition p : this.homeTeam) {
if(!this.mouseOver(p))
fill(COLOR_HOME_TEAM);
else
fill(0, 0, 0);
ellipse(p.getX()*ratio, p.getY()*ratio, playerDiam, playerDiam);
fill(255);
textFont(FONT_12);
textAlign(CENTER);
text(p.getPlayer().getJerseyNumber(), p.getX()*ratio, (p.getY()+0.35)*ratio);
}
// Visiting teams
for(PlayerPosition p : this.visitingTeam) {
if(!this.mouseOver(p))
fill(COLOR_VISITOR_TEAM);
else
fill(0, 0, 0);
ellipse(p.getX()*ratio, p.getY()*ratio, playerDiam, playerDiam);
fill(255);
textFont(FONT_12);
textAlign(CENTER);
text(p.getPlayer().getJerseyNumber(), p.getX()*ratio, (p.getY()+0.35)*ratio);
}
}
}