-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathengine.js
More file actions
156 lines (105 loc) · 2.8 KB
/
engine.js
File metadata and controls
156 lines (105 loc) · 2.8 KB
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
/* >>> F A X M A C H I N E <<< */
const WIDTH = 480, HEIGHT = 480;
var canvas = document.getElementById('voxelscreen');
var ofscr = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var ofc = ofscr.getContext('2d');
ctx.imageSmoothingEnabled = false;
//ctx.scale(2, 2);
ofscr.width = WIDTH;
ofscr.height = HEIGHT;
/* function for drawing 4-sided polygons */
function drawpoly(p1, p2, p4, p3, color) {
ofc.fillStyle = color;
ofc.beginPath();
ofc.moveTo(p1.x, p1.y);
ofc.lineTo(p3.x, p3.y);
ofc.lineTo(p4.x, p4.y);
ofc.lineTo(p2.x, p2.y);
ofc.closePath();
ofc.fill();
ofc.beginPath();
ofc.moveTo(p1.x, p1.y);
ofc.lineTo(p3.x, p3.y);
ofc.moveTo(p3.x, p3.y);
ofc.lineTo(p4.x, p4.y);
ofc.moveTo(p4.x, p4.y);
ofc.lineTo(p2.x, p2.y);
ofc.moveTo(p2.x, p2.y);
ofc.lineTo(p1.x, p1.y);
ofc.closePath();
ofc.stroke();
}
class Point2D {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
class Point3D {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
class Plain {
constructor(p1, p2, p3, p4, color, pos1, pos2) {
this.p = [p1, p2, p3, p4];
this.color = color;
this.pos1 = pos1;
this.pos2 = pos2;
}
}
function rotate(x, y, angle) {
return [x * angle.cos - y * angle.sin, x * angle.sin + y * angle.cos];
}
function rotate3d(x, y, z, lr, ud) {
[x, z] = rotate(x, z, lr);
[y, z] = rotate(y, z, ud);
return [x, y, z];
}
/*
new Camera( <Number = field of view> ) returns Camera
Camera.rotate( <Number = left-right rotation>, <Number = up-down rotation> ) returns void
Camera.project( <Point3D> ) returns Point2D
Camera.setFov( <Number = field of view> )
*/
class Camera {
constructor(fov, dist) {
this.csz = 0.6 / 8;
this.top = 0;
this.rotLR = {val: 0, sin: 0, cos: 1};
this.rotUD = {val: 0, sin: 0, cos: 1};
this.dist = dist;
this.fov_ctg = 1 / Math.tan(fov * Math.PI / 360);
}
rotate(xz, yz) {
this.rotLR.val -= xz;
this.rotUD.val -= yz;
this.rotLR.sin = Math.sin(this.rotLR.val);
this.rotLR.cos = Math.cos(this.rotLR.val);
this.rotUD.sin = Math.sin(this.rotUD.val);
this.rotUD.cos = Math.cos(this.rotUD.val);
}
setFov(fov) {
this.fov_ctg = 1 / Math.tan(fov * Math.PI / 360);
}
project(p3d) {
var x = (p3d.x - 8) * this.csz;
var y = (this.top - p3d.y) * this.csz;
var z = (p3d.z - 8) * this.csz;
[x, y, z] = rotate3d(x, y, z, this.rotLR, this.rotUD);
z += this.dist;
var d = this.fov_ctg / z;
return new Point2D(WIDTH * (0.5 + x * d), HEIGHT * (0.5 + y * d));
}
getDist(p3d) {
var x = (p3d.x - 8) * this.csz;
var y = (this.top - p3d.y) * this.csz;
var z = (p3d.z - 8) * this.csz;
[x, y, z] = rotate3d(x, y, z, this.rotLR, this.rotUD);
z += this.dist;
return x * x + y * y + z * z;
}
}