-
Notifications
You must be signed in to change notification settings - Fork 3
/
raycast.js
executable file
·379 lines (294 loc) · 10.9 KB
/
raycast.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
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
const TILE_SIZE = 32;
const MAP_NUM_ROWS = 11;
const MAP_NUM_COLS = 15;
const WINDOW_WIDTH = MAP_NUM_COLS * TILE_SIZE
const WINDOW_HEIGHT = MAP_NUM_ROWS * TILE_SIZE
console.log(WINDOW_WIDTH);
console.log(WINDOW_HEIGHT);
var FOV = 60 * (Math.PI / 180);
const NUM_RAYS = WINDOW_WIDTH/4;
var mouse;
class Map {
constructor() {
this.grid = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
}
// a very userful function for checking if there is a wall at a point
hasWallAt(x, y) {
return this.grid[Math.floor(y / TILE_SIZE)][Math.floor(x / TILE_SIZE)];
}
render() {
for (var i = 0; i < MAP_NUM_ROWS; i++) {
for (var j = 0; j < MAP_NUM_COLS; j++) {
let tileX = j * TILE_SIZE;
let tileY = i * TILE_SIZE;
let tileColor = this.grid[i][j] == 1 ? "#222" : "#fff";
fill(tileColor);
stroke("#222")
rect(tileX, tileY, TILE_SIZE, TILE_SIZE);
}
}
}
}
class Player {
constructor() {
this.x = WINDOW_WIDTH / 2;
this.y = WINDOW_HEIGHT / 2;
this.radius = 3;
this.turnDirection = 0;
this.walkDirection = 0;
this.rotationAngle = Math.PI / 2;
this.moveSpeed = 2.5;
this.rotationSpeed = 2 * (Math.PI / 180);
}
update() {
this.rotationAngle += this.turnDirection * this.rotationSpeed;
// if (!grid.hasWallAt(this.x, this.y)) {
// only do that if the player is not colliding
let moveStep = this.walkDirection * this.moveSpeed;
this.x += Math.cos(this.rotationAngle) * moveStep;
this.y += Math.sin(this.rotationAngle) * moveStep;
// }
// reseting angle
if (this.rotationAngle < 0)
this.rotationAngle += 2 * Math.PI;
if (this.rotationAngle > 2 * Math.PI)
this.rotationAngle -= 2 * Math.PI;
}
render() {
noStroke();
fill("red");
circle(this.x, this.y, this.radius);
stroke("red");
// a line for showing the direction of the player
line(
this.x,
this.y,
this.x + Math.cos(this.rotationAngle) * 30,
this.y + Math.sin(this.rotationAngle) * 30
);
}
}
class Ray {
constructor(rayAngle) {
this.rayAngle = normalizeAngle(rayAngle); // the angle will be normalized
this.wallHitX = 0;
this.wallHitY = 0;
this.distance = 0;
this.color = 255;
// booleans to check if the player is looking at the directions
this.isRayFacingDown = this.rayAngle > 0 && this.rayAngle < Math.PI; // the y is inverted, so the up is down wow
this.isRayFacingUp = !this.isRayFacingDown;
this.isRayFacingRight = this.rayAngle < 0.5 * Math.PI || this.rayAngle > 1.5 * Math.PI;
this.isRayFacingLeft = !this.isRayFacingRight;
}
cast(columnId) {
var xintersect, yintersect; // variables for storing the first intersection (that one that has the player position)
var xstep, ystep; // variables for storing the xstep and the ystep after finding the xintersect and yintersect
////////////////////////////////////////////////
// HORIZONTAL INTERSECTION CHECKING
///////////////////////////////////////////////
var foundHorizontalWall = false; // we have to check if we found a wall (horizontal)
var horizontalWallHitX = 0; // var to store the X position of the wall in horizontal which was hit
var horizontalWallHitY = 0; // var to store the Y position of the wall in horizontal which was hit
yintersect = Math.floor(player.y / TILE_SIZE) * TILE_SIZE; // the position of the first intersection
// TODO: explain the math behind this shit
if (this.rayAngle > 0 && this.rayAngle < Math.PI) // looking down
yintersect += TILE_SIZE;
xintersect = player.x + (yintersect - player.y) / Math.tan(this.rayAngle); // the x position of the first intersection
ystep = TILE_SIZE; // the y step for the horizontal checking will be the same as the tile size (only if the player is looking down)
if (!(this.rayAngle > 0 && this.rayAngle < Math.PI)) // looking up
ystep *= -1;
xstep = ystep/Math.tan(this.rayAngle); // TODO: explain the math behind this thing
// the next intersection starts at the first intersection (with the player)
var nextHorizontalX = xintersect;
var nextHorizontalY = yintersect;
// TODO: explain the math behind this
if (!(this.rayAngle > 0 && this.rayAngle < Math.PI)) // looking up (see that is the same as the above)
nextHorizontalY -= 0.01;
// checking the horizontal lines //
// while the it is inside the window
while (nextHorizontalX <= WINDOW_WIDTH && nextHorizontalX >= 0 && nextHorizontalY <= WINDOW_HEIGHT && nextHorizontalY >= 0) {
// if there is a wall at the position found in nextHorizontalX and Y
if (grid.hasWallAt(nextHorizontalX, nextHorizontalY)) {
foundHorizontalWall = true;
// we need to store the position of the wall found
horizontalWallHitX = nextHorizontalX;
horizontalWallHitY = nextHorizontalY;
break;
} else {
// if we didn't found a wall, we need to keep checking
nextHorizontalX += xstep;
nextHorizontalY += ystep;
}
}
/////////////////////////////////////
// VERTICAL INTERSECTION CHECKING
////////////////////////////////////
var foundVerticalWall = false;
var verticalWallHitX = 0;
var verticalWallHitY = 0;
xintersect = Math.floor(player.x / TILE_SIZE) * TILE_SIZE;
if (this.rayAngle < 0.5 * Math.PI || this.rayAngle > 1.5 * Math.PI) // facing right
xintersect += TILE_SIZE;
yintersect = player.y + (xintersect - player.x) * Math.tan(this.rayAngle); // TODO: see why is player.x + ...
xstep = TILE_SIZE;
if (!(this.rayAngle < 0.5 * Math.PI || this.rayAngle > 1.5 * Math.PI)) // facing left
xstep *= -1;
ystep = xstep * Math.tan(this.rayAngle);
var nextVerticalX = xintersect;
var nextVerticalY = yintersect;
if (this.isRayFacingLeft)
nextVerticalX -= 0.01;
while (nextVerticalX >= 0 && nextVerticalX <= WINDOW_WIDTH && nextVerticalY >= 0 && nextVerticalY <= WINDOW_HEIGHT) {
if (grid.hasWallAt(nextVerticalX, nextVerticalY)) {
foundVerticalWall = true;
verticalWallHitX = nextVerticalX;
verticalWallHitY = nextVerticalY;
break;
} else {
nextVerticalX += xstep;
nextVerticalY += ystep;
}
}
/////////////////////////////////////////////////////////////////////////
// Distance calculation
/////////////////////////////////////////////////////////////////////////
// we need to compare the horizontal distance with the vertical distance.
// then check which one is the nearest to the player
/////////////////////////////////////////////////////////////////////////
var horizontalDistance;
var verticalDistance;
if (foundHorizontalWall) {
horizontalDistance = distanceBetween(player.x, player.y, horizontalWallHitX, horizontalWallHitY);
} else {
horizontalDistance = Number.MAX_VALUE;
}
if (foundVerticalWall) {
verticalDistance = distanceBetween(player.x, player.y, verticalWallHitX, verticalWallHitY);
} else {
verticalDistance = Number.MAX_VALUE;
}
this.wallHitX = (horizontalDistance < verticalDistance) ? horizontalWallHitX : verticalWallHitX;
this.wallHitY = (horizontalDistance < verticalDistance) ? horizontalWallHitY : verticalWallHitY;
this.distance = (horizontalDistance < verticalDistance) ? horizontalDistance : verticalDistance;
this.distance *= Math.cos(player.rotationAngle - this.rayAngle);
if (verticalDistance < horizontalDistance) {
this.color = 160;
}
if (horizontalDistance < verticalDistance) {
this.color = 255;
}
}
render() {
stroke("red");
line(
player.x,
player.y,
this.wallHitX,
this.wallHitY
);
}
}
var grid = new Map();
var player = new Player();
var rays = [];
function keyPressed() {
if (keyCode == UP_ARROW) {
player.walkDirection = 1;
} else if (keyCode == DOWN_ARROW) {
player.walkDirection = -1;
} else if (keyCode == RIGHT_ARROW) {
player.turnDirection = 1
} else if (keyCode == LEFT_ARROW) {
player.turnDirection = -1
}
}
function keyReleased() {
if (keyCode == UP_ARROW) {
player.walkDirection = 0;
} else if (keyCode == DOWN_ARROW) {
player.walkDirection = 0;
} else if (keyCode == RIGHT_ARROW) {
player.turnDirection = 0
} else if (keyCode == LEFT_ARROW) {
player.turnDirection = 0
}
}
function normalizeAngle(angle) {
angle = angle % (2 * Math.PI);
if (angle < 0) {
angle = (2 * Math.PI) + angle;
}
return angle;
}
function distanceBetween(x1, y1, x2, y2) {
// TODO: TROCAR ISSO AQUI PELO O QUE TA NO PAPEL PRA VER NO QUE DÁ
return Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
}
function castAllRays() {
var columnId = 0;
// start first ray subtracting half of the FOV
rays = [];
// loop all columns casting the rays
for (var i = 0; i < NUM_RAYS; i++) {
var rayAngle = (player.rotationAngle - FOV/2.0) + (i/NUM_RAYS) * FOV; // TODO: REVIEW AND TRY TO EXPLAIN THIS LINE OF CODE
var ray = new Ray(rayAngle);
ray.cast();
rays.push(ray);
// rayAngle += FOV / NUM_RAYS;
columnId++; //useless
}
}
function setFOV(angle) {
FOV = angle * (Math.PI/180);
}
function setup() {
var myCanvas = createCanvas(WINDOW_WIDTH*2, WINDOW_HEIGHT);
myCanvas.parent("gameWindow")
bg = loadImage('image.jpg');
mouse = mouseX;
}
function update() {
player.update();
}
function draw() {
update();
image(bg,WINDOW_WIDTH,0, width, height);
grid.render();
castAllRays();
for (ray of rays) {
ray.render();
}
player.render();
for (var i = 0; i < NUM_RAYS; i++) {
// TODO: figure out this formula
var lineHeight = 32*(WINDOW_HEIGHT) / rays[i].distance;
var drawStart = -lineHeight / 2 + (WINDOW_HEIGHT + 100) / 2;
if (drawStart < 0)
drawStart = 0;
var drawEnd = lineHeight / 2 + WINDOW_HEIGHT / 2;
if (drawEnd >= WINDOW_HEIGHT)
drawEnd = WINDOW_HEIGHT - 1;
// where 3d stuff is being rendered
noStroke();
stroke(rays[i].color);
strokeWeight(4);
fill(255, 0, 0);
rect((i*4) + WINDOW_WIDTH, (drawStart-TILE_SIZE), 0, (drawEnd-drawStart)+TILE_SIZE);
strokeWeight(2);
}
}
// TODO: consider looking at this page if adding sprites in the future:
// https://github.com/ssloy/tinyraycaster/wiki/Part-3:-populating-the-world