-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
215 lines (197 loc) · 5.96 KB
/
index.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
document.addEventListener("DOMContentLoaded", function () {
function Ball(x, y, radius) {
this.radius = radius;
this.dx = 5;
this.dy = 5;
// mass is that of a sphere as opposed to circle.
// it *does* make a difference.
this.mass = this.radius * this.radius * this.radius;
this.x = x;
this.y = y;
this.color = randomColor();
this.draw = function () {
ctx.beginPath();
ctx.arc(
Math.round(this.x),
Math.round(this.y),
this.radius,
0,
2 * Math.PI
);
ctx.fillStyle = this.color;
ctx.fill();
ctx.strokeStyle = "rgba(0, 0, 0, 0.6)";
ctx.stroke();
ctx.closePath();
};
this.speed = function () {
// magnitude of velocity vector
return Math.sqrt(this.dx * this.dx + this.dy * this.dy);
};
this.angle = function () {
//angle of ball with the x axis
return Math.atan2(this.dy, this.dx);
};
this.kineticEnergy = function () {
// only for masturbation purposes, not rly used for computation.
return 0.5 * this.mass * this.speed() * this.speed();
};
this.onGround = function () {
return this.y + this.radius >= canvas.height;
};
}
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var objArray = [];
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function wallCollision(ball) {
if (
ball.x - ball.radius + ball.dx < 0 ||
ball.x + ball.radius + ball.dx > canvas.width
) {
ball.dx *= -1;
}
if (
ball.y - ball.radius + ball.dy < 0 ||
ball.y + ball.radius + ball.dy > canvas.height
) {
ball.dy *= -1;
}
if (ball.y + ball.radius > canvas.height) {
ball.y = canvas.height - ball.radius;
}
if (ball.y - ball.radius < 0) {
ball.y = ball.radius;
}
if (ball.x + ball.radius > canvas.width) {
ball.x = canvas.width - ball.radius;
}
if (ball.x - ball.radius < 0) {
ball.x = ball.radius;
}
}
function ballCollision() {
for (var obj1 in objArray) {
for (var obj2 in objArray) {
if (
obj1 !== obj2 &&
distanceNextFrame(objArray[obj1], objArray[obj2]) <= 0
) {
var theta1 = objArray[obj1].angle();
var theta2 = objArray[obj2].angle();
var phi = Math.atan2(
objArray[obj2].y - objArray[obj1].y,
objArray[obj2].x - objArray[obj1].x
);
var m1 = objArray[obj1].mass;
var m2 = objArray[obj2].mass;
var v1 = objArray[obj1].speed();
var v2 = objArray[obj2].speed();
var dx1F =((v1 * Math.cos(theta1 - phi) * (m1 - m2) +2 * m2 * v2 * Math.cos(theta2 - phi)) / (m1 + m2)) *Math.cos(phi) + v1 * Math.sin(theta1 - phi) * -Math.sin(phi);
var dy1F = ((v1 * Math.cos(theta1 - phi) * (m1 - m2) + 2 * m2 * v2 * Math.cos(theta2 - phi)) /(m1 + m2)) *Math.sin(phi) +v1 * Math.sin(theta1 - phi) * Math.cos(phi);
var dx2F = ((v2 * Math.cos(theta2 - phi) * (m2 - m1) + 2 * m1 * v1 * Math.cos(theta1 - phi)) /(m1 + m2)) *Math.cos(phi) +v2 * Math.sin(theta2 - phi) * -Math.sin(phi);
var dy2F =((v2 * Math.cos(theta2 - phi) * (m2 - m1) +2 * m1 * v1 * Math.cos(theta1 - phi)) /(m1 + m2)) * Math.sin(phi) +v2 * Math.sin(theta2 - phi) * Math.cos(phi);
objArray[obj1].dx = dx1F;
objArray[obj1].dy = dy1F;
objArray[obj2].dx = dx2F;
objArray[obj2].dy = dy2F;
}
}
wallCollision(objArray[obj1]);
}
}
function staticCollision() {
for (var obj1 in objArray) {
for (var obj2 in objArray) {
if (
obj1 !== obj2 &&
distance(objArray[obj1], objArray[obj2]) <
objArray[obj1].radius + objArray[obj2].radius
) {
var theta = Math.atan2(
objArray[obj1].y - objArray[obj2].y,
objArray[obj1].x - objArray[obj2].x
);
var overlap =
objArray[obj1].radius +
objArray[obj2].radius -
distance(objArray[obj1], objArray[obj2]);
var smallerObject =
objArray[obj1].radius < objArray[obj2].radius ? obj1 : obj2;
objArray[smallerObject].x -= overlap * Math.cos(theta);
objArray[smallerObject].y -= overlap * Math.sin(theta);
}
}
}
}
function moveObjects() {
for (var obj in objArray) {
objArray[obj].x += objArray[obj].dx;
objArray[obj].y += objArray[obj].dy;
}
}
function drawObjects() {
for (var obj in objArray) {
objArray[obj].draw();
}
}
function draw() {
clearCanvas();
// canvasBackground();
moveObjects();
drawObjects();
staticCollision();
ballCollision();
requestAnimationFrame(draw);
console.log("Started");
}
// spawn the initial small thingies.
for (i = 0; i < 5; i++) {
objArray[objArray.length] = new Ball(randomX(), randomY(), randomRadius());
}
function randomColor() {
red = Math.floor(Math.random() * 3) * 127;
green = Math.floor(Math.random() * 3) * 127;
blue = Math.floor(Math.random() * 3) * 127;
rc = "rgb(" + red + ", " + green + ", " + blue + ")";
return rc;
}
function randomX() {
x = Math.floor(Math.random() * canvas.width);
if (x < 30) {
x = 30;
} else if (x + 30 > canvas.width) {
x = canvas.width - 30;
}
return x;
}
function randomY() {
y = Math.floor(Math.random() * canvas.height);
if (y < 30) {
y = 30;
} else if (y + 30 > canvas.height) {
y = canvas.height - 30;
}
return y;
}
function randomRadius() {
r = Math.ceil(Math.random() * 30 + 10);
//r = 2;
return r;
}
function distanceNextFrame(a, b) {
return (
Math.sqrt(
(a.x + a.dx - b.x - b.dx) ** 2 + (a.y + a.dy - b.y - b.dy) ** 2
) -
a.radius -
b.radius
);
}
function distance(a, b) {
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
}
draw();
});