Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LabDemo2Js (Beta) :: "calculate Walls.js"
- // Raycasting and rendering logic for walls
- function calculateWalls(canvas) {
- // [ canvas, context, XDIM, YDIM ] = windowFit(canvas);
- // [ XDIM, YDIM ] = [ 320, 240 ];
- walls.length = 0; // Reset the array
- cosang = Math.cos(player.angle);
- sinang = Math.sin(player.angle);
- vxinc = sinang * -2 / XDIM;
- vx = cosang + sinang + vxinc * 0.5;
- vyinc = cosang * 2 / XDIM;
- vy = sinang - cosang + vyinc * 0.5;
- for (let sx = 0; sx < XDIM; sx++) {
- scan.x = Math.floor(player.x);
- xdir = Math.sign(vx);
- incx = Math.abs(vx);
- scan.y = Math.floor(player.y);
- ydir = Math.sign(vy);
- incy = Math.abs(vy);
- xtemp = player.x - scan.x;
- if (xdir > 0) xtemp = 1 - xtemp;
- ytemp = player.y - scan.y;
- if (ydir > 0) ytemp = 1 - ytemp;
- d = xtemp * incy - ytemp * incx;
- let textureX; // Horizontal texture position
- // Raycasting loop to find the closest wall
- while (true) {
- if (d < 0) {
- scan.x += xdir;
- if (scan.x < 0 || scan.x >= board.length || scan.y < 0 || scan.y >= board[1].length) break;
- if (board[scan.x] && board[scan.x][scan.y] && board[scan.x][scan.y][0]) {
- hx = scan.x;
- if (xdir < 0) hx += 1;
- hy = player.y + vy * (hx - player.x) / vx;
- bbx = Math.floor((hy - Math.floor(hy)) * board[1].length);
- if (xdir < 0) bbx = board.length - bbx;
- // Capture horizontal texture position
- textureX = hy - Math.floor(hy); // When moving vertically
- if(detectBackFace()){
- textureX = hy - textureX;
- }
- break;
- }
- d += incy;
- } else {
- scan.y += ydir;
- if (scan.x < 0 || scan.x >= MagicSpan || scan.y < 0 || scan.y >= MagicSpan) break;
- if (
- board &&
- board[Math.floor(scan.x)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)][0]
- ){
- hy = scan.y;
- if (ydir < 0) hy += 1;
- hx = player.x + vx * (hy - player.y) / vy;
- bbx = Math.floor((hx - Math.floor(hx)) * board.length);
- if (ydir > 0) bbx = board[0].length - bbx;
- // Capture horizontal texture position
- textureX = hx - Math.floor(hx); // When moving horizontally
- if(detectBackFace()){
- textureX = hx - textureX;
- }
- break;
- }
- d -= incx;
- }
- }
- // Calculate the distance to the wall
- dist = cosang * (hx - player.x) + sinang * (hy - player.y);
- if (dist > MagicMin / MagicSpan) {
- // Calculate vertical screen positions
- sy1 = Math.floor(YDIM / 2 + (-player.z - 0.5) * HVAL / dist);
- sy2 = Math.floor(YDIM / 2 + (-player.z + 0.5) * HVAL / dist);
- // Apply clipping to ensure the strip stays within the screen
- let clippedTop = 0;
- let clippedBottom = 0;
- if (sy1 < 0) {
- clippedTop = -sy1;
- sy1 = 0;
- }
- if (sy2 >= YDIM) {
- clippedBottom = sy2 - (YDIM - MagicMin);
- sy2 = YDIM - MagicMin;
- }
- const visibleHeight = sy2 - sy1 + MagicMin;
- // Calculate the vertical texture position (V-coordinate)
- let textureYStart = clippedTop / visibleHeight;
- let textureYEnd = (visibleHeight - clippedBottom) / visibleHeight;
- // Render the wall slice with the calculated height
- // renderMock(sx, visibleHeight);
- if (
- board &&
- board[Math.floor(scan.x)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)][0] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)][0] !== ``
- ){
- mock = mockData[board[Math.floor(scan.x)][Math.floor(scan.y)][0]];
- if (mock.complete && mock.naturalWidth > 0) {
- walls.push({
- graphic: board[Math.floor(scan.x)][Math.floor(scan.y)][0],
- scanx: scan.x,
- scany: scan.y,
- sx: sx,
- sy1: sy1,
- sy2: sy2,
- visibleHeight: visibleHeight,
- dist: dist,
- textureX: textureX,
- textureYStart: textureYStart,
- textureYEnd: textureYEnd
- });
- break;
- }else{
- console.log(`Faulty Mock: ${board[Math.floor(scan.x)][Math.floor(scan.y)][0]}`);
- }
- }else{
- console.log(`Faulty Board Position: ${Math.floor(scan.x)},${Math.floor(scan.y)}`);
- break;
- }
- } else {
- // If no wall is found, render the floor/ceiling
- sy1 = YDIM / 2;
- sy2 = sy1;
- visibleHeight = YDIM;
- if (
- board &&
- board[Math.floor(scan.x)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)][0] &&
- board[Math.floor(scan.x)][Math.floor(scan.y)][0] !== ``
- ){
- mock = mockData[board[Math.floor(scan.x)][Math.floor(scan.y)][0]];
- if (mock.complete && mock.naturalWidth > 0) {
- walls.push({
- graphic: board[Math.floor(scan.x)][Math.floor(scan.y)][0],
- scanx: scan.x,
- scany: scan.y,
- sx: sx,
- sy1: sy1,
- sy2: sy2,
- visibleHeight: visibleHeight,
- dist: dist,
- textureX: textureX,
- textureYStart: textureYStart,
- textureYEnd: textureYEnd
- });
- break;
- }else{
- console.log(`Faulty Mock: ${board[Math.floor(scan.x)][Math.floor(scan.y)][0]}`);
- }
- }else{
- console.log(`Faulty Board Position: ${Math.floor(scan.x)},${Math.floor(scan.y)}`);
- break;
- }
- }
- // Update the increment values for the next strip
- vx += vxinc;
- vy += vyinc;
- }
- }
- // Function to determine if a face is the back face
- function isBackFace(faceNormal, cameraPosition, facePosition) {
- // Calculate the view direction vector
- let viewDirection = {
- x: facePosition.x - cameraPosition.x,
- y: facePosition.y - cameraPosition.y,
- z: facePosition.z - cameraPosition.z
- };
- // Normalize the view direction (optional, for consistency)
- let length = Math.sqrt(viewDirection.x * viewDirection.x + viewDirection.y * viewDirection.y + viewDirection.z * viewDirection.z);
- viewDirection.x /= length;
- viewDirection.y /= length;
- viewDirection.z /= length;
- // Calculate the dot product of the face normal and the view direction
- let dotProduct = faceNormal.x * viewDirection.x + faceNormal.y * viewDirection.y + faceNormal.z * viewDirection.z;
- // If the dot product is negative, it's the back face
- return dotProduct < 0;
- }
- // Example usage
- function detectBackFace ( ) {
- let cameraPosition = { x: player.x, y: player.y, z: player.z };
- let facePosition = { x: scan.x, y: scan.y, z: 0 };
- // Assume this is the normal for a face pointing outwards
- let faceNormal = { x: 0, y: 0, z: -1 };
- if (isBackFace(faceNormal, cameraPosition, facePosition)) {
- // console.log("This is the back face.");
- return true;
- } else {
- // console.log("This is the front face.");
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement