-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplains.html
executable file
·539 lines (419 loc) · 11.8 KB
/
plains.html
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<html>
<head>
<title>Desert at 01:54 on September 19th, 1995</title>
<meta charset="utf-8">
<style>
body {
background-color: slateblue;
}
#cavalier {
margin: auto;
text-align: center;
display: block;
}
#centerModule {
text-align: center;
margin: auto;
}
</style>
<script>
window.onload = setup;
window.addEventListener("keydown", handleKeyPress, false);
window.addEventListener("keyup", handleKeyNegate, false);
//global variables
var canvas;
var ctx;
var centerX;
var centerY;
let page_animation;
let player;
var world_bg = "#002";
var world_starDistance = 5000;
let world_objects = [];
var render_clipDistance = 0.1;
//classes
class Player {
constructor(x, y, z, xRot, yRot) {
this.friction = 0.85;
this.height = 5;
this.scale = 100;
this.sens = 0.04;
this.speed = 0.05;
this.x = x;
this.y = y;
this.z = z;
this.dx = 0;
this.dy = 0;
this.dz = 0;
this.dMax = 1;
this.ax = 0;
this.ay = 0;
this.az = 0;
this.theta = yRot;
this.phi = xRot;
this.dt = 0;
this.dp = 0;
}
tick() {
//handling velocity
//adding
this.dx += this.ax;
//binding max
if (Math.abs(this.dx) > this.dMax) {
this.dx *= 0.95;
}
//friction
if (this.ax == 0) {
this.dx *= this.friction;
}
this.dz += this.az;
if (Math.abs(this.dz) > this.dMax) {
this.dz *= 0.95;
}
if (this.az == 0) {
this.dz *= this.friction;
}
//handling position
this.x += this.dz * Math.sin(this.theta);
this.z += this.dz * Math.cos(this.theta);
this.x += this.dx * Math.sin(this.theta + (Math.PI/2));
this.z += this.dx * Math.cos(this.theta + (Math.PI/2));
//camera velocity
this.theta += this.dt;
this.phi += this.dp;
//special case for vertical camera orientation
if (Math.abs(this.phi) >= Math.PI * 0.5) {
//if the camera angle is less than 0, set it to -1/2 pi. Otherwise, set it to 1/2 pi
this.phi = Math.PI * (-0.5 + (this.phi > 0));
}
}
}
class Platform {
constructor(x, y, z, l, w, color) {
this.x = x;
this.y = y;
this.z = z;
this.len = l;
this.wdt = w;
this.collisionHeight = 20;
this.color = color;
this.points = [ [this.x + this.len, this.y, this.z - this.wdt],
[this.x + this.len, this.y, this.z + this.wdt],
[this.x - this.len, this.y, this.z + this.wdt],
[this.x - this.len, this.y, this.z - this.wdt]];
this.drawCoords = [];
}
tick() {
//collide correctly with player
}
beDrawn() {
//first get camera coordinate points
var tempPoints = [this.spaceToCamera(this.points[0]), this.spaceToCamera(this.points[1]), this.spaceToCamera(this.points[2]), this.spaceToCamera(this.points[3])];
//loop through all points
for (var y=0;y<tempPoints.length;y++) {
//if the selected point will be clipped, run the algorithm
if (tempPoints[y][2] < render_clipDistance) {
//freefriends is the number of adjacent non-clipped points
var freeFriends = (tempPoints[(y+(tempPoints.length-1))%tempPoints.length][2] >= render_clipDistance) + (tempPoints[(y+1)%tempPoints.length][2] >= render_clipDistance);
if (freeFriends == 0) {
//if there are no free friends, there's no point in attempting, so just move on
tempPoints.splice(y, 1);
y -= 1;
} else {
//move towards friends
var friendCoords = tempPoints[(y+(tempPoints.length-1))%tempPoints.length];
var moveAmount = getPercentage(friendCoords[2], tempPoints[y][2], render_clipDistance)
var newPointCoords = [linterp(friendCoords[0], tempPoints[y][0], moveAmount), linterp(friendCoords[1], tempPoints[y][1], moveAmount), render_clipDistance + 0.05];
tempPoints.splice(y, 0, newPointCoords);
y += 1;
friendCoords = tempPoints[(y+1)%tempPoints.length];
moveAmount = getPercentage(friendCoords[2], tempPoints[y][2], render_clipDistance)
newPointCoords = [linterp(friendCoords[0], tempPoints[y][0], moveAmount), linterp(friendCoords[1], tempPoints[y][1], moveAmount), render_clipDistance + 0.05];
tempPoints.splice(y, 1);
tempPoints.splice(y, 0, newPointCoords);
}
}
}
//turn points into screen coordinates
var screenPoints = [];
for (var a=0;a<tempPoints.length;a++) {
screenPoints.push(this.cameraToScreen(tempPoints[a]));
}
if (screenPoints.length == 0) {
screenPoints = [[0, 0], [0, 0]];
}
//finally draw self
drawPoly(this.color, screenPoints);
}
//these two functions do the same thing as spaceToScreen, but split so the clipping plane can be implemented
//turns world coordinates into 3d camera coordinates, for clipping
spaceToCamera(point) {
var [tX, tY, tZ] = point;
tX -= player.x;
tY -= player.y;
tZ -= player.z;
[tX, tZ] = rotate(tX, tZ, player.theta);
[tY, tZ] = rotate(tY, tZ, player.phi);
return [tX, tY, tZ];
}
//turns camera coordinates into 2d screen coordinates
cameraToScreen(point) {
//divide by axis perpendicular to player
var [tX, tY, tZ] = point;
tX /= tZ;
tY /= tZ;
//accounting for camera scale
tX *= player.scale;
//flipping image
tY *= -1 * player.scale;
//accounting for screen coordinates
tX += canvas.width / 2;
tY += canvas.height / 2;
return [tX, tY];
}
}
class Star {
constructor(x, y, z) {
this.color = "#AAF";
this.x = x;
this.y = y;
this.z = z;
}
tick() {
}
beDrawn() {
if (!isClipped([this.x, this.y, this.z])) {
var drawCoords = spaceToScreen([this.x, this.y, this.z]);
drawCircle(this.color, drawCoords[0], drawCoords[1], 3);
}
}
}
//setup function
function setup() {
canvas = document.getElementById("cornh");
ctx = canvas.getContext("2d");
centerX = canvas.width * 0.5;
centerY = canvas.height * 0.5;
player = new Player(0, 5, 0, 0, 0);
//setting up world
world_objects = [new Platform(0, 0, 0, 1000, 1000, "#FFF"), new Platform(0, 1, 0, 10, 10, "#F0F")];
populateStarSphere();
page_animation = window.requestAnimationFrame(main);
}
function main() {
//drawing background
ctx.fillStyle = world_bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
//handling entities
player.tick();
for (var a=0;a<world_objects.length;a++) {
world_objects[a].tick();
world_objects[a].beDrawn();
}
//call self
//player.y += 0.1;
page_animation = window.requestAnimationFrame(main);
}
//input handling
function handleKeyPress(a) {
switch(a.keyCode) {
//player controls
// a/d
case 65:
player.ax = -1 * player.speed;
break;
case 68:
player.ax = player.speed;
break;
// w/s
case 87:
player.az = player.speed;
break;
case 83:
player.az = -1 * player.speed;
break;
//camera controls
case 37:
player.dt = -1 * player.sens;
break;
case 38:
player.dp = player.sens;
break;
case 39:
player.dt = player.sens;
break;
case 40:
player.dp = -1 * player.sens;
break;
}
}
function handleKeyNegate(a) {
switch(a.keyCode) {
//player controls
// a/d
case 65:
if (player.ax < 0) {
player.ax = 0;
}
break;
case 68:
if (player.ax > 0) {
player.ax = 0;
}
break;
// w/s
case 87:
if (player.az > 0) {
player.az = 0;
}
break;
case 83:
if (player.az < 0) {
player.az = 0;
}
break;
//camera controls
case 37:
if (player.dt < 0) {
player.dt = 0;
}
break;
case 38:
if (player.dp > 0) {
player.dp = 0;
}
break;
case 39:
if (player.dt > 0) {
player.dt = 0;
}
break;
case 40:
if (player.dp < 0) {
player.dp = 0;
}
break;
}
}
//drawing functions
function drawQuad(color, p1, p2, p3, p4) {
//console.log(color, p1, p2, p3, p4);
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
ctx.lineTo(p3[0], p3[1]);
ctx.lineTo(p4[0], p4[1]);
ctx.lineTo(p1[0], p1[1]);
ctx.stroke();
ctx.fill();
}
function drawPoly(color, xyPointsArr) {
ctx.fillStyle = color;
ctx.strokeStyle = color;
var xypa = xyPointsArr;
ctx.beginPath();
ctx.moveTo(xypa[0][0], xypa[0][1]);
for (var i=1;i<xypa.length;i++) {
ctx.lineTo(xypa[i][0], xypa[i][1]);
}
//back to start
ctx.lineTo(xypa[0][0], xypa[0][1]);
ctx.stroke();
ctx.fill();
}
function drawCircle(color, x, y, radius) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.ellipse(x, y, radius, radius, 0, 0, Math.PI * 2);
ctx.stroke();
ctx.fill();
}
//misc game functions
function populateStarSphere() {
//random stars
for (var e=0;e<100;e++) {
var pos = polToCart(randomCustom(0, Math.PI * 2), randomCustom(0.1, (Math.PI * 0.48)), randomCustom(world_starDistance, world_starDistance * 2));
world_objects.push(new Star(pos[0], pos[1], pos[2]));
}
}
//utility functions
//returns the percentage from val1 to val2 that the checkVal is in
//example: 0, 10, 5, returns 0.5)
function getPercentage(val1, val2, checkVal) {
val2 -= val1;
checkVal -= val1;
return checkVal / val2;
}
//determines if a point will be clipped due to being behind / too close to the player
function isClipped(pointArr) {
var tX = pointArr[0];
var tY = pointArr[1];
var tZ = pointArr[2];
tX -= player.x;
tY -= player.y;
tZ -= player.z;
[tX, tZ] = rotate(tX, tZ, player.theta);
[tY, tZ] = rotate(tY, tZ, player.phi);
return (tZ < 0.1);
}
//performs a linear interpolation between 2 values
function linterp(a, b, percentage) {
return a + ((b - a) * percentage);
}
function polToCart(theta, phi, radius) {
//theta here is horizontal angle, while phi is vertical inclination
var x = radius * Math.cos(theta) * Math.sin(phi);
var y = radius * Math.cos(phi);
var z = radius * Math.sin(theta) * Math.sin(phi);
return [x, y, z];
}
function spaceToScreen(pointArr) {
//takes in an xyz list and outputs an xy list
var tX = pointArr[0];
var tY = pointArr[1];
var tZ = pointArr[2];
//step 1: make coordinates relative to player
tX -= player.x;
tY -= player.y;
tZ -= player.z;
//step 2: rotate coordinates
//rotating around y axis
[tX, tZ] = rotate(tX, tZ, player.theta);
//rotating around x axis
[tY, tZ] = rotate(tY, tZ, player.phi);
//step 2.5: clipping if behind the player
if (tZ < 0.1) {
tX = (tX * -1) / tZ;
tY = (tY * -1) / tZ;
} else {
//step 3: divide by axis perpendicular to player
tX /= tZ;
tY /= tZ;
}
//step 4: account for camera scale
tX *= player.scale;
//flipping image
tY *= -1 * player.scale;
//accounting for screen coordinates
tX += canvas.width / 2;
tY += canvas.height / 2;
return [tX, tY];
}
function randomCustom(min, max) {
return (Math.random() * (max - min)) + min;
}
function rotate(x, z, radians) {
[x, z] = [(x * Math.cos(radians)) - (z * Math.sin(radians)), (z * Math.cos(radians)) + (x * Math.sin(radians))];
return [x, z];
}
</script>
</head>
<body>
<div id="centerModule">
<canvas id="cornh" width="640" height="480"></canvas><br>
</div>
</body>
</html>