-
Notifications
You must be signed in to change notification settings - Fork 126
/
Basketball.js
356 lines (303 loc) · 9.56 KB
/
Basketball.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import React, {
Component,
} from 'react';
import {
StyleSheet,
View,
Dimensions,
} from 'react-native';
import Ball from './components/Ball';
import Hoop from './components/Hoop';
import Net from './components/Net';
import Floor from './components/Floor';
import Emoji from './components/Emoji';
import Score from './components/Score';
import Vector from './utils/Vector';
// physical variables
const gravity = 0.6; // gravity
const radius = 48; // ball radius
const rotationFactor = 10; // ball rotation factor
// components sizes and positions
const FLOOR_HEIGHT = 48;
const FLOOR_Y = 11;
const HOOP_Y = Dimensions.get('window').height - 227;
const NET_HEIGHT = 6;
const NET_WIDTH = 83;
const NET_Y = Dimensions.get('window').height - 216;
const NET_X = (Dimensions.get('window').width / 2) - (NET_WIDTH / 2);
const NET_LEFT_BORDER_X = NET_X + NET_HEIGHT / 2;
const NET_LEFT_BORDER_Y = NET_Y;
const NET_RIGHT_BORDER_X = NET_X + NET_WIDTH - NET_HEIGHT / 2;
const NET_RIGHT_BORDER_Y = NET_LEFT_BORDER_Y;
// ball lifecycle
const LC_WAITING = 0;
const LC_STARTING = 1;
const LC_FALLING = 2;
const LC_BOUNCING = 3;
const LC_RESTARTING = 4;
const LC_RESTARTING_FALLING = 5;
class Basketball extends Component {
constructor(props) {
super(props);
this.interval = null;
// initialize ball states
this.state = {
x: Dimensions.get('window').width / 2 - radius,
y: FLOOR_Y,
vx: 0,
vy: 0,
rotate: 0,
scale: 1,
lifecycle: LC_WAITING,
scored: null,
score: 0,
};
}
componentDidMount() {
this.interval = setInterval(this.update.bind(this), 1000 / 60);
}
componentWillUnmount() {
if (this.interval) {
clearInterval(this.interval);
}
}
onStart(angle) {
if (this.state.lifecycle === LC_WAITING) {
this.setState({
vx: angle * 0.2,
vy: -16,
lifecycle: LC_STARTING,
});
}
}
randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
circlesColliding(circle1, circle2) {
const dx = circle1.x - circle2.x;
const dy = circle1.y - circle2.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < circle1.radius + circle2.radius) {
return true;
}
return false;
}
// Inspired by http://www.adambrookesprojects.co.uk/project/canvas-collision-elastic-collision-tutorial/
updateCollisionVelocity(nextState, ball, netBorder) {
const xDistance = (netBorder.x - ball.x);
const yDistance = (netBorder.y - ball.y);
let normalVector = new Vector(xDistance, yDistance);
normalVector = normalVector.normalise();
const tangentVector = new Vector((normalVector.getY() * -1), normalVector.getX());
// create ball scalar normal direction.
const ballScalarNormal = normalVector.dot(ball.velocity);
const netScalarNormal = normalVector.dot(netBorder.velocity);
// create scalar velocity in the tagential direction.
const ballScalarTangential = tangentVector.dot(ball.velocity);
const ballScalarNormalAfter = (ballScalarNormal * (ball.mass - netBorder.mass) +
2 * netBorder.mass * netScalarNormal) / (ball.mass + netBorder.mass);
const ballScalarNormalAfterVector = normalVector.multiply(ballScalarNormalAfter);
const ballScalarNormalVector = (tangentVector.multiply(ballScalarTangential));
const nextVelocity = ballScalarNormalVector.add(ballScalarNormalAfterVector);
if (ball.y < NET_Y + NET_HEIGHT / 2) {
nextState.vx = nextVelocity.x;
} else {
nextState.vx = -nextVelocity.x;
}
nextState.vy = nextVelocity.y;
nextState.x = this.state.x + nextState.vx;
nextState.y = this.state.y - nextState.vy;
}
handleCollision(nextState) {
if (nextState.lifecycle !== LC_FALLING && nextState.lifecycle !== LC_BOUNCING) return;
const _self = this;
const ball = {
x: nextState.x + radius,
y: nextState.y + radius,
radius: radius * nextState.scale,
velocity: {
getX() {
return _self.state.vx;
},
getY() {
return _self.state.vy;
},
},
mass: 2,
};
const netLeftBorder = {
x: NET_LEFT_BORDER_X,
y: NET_LEFT_BORDER_Y,
radius: NET_HEIGHT / 2,
velocity: {
getX() {
return 0;
},
getY() {
return 0;
},
},
mass: 10,
};
const netRightBorder = {
x: NET_RIGHT_BORDER_X,
y: NET_RIGHT_BORDER_Y,
radius: NET_HEIGHT / 2,
velocity: {
getX() {
return 0;
},
getY() {
return 0;
},
},
mass: 10,
};
const isLeftCollision = this.circlesColliding(ball, netLeftBorder);
if (isLeftCollision) {
nextState.lifecycle = LC_BOUNCING;
this.updateCollisionVelocity(nextState, ball, netLeftBorder);
} else {
const isRightCollision = this.circlesColliding(ball, netRightBorder);
if (isRightCollision) {
nextState.lifecycle = LC_BOUNCING;
this.updateCollisionVelocity(nextState, ball, netRightBorder);
}
}
}
updateVelocity(nextState) {
nextState.vx = this.state.vx;
if (nextState.lifecycle === LC_STARTING && nextState.y < NET_Y - 200) {
nextState.vy = this.state.vy;
} else {
nextState.vy = this.state.vy + gravity;
}
}
updatePosition(nextState) {
nextState.x = this.state.x + nextState.vx;
nextState.y = this.state.y - nextState.vy;
if (nextState.lifecycle === LC_STARTING && nextState.y < this.state.y) {
nextState.lifecycle = LC_FALLING;
}
if (nextState.lifecycle === LC_RESTARTING && nextState.y < this.state.y) {
nextState.lifecycle = LC_RESTARTING_FALLING;
}
if (this.state.scored === null) {
if (this.state.y + radius > NET_Y + NET_HEIGHT / 2 && nextState.y + radius < NET_Y + NET_HEIGHT / 2) {
if (nextState.x + radius > NET_LEFT_BORDER_X && nextState.x + radius < NET_RIGHT_BORDER_X) {
nextState.scored = true;
nextState.score += 1;
} else {
nextState.scored = false;
}
}
}
}
updateScale(nextState) {
if (nextState.lifecycle === LC_BOUNCING || nextState.lifecycle === LC_RESTARTING || nextState.lifecycle === LC_RESTARTING_FALLING) return;
let scale = this.state.scale;
if (scale > 0.4 && this.state.y > FLOOR_HEIGHT) {
scale -= 0.01;
}
nextState.scale = scale;
}
updateRotate(nextState) {
nextState.rotate = this.state.rotate + (nextState.vx * rotationFactor);
}
handleRestart(nextState) {
if (nextState.lifecycle === LC_RESTARTING_FALLING && nextState.y <= FLOOR_Y) {
// in front of the Floor
// will restart to 'Waiting' lifecycle step
nextState.y = FLOOR_Y;
nextState.vx = 0;
nextState.vy = 0;
nextState.rotate = 0;
nextState.scale = 1;
nextState.lifecycle = LC_WAITING;
nextState.scored = null;
}
const outOfScreen = (nextState.x > Dimensions.get('window').width + 100 || nextState.x < 0 - (radius * 2) - 100);
if (
(outOfScreen === true)
|| ((nextState.lifecycle === LC_FALLING || nextState.lifecycle === LC_BOUNCING) && (nextState.y + (radius * nextState.scale * 2) < FLOOR_Y + radius * -2))
) {
if (outOfScreen && nextState.scored === null) {
nextState.scored = false;
}
// behind the Floor
// will be thrown to the front of the floor
nextState.y = FLOOR_Y;
if (nextState.scored === true) {
nextState.x = this.randomIntFromInterval(4, Dimensions.get('window').width - (radius * 2) - 4);
} else {
// nextState.x = Dimensions.get('window').width / 2 - radius;
nextState.x = this.randomIntFromInterval(4, Dimensions.get('window').width - (radius * 2) - 4);
nextState.score = 0;
}
// nextState.x = Dimensions.get('window').width / 2 - radius;
nextState.vy = -8;
nextState.vx = 0;
nextState.scale = 1;
nextState.rotate = 0;
nextState.lifecycle = LC_RESTARTING;
}
}
update() {
if (this.state.lifecycle === LC_WAITING) return;
let nextState = null;
nextState = Object.assign({}, this.state);
this.updateVelocity(nextState);
this.updatePosition(nextState);
this.updateScale(nextState);
this.updateRotate(nextState);
this.handleCollision(nextState);
this.handleRestart(nextState);
this.setState(nextState);
}
renderNet(render) {
if (render === true) {
return (
<Net y={NET_Y} x={NET_X} height={NET_HEIGHT} width={NET_WIDTH} />
);
}
return null;
}
renderFloor(render) {
if (this.state.lifecycle === LC_RESTARTING || this.state.lifecycle === LC_RESTARTING_FALLING) {
render = !render;
}
if (render === true) {
return (
<Floor height={FLOOR_HEIGHT} />
);
}
return null;
}
render() {
return (
<View style={styles.container}>
<Score y={FLOOR_HEIGHT * 3} score={this.state.score} scored={this.state.scored} />
<Hoop y={HOOP_Y} />
{this.renderNet(this.state.lifecycle === LC_STARTING)}
{this.renderFloor(this.state.vy <= 0)}
<Ball
onStart={this.onStart.bind(this)}
x={this.state.x}
y={this.state.y}
radius={radius}
rotate={this.state.rotate}
scale={this.state.scale}
/>
{this.renderNet(this.state.lifecycle !== LC_STARTING)}
{this.renderFloor(this.state.vy > 0)}
<Emoji y={NET_Y} scored={this.state.scored} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default Basketball;