-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.js
92 lines (77 loc) · 2.29 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
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
//var gravity = 0.05;
//var mHeight = 100;
var randColor = function () { return '#' + (Math.random() * 0xFFFFFF << 0).toString(16); }
var square = function (x) { return x * x; }
function Ball() {
this.radius = 10 + 10 * Math.random();
this.x = this.radius + (mWidth - 2 * this.radius) * Math.random();
this.y = (mHeight - this.radius) * Math.random();
this.vx = 4 + 8 * Math.random();
if (Math.random() > 0.5)
this.vx *= -1;
this.vy = 6 * (Math.random() - 0.5);
//the energy, /m, h relative to the top level, without vx
this.calcE();
this.color = randColor();
};
Ball.prototype.calcE = function () {
/*
* calculate according to the formula:
* E = 1/2 m v^2 + m g h
* and h relative to the top of the page.
*/
this.E = square(this.vy) / 2 - this.y * gravity;
}
Ball.prototype.setY = function (y) {
this.y = y;
this.calcE();
}
Ball.prototype.setVY = function (vy) {
this.vy = vy;
this.calcE();
}
Ball.prototype.draw = function (ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
};
Ball.prototype.move = function () {
var cc = false;
if (this.x - this.radius < 0 || this.x + this.radius > mWidth) {
//Bounce back from side
this.vx *= -1;
cc = true;
}
if (this.y + this.radius > mHeight && this.vy > 0) {
//Bounce back from top
this.vy *= -1;
this.y = mHeight - this.radius;
var counter = document.getElementById('jumpCounter');
counter.innerHTML++;
if (this.vy > -gravity)
//it is too slow
this.vy = (-5 - 25 * Math.random()) * gravity;
else
cc = true;
}
if (this.y - this.radius < 0 && this.vy < 0) {
cc = true;
//Bounce back from botton
this.vy *= -1;
}
if (cc) {
this.color = randColor();
}
this.vy += gravity;
this.x += this.vx;
if (gravity != 0) {
var Ep = this.E - square(this.vy) / 2;
this.y = -Ep / gravity;
} else this.y += this.vy;
};
Ball.prototype.crash = function (other) {
var distance = Math.sqrt(square(this.x - other.x) + square(this.y - other.y));
return distance <= this.radius + other.radius;
};