I am coding a small Javascript/ HTML-canvas Wolfenstein style game. I am following Permadi tutorial.
For now I did suceed to implement the textured wall raycasting. What I want to do now is to do the floor raycasting.
As far as I understand, when I finish to draw a slice of wall, I have to check if it reaches the bottom of the canvas. If not, that means there is a floor to be rendered underneath it. So I grab every pixel from the bottom of the wall to the bottom of the canvas, calculate their coordinates in "real-world", grab their texture and draw them on the screen.
I am using these two schemas for my calculations.
These is my code:
//we check if the wall reaches the bottom of the canvas
// this.wallToBorder = (400 - wallHeight) / 2;
if (this.wallToBorder > 0) {
// we calculate how many pixels we have from bottom of wall to border of canvas
var pixelsToBottom = Math.floor(this.wallToBorder);
//we calculate the distance between the first pixel at the bottom of the wall and the player eyes (canvas.height / 2)
var pixelRowHeight = 200 - pixelsToBottom;
// then we loop through every pixels until we reach the border of the canvas
for (let i = pixelRowHeight; i < 200; i++) {
// we calculate the straight distance between the player and the pixel
var directDistFloor = this.screenDist * (canvas.height/2) / Math.floor(i);
// we calculate it's real world distance with the angle relative to the player
var realDistance = directDistFloor / Math.cos(this.angleR);
// we calculate it's real world coordinates with the player angle
this.floorPointx = this.player.x + Math.cos(this.angle) * realDistance;
this.floorPointy = this.player.y - Math.sin(this.angle) * realDistance;
// we map the texture
var textY = Math.floor(this.floorPointx % 64);
var textX = Math.floor(this.floorPointy % 64);
var pixWidthHeight = (1 / realDistance) * this.screenDist;
if (pixWidthHeight < 1) pixWidthHeight = 1;
// we draw it on the canvas
this.ctx.drawImage(wallsSprite, textX, textY + 64, 1, 1, this.index, i + 200, pixWidthHeight, pixWidthHeight);
}
}
But The result is not I am expecting:
Here is my project in StackBlitz. What I am doing wrong?