-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.js
45 lines (32 loc) · 1.29 KB
/
wall.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
function WallObject(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
WallObject.prototype = {
collisionBall: function(ball) {
if (((ball.p.y + ball.v.y - (ball.r / 2) <= this.y + this.h) &&
(ball.p.y - (ball.r / 2) >= this.y + this.h)) ||
((ball.p.y + ball.v.y + (ball.r / 2) >= this.y) &&
(ball.p.y + (ball.r / 2) <= this.y ))) {
if (ball.p.x + ball.v.x + (ball.r / 2) >= this.x &&
ball.p.x + ball.v.x - (ball.r / 2)<= this.x + this.w){
ball.p.y = ball.p.y + ball.v.y;
if (ball.v.y < 0) ball.p.y += ball.r / 2; else ball.p.y -= ball.r / 2;
ball.v.y = -ball.v.y;
}
}
if (((ball.p.x + ball.v.x - (ball.r/2) <= this.x + this.w) &&
(ball.p.x - (ball.r/2) >= this.x + this.w)) ||
((ball.p.x + ball.v.x + (ball.r/2) >= this.x) &&
(ball.p.x + (ball.r/2) <= this.x ))) {
if (ball.p.y + ball.v.y + (ball.r / 2) >= this.y &&
ball.p.y + ball.v.y - (ball.r / 2)<= this.y + this.h) {
ball.p.x = ball.p.x + ball.v.x;
if (ball.v.x < 0) ball.p.x += ball.r / 2; else ball.p.x -= ball.r / 2;
ball.v.x = -ball.v.x;
}
}
}
}