/
collide.js
210 lines (182 loc) · 5.65 KB
/
collide.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
function Circle(xPos, yPos, radius, xVelocity, yVelocity) {
this.x = xPos;
this.y = yPos;
this.r = radius;
this.m = 2 * Math.PI * radius;
this.xVel = xVelocity;
this.yVel = yVelocity;
}
Circle.prototype.move = function(second) {
this.x += this.xVel * second;
this.y += this.yVel * second;
};
Circle.prototype.draw = function() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.strokeStyle = "black";
context.beginPath();
context.arc(this.x, this.y, this.r, 0, Math.PI * 2, true);
context.stroke();
context.closePath();
};
var numCircle = 5;
var width = 1000;
var height = 600;
var span = 50;
var speedLimit = 20;
function random(low, high) {
return low + Math.floor(Math.random() * (high - low));
}
function genRandomCircles(nCircle) {
var arrayCircle = [];
for (var i = 0; i < nCircle; ++i) {
var x = random(span, width - span);
var y = random(span, height - span);
var minDisToEdge = Math.min(x, y, width - x, height - y);
var r = random(minDisToEdge / 2, minDisToEdge);
var xVel = random(-speedLimit, speedLimit);
var yVel = random(-speedLimit, speedLimit);
arrayCircle.push(new Circle(x, y, r, xVel, yVel));
}
return arrayCircle;
}
function genCirclesTest() {
var arrayCircle = [];
arrayCircle[0] = new Circle(150, 150, 100, 10, 0);
arrayCircle[1] = new Circle(800, 150, 100, -10, 0);
return arrayCircle;
}
function genCircles() {
var arrayCircle = [];
var xVel = random(-speedLimit, speedLimit);
var yVel = random(-speedLimit, speedLimit);
arrayCircle[0] = new Circle(150, 150, 100, xVel, yVel);
xVel = random(-speedLimit, speedLimit);
yVel = random(-speedLimit, speedLimit);
arrayCircle[1] = new Circle(400, 150, 120, xVel, yVel);
xVel = random(-speedLimit, speedLimit);
yVel = random(-speedLimit, speedLimit);
arrayCircle[2] = new Circle(800, 150, 100, xVel, yVel);
xVel = random(-speedLimit, speedLimit);
yVel = random(-speedLimit, speedLimit);
arrayCircle[3] = new Circle(100, 400, 100, xVel, yVel);
/*xVel = random(-speedLimit, speedLimit);
yVel = random(-speedLimit, speedLimit);
arrayCircle[4] = new Circle(400, 400, 50, xVel, yVel);
xVel = random(-speedLimit, speedLimit);
yVel = random(-speedLimit, speedLimit);
arrayCircle[5] = new Circle(800, 400, 150, xVel, yVel);*/
return arrayCircle;
}
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.prototype.len = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
Vector.prototype.unit = function() {
var len = this.len();
this.x /= len;
this.y /= len;
}
Vector.prototype.rotate = function(angle) {
var x = this.x;
var y = this.y;
this.x = x * Math.cos(angle) + y * Math.sin(angle);
this.y = -x * Math.sin(angle) + y * Math.cos(angle);
}
Vector.prototype.scale = function(factor) {
var v = new Vector(this.x, this.y);
t = factor / v.len();
this.x *= t;
this.y *= t;
}
function dotProduct(vecp, vecq) {
return vecp.x * vecq.x + vecp.y * vecq.y;
}
function calcSpeed(mp, vp, mq, vq) {
new_vp = (2 * mq * vq + vp * (mp - mq)) / (mp + mq);
new_vq = (2 * mp * vp + vq * (mq - mp)) / (mp + mq);
return [new_vp, new_vq];
}
function collide(p, q) {
if ((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y) >
(p.r + q.r) * (p.r + q.r))
return;
vecp = new Vector(p.xVel, p.yVel);
vecq = new Vector(q.xVel, q.yVel);
vecs = new Vector(p.x - q.x, p.y - q.y); //圆心连线方向
vecs.unit();
vect = new Vector(vecs.x, vecs.y); //切线方向
vect.rotate(Math.PI / 2);
var vps = dotProduct(vecp, vecs);
var vpt = dotProduct(vecp, vect);
var vqs = dotProduct(vecq, vecs);
var vqt = dotProduct(vecq, vect);
var mp = p.m;
var mq = q.m;
var vlist = calcSpeed(mp, vps, mq, vqs);
vps = vlist[0];
vqs = vlist[1];
ps = new Vector(vecs.x, vecs.y);
ps.scale(vps);
pt = new Vector(vect.x, vect.y);
pt.scale(vpt);
p.xVel = ps.x + pt.x;
p.yVel = ps.y + pt.y;
qs = new Vector(vecs.x, vecs.y);
qs.scale(vqs);
qt = new Vector(vect.x, vect.y);
qt.scale(vqt);
q.xVel = qs.x + qt.x;
q.yVel = qs.y + qt.y;
}
function collideDetect(circles) {
for (var i in circles) {
var c = circles[i];
/*if (c.x - c.r <= 0 || c.x + c.r >= width) {
c.xVel = -c.xVel;
}
if (c.y - c.r <= 0 || c.y + c.r >= height) {
c.yVel = -c.yVel;
}*/
if (c.x - c.r <= 0) c.xVel = Math.abs(c.xVel);
if (c.x + c.r >= width) c.xVel = -Math.abs(c.xVel)
if (c.y - c.r <= 0) c.yVel = Math.abs(c.yVel);
if (c.y + c.r >= height) c.yVel = -Math.abs(c.yVel);
}
for (var i = 0; i < circles.length; ++i)
for (var j = i + 1; j < circles.length; ++j) {
collide(circles[i], circles[j]);
}
//TODO
}
function moveCircles(circles) {
for (var i in circles) {
circles[i].move(1);
}
}
function drawCircles(circles) {
for (var i in circles) {
circles[i].draw();
}
}
function clearCanvas() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
}
//var arrayCircle = genRandomCircles(numCircle);
var arrayCircle = genCircles();
//var arrayCircle = genCirclesTest();
function run() {
clearCanvas();
moveCircles(arrayCircle);
collideDetect(arrayCircle);
drawCircles(arrayCircle);
}
window.onload = function () {
var intervalTime = 50;
setInterval(run, intervalTime);
}