-
Notifications
You must be signed in to change notification settings - Fork 3
/
ball.js
46 lines (40 loc) · 1.15 KB
/
ball.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
function Ball() {
this.pos = createVector(width / 2, height / 2);
this.r = 30;
this.vel = createVector(1, 1).mult(4);
this.direction = createVector(1, 1);
this.update = function() {
this.pos.x += this.vel.x * this.direction.x;
this.pos.y += this.vel.y * this.direction.y;
}
this.display = function() {
ellipse(this.pos.x, this.pos.y, this.r * 2, this.r * 2);
}
this.checkEdges = function() {
if (this.pos.x > width - this.r && this.direction.x > 0) {
this.direction.x *= -1;
}
if (this.pos.x < this.r && this.direction.x < 0) {
this.direction.x *= -1;
}
if (this.pos.y < this.r && ball.direction.y < 0) this.direction.y *= -1;
}
this.meets = function(paddle) {
if (this.pos.y < paddle.pos.y &&
this.pos.y > paddle.pos.y - this.r &&
ball.pos.x > paddle.pos.x - ball.r &&
ball.pos.x < paddle.pos.x + paddle.w + ball.r) {
return true;
} else {
return false;
}
}
this.hits = function(brick) {
var d = dist(this.pos.x, this.pos.y, brick.pos.x, brick.pos.y);
if (d < brick.r + this.r) {
return true;
} else {
return false;
}
}
}