-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.js
350 lines (303 loc) · 11.8 KB
/
physics.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var SAT = require("./SAT.js");
function checkCollisions(translation, points, map){
var possibleTrans = translation;
var minX = Math.floor(points[0].x/100),
maxX = Math.floor(points[0].x/100),
minY = Math.floor(points[0].y/100),
maxY = Math.floor(points[0].y/100);
for(i in points){
if(points[i].x+translation.x < 0){
possibleTrans.x = 0-points[i].x;
possibleTrans.point = points[i];
}
if(points[i].y+translation.y < 0){
possibleTrans.y = 0-points[i].y;
possibleTrans.point = points[i];
}
if(points[i].x+translation.x > map.width*100){
possibleTrans.x = map.width*100-points[i].x;
possibleTrans.point = points[i];
}
if(points[i].y+translation.y > map.height*100){
possibleTrans.y = map.height*100-points[i].y;
possibleTrans.point = points[i];
}
}
for(i in points){
if(Math.floor(points[i].x /100) < minX) minX = Math.floor(points[i].x/100);
if(Math.floor((points[i].x +possibleTrans.x)/100) < minX) minX = Math.floor(points[i].x+possibleTrans.x/100);
if(Math.floor(points[i].x /100) > maxX) maxX = Math.floor(points[i].x/100);
if(Math.floor((points[i].x +possibleTrans.x)/100) > maxX) maxX = Math.floor(points[i].x+possibleTrans.x/100);
if(Math.floor(points[i].y /100) < minY) minY = Math.floor(points[i].y/100);
if(Math.floor((points[i].y+possibleTrans.y)/100) < minY) minY = Math.floor(points[i].y+possibleTrans.y/100);
if(Math.floor(points[i].y /100) > maxY) maxY = Math.floor(points[i].y/100);
if(Math.floor((points[i].y+possibleTrans.y)/100) > maxY) maxY = Math.floor(points[i].y+possibleTrans.y/100);
}
if(minY < 0) minY = 0;
if(minX < 0) minX = 0;
if(maxY < 0) maxY = 0;
if(maxX < 0) maxX = 0;
if(maxY >= map.wallsH.length) maxY = map.wallsH.length-1;
if(maxX >= map.wallsH[0].length) maxX = map.wallsH[0].length-1;
if(minY >= map.wallsH.length) minY = map.wallsH.length-1;
if(minX >= map.wallsH[0].length) minX = map.wallsH[0].length-1;
if(map.wallsH.length > maxY && map.wallsV.length > maxY){
var set = new Set();
set.add({'x':maxX, 'y':maxY});
set.add({'x':maxX, 'y':minY});
set.add({'x':minX, 'y':minY});
set.add({'x':minX, 'y':maxY});
var toCheck = [];
for(let it of set){
if(map.wallsH[it.y][it.x])
toCheck.push(new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(it.x*100, it.y*100),
new SAT.Vector(it.x*100+100, it.y*100),
new SAT.Vector(it.x*100+100, it.y*100+10),
new SAT.Vector(it.x*100+0, it.y*100+10)
]));
if(map.wallsV[it.y][it.x])
toCheck.push(new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(it.x*100, it.y*100),
new SAT.Vector(it.x*100+10, it.y*100),
new SAT.Vector(it.x*100+10, it.y*100+100),
new SAT.Vector(it.x*100, it.y*100+100)
]));
}
var tankPol = new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(points[0].x+possibleTrans.x, points[0].y+possibleTrans.y),
new SAT.Vector(points[1].x+possibleTrans.x, points[1].y+possibleTrans.y),
new SAT.Vector(points[2].x+possibleTrans.x, points[2].y+possibleTrans.y),
new SAT.Vector(points[3].x+possibleTrans.x, points[3].y+possibleTrans.y)
]);
for(let pol of toCheck){
var resp = new SAT.Response();
if(SAT.testPolygonPolygon(tankPol, pol, resp)){
possibleTrans.x -= resp.overlapV.x;
possibleTrans.y -= resp.overlapV.y;
tankPol = new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(points[0].x+possibleTrans.x, points[0].y+possibleTrans.y),
new SAT.Vector(points[1].x+possibleTrans.x, points[1].y+possibleTrans.y),
new SAT.Vector(points[2].x+possibleTrans.x, points[2].y+possibleTrans.y),
new SAT.Vector(points[3].x+possibleTrans.x, points[3].y+possibleTrans.y)
]);
}
}
}
return possibleTrans;
}
function getTankPoints(pos){
var res = [];
var rot = pos.rot * Math.PI / 180; //in radians
var tempX = -22.5;
var tempY = -15;
var rotatedX = tempX*Math.cos(rot) - tempY*Math.sin(rot);
var rotatedY = tempX*Math.sin(rot) + tempY*Math.cos(rot);
res.push({'x': rotatedX + pos.x,
'y': rotatedY + pos.y});
tempX = 22.5;
tempY = -15;
rotatedX = tempX*Math.cos(rot) - tempY*Math.sin(rot);
rotatedY = tempX*Math.sin(rot) + tempY*Math.cos(rot);
res.push({'x': rotatedX + pos.x,
'y': rotatedY + pos.y});
tempX = 22.5;
tempY = 15;
rotatedX = tempX*Math.cos(rot) - tempY*Math.sin(rot);
rotatedY = tempX*Math.sin(rot) + tempY*Math.cos(rot);
res.push({'x': rotatedX + pos.x,
'y': rotatedY + pos.y});
tempX = -22.5;
tempY = 15;
rotatedX = tempX*Math.cos(rot) - tempY*Math.sin(rot);
rotatedY = tempX*Math.sin(rot) + tempY*Math.cos(rot);
res.push({'x': rotatedX + pos.x,
'y': rotatedY + pos.y});
return res;
}
module.exports.tankMove = function(pos, move, map){
var transX = 0;
var transY = 0;
var transRot = 0;
if(move.left){
transRot -= 6;
}
if(move.right){
transRot += 6;
}
if(move.front){//TODO on front and back make rotation by transX-possibleTransX and Y
transX += 4*Math.cos(pos.rot * Math.PI / 180);
transY += 4*Math.sin(pos.rot * Math.PI / 180);
}
if(move.back){
transX += -2*Math.cos(pos.rot * Math.PI / 180);
transY += -2*Math.sin(pos.rot * Math.PI / 180);
}
if(transRot || transX || transY || move.rot){ //if there is move
//calc possible move
var postrans = checkCollisions({'x': transX, 'y': transY }, getTankPoints(pos), map);
if(postrans.point){//if there is point of collision, rotate tank
if(postrans.point.x > pos.x)
pos.rot -= (transY - postrans.y)*2;
else
pos.rot += (transY - postrans.y)*2;
if(postrans.point.y > pos.y)
pos.rot += (transX - postrans.x)*2;
else
pos.rot -= (transX - postrans.x)*2;
}
//add rotation from controls
pos.rot += transRot;
if(move.rot)
pos.rot = move.rot;
pos.rot %= 360;
//last count to not colliding and finally set postions
var postrans = checkCollisions({'x': postrans.x, 'y': postrans.y }, getTankPoints(pos), map);
pos.x += postrans.x;
pos.y += postrans.y;
if(postrans.x != transX || postrans.y != transY){//show particles if colliding
pos.particles = true;
}else
pos.particles = false;
return true;
}else{
pos.particles = false;
return false;
}
};
function checkBulCols(translation, bullet, points, map, players){
var possibleTrans = translation;
var minX = Math.floor(points[0].x/100),
maxX = Math.floor(points[0].x/100),
minY = Math.floor(points[0].y/100),
maxY = Math.floor(points[0].y/100);
for(i in points){
if(points[i].x+translation.x < 0){
possibleTrans.x = 0-points[i].x;
possibleTrans.point = points[i];
}
if(points[i].y+translation.y < 0){
possibleTrans.y = 0-points[i].y;
possibleTrans.point = points[i];
}
if(points[i].x+translation.x > map.width*100){
possibleTrans.x = map.width*100-points[i].x;
possibleTrans.point = points[i];
}
if(points[i].y+translation.y > map.height*100){
possibleTrans.y = map.height*100-points[i].y;
possibleTrans.point = points[i];
}
}
for(i in points){
if(Math.floor(points[i].x /100) < minX) minX = Math.floor(points[i].x/100);
if(Math.floor((points[i].x +possibleTrans.x)/100) < minX) minX = Math.floor(points[i].x+possibleTrans.x/100);
if(Math.floor(points[i].x /100) > maxX) maxX = Math.floor(points[i].x/100);
if(Math.floor((points[i].x +possibleTrans.x)/100) > maxX) maxX = Math.floor(points[i].x+possibleTrans.x/100);
if(Math.floor(points[i].y /100) < minY) minY = Math.floor(points[i].y/100);
if(Math.floor((points[i].y+possibleTrans.y)/100) < minY) minY = Math.floor(points[i].y+possibleTrans.y/100);
if(Math.floor(points[i].y /100) > maxY) maxY = Math.floor(points[i].y/100);
if(Math.floor((points[i].y+possibleTrans.y)/100) > maxY) maxY = Math.floor(points[i].y+possibleTrans.y/100);
}
if(minY < 0) minY = 0;
if(minX < 0) minX = 0;
if(maxY < 0) maxY = 0;
if(maxX < 0) maxX = 0;
if(maxY >= map.wallsH.length) maxY = map.wallsH.length-1;
if(maxX >= map.wallsH[0].length) maxX = map.wallsH[0].length-1;
if(minY >= map.wallsH.length) minY = map.wallsH.length-1;
if(minX >= map.wallsH[0].length) minX = map.wallsH[0].length-1;
if(map.wallsH.length > maxY && map.wallsV.length > maxY){
var set = new Set();
set.add({'x':maxX, 'y':maxY});
set.add({'x':maxX, 'y':minY});
set.add({'x':minX, 'y':minY});
set.add({'x':minX, 'y':maxY});
var toCheck = [];
for(let it of set){
if(map.wallsH[it.y][it.x])
toCheck.push(new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(it.x*100, it.y*100),
new SAT.Vector(it.x*100+100, it.y*100),
new SAT.Vector(it.x*100+100, it.y*100+10),
new SAT.Vector(it.x*100+0, it.y*100+10)
]));
if(map.wallsV[it.y][it.x])
toCheck.push(new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(it.x*100, it.y*100),
new SAT.Vector(it.x*100+10, it.y*100),
new SAT.Vector(it.x*100+10, it.y*100+100),
new SAT.Vector(it.x*100, it.y*100+100)
]));
}
var bull = new SAT.Circle(new SAT.Vector(bullet.x+possibleTrans.x,bullet.y+possibleTrans.y), 4);
for(let pol of toCheck){
var resp = new SAT.Response();
if(SAT.testCirclePolygon(bull, pol, resp)){
possibleTrans.x -= resp.overlapV.x;
possibleTrans.y -= resp.overlapV.y;
var bull = new SAT.Circle(new SAT.Vector(bullet.x+possibleTrans.x,bullet.y+possibleTrans.y), 4);
}
}
for(id in players){
if(players[id].dead) continue;
var resp = new SAT.Response();
var points = getTankPoints(players[id].pos);
var tankPol = new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(points[0].x+possibleTrans.x, points[0].y+possibleTrans.y),
new SAT.Vector(points[1].x+possibleTrans.x, points[1].y+possibleTrans.y),
new SAT.Vector(points[2].x+possibleTrans.x, points[2].y+possibleTrans.y),
new SAT.Vector(points[3].x+possibleTrans.x, points[3].y+possibleTrans.y)
]);
if(SAT.testCirclePolygon(bull, tankPol, resp)){
if(!(bullet.time > 997 && bullet.shooterId != players.sId)){
players[id].kill();
bullet.remove();
}
}
}
}
return possibleTrans;
}
function getBulPoints(pos){
var res = [];
res.push({'x': pos.x + 4,
'y': pos.y + 4});
res.push({'x': pos.x - 4,
'y': pos.y + 4});
res.push({'x': pos.x + 4,
'y': pos.y - 4});
res.push({'x': pos.x - 4,
'y': pos.y - 4});
return res;
}
module.exports.bulletMove = function(bul, players, map, time){
var ch = 8 * time/20;
var transX = ch*Math.cos(bul.rot * Math.PI / 180);
var transY = ch*Math.sin(bul.rot * Math.PI / 180);
var postrans = checkBulCols({'x': transX, 'y':transY}, bul, getBulPoints(bul), map, players);
while((postrans.x > transX && transX > 0) || (postrans.y > transY && transY > 0) || (postrans.x < transX && transX < 0) || (postrans.y < transY && transY < 0)){
ch /= 2;
var transX = ch*Math.cos(bul.rot * Math.PI / 180);
var transY = ch*Math.sin(bul.rot * Math.PI / 180);
postrans = checkBulCols({'x': transX, 'y':transY}, bul, getBulPoints(bul), map, players);
}
bul.x += postrans.x;
bul.y += postrans.y;
if(postrans.x != transX){
if(bul.rot < 180){
bul.rot = 90+(90-bul.rot);
}else{
bul.rot = 270+(270-bul.rot);
}
bul.y += postrans.y - transY;
}
if(postrans.y != transY){
if(bul.rot < 270 && bul.rot > 90){
bul.rot = 180+(180-bul.rot);
}else{
bul.rot = 0-bul.rot;
}
bul.y += postrans.y - transY;
}
};