Fix door rendering issue
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -34,49 +34,34 @@ class RaycasterPainter extends CustomPainter {
|
|||||||
|
|
||||||
int screenWidth = size.width.toInt();
|
int screenWidth = size.width.toInt();
|
||||||
|
|
||||||
// 2. Camera Plane Setup
|
|
||||||
// Direction vector of the player
|
|
||||||
double dirX = math.cos(playerAngle);
|
double dirX = math.cos(playerAngle);
|
||||||
double dirY = math.sin(playerAngle);
|
double dirY = math.sin(playerAngle);
|
||||||
|
|
||||||
// The camera plane is perpendicular to the direction vector.
|
|
||||||
// Multiplying by tan(fov/2) scales the plane to match our field of view.
|
|
||||||
double planeX = -dirY * math.tan(fov / 2);
|
double planeX = -dirY * math.tan(fov / 2);
|
||||||
double planeY = dirX * math.tan(fov / 2);
|
double planeY = dirX * math.tan(fov / 2);
|
||||||
|
|
||||||
for (int x = 0; x < screenWidth; x++) {
|
for (int x = 0; x < screenWidth; x++) {
|
||||||
// Calculate where on the camera plane this ray passes (-1 is left edge, 1 is right edge)
|
|
||||||
double cameraX = 2 * x / screenWidth - 1.0;
|
double cameraX = 2 * x / screenWidth - 1.0;
|
||||||
double rayDirX = dirX + planeX * cameraX;
|
double rayDirX = dirX + planeX * cameraX;
|
||||||
double rayDirY = dirY + planeY * cameraX;
|
double rayDirY = dirY + planeY * cameraX;
|
||||||
|
|
||||||
// Current map box we are in
|
|
||||||
int mapX = player.x.toInt();
|
int mapX = player.x.toInt();
|
||||||
int mapY = player.y.toInt();
|
int mapY = player.y.toInt();
|
||||||
|
|
||||||
// Length of ray from current position to next x or y-side
|
|
||||||
double sideDistX;
|
double sideDistX;
|
||||||
double sideDistY;
|
double sideDistY;
|
||||||
|
|
||||||
// Length of ray from one x or y-side to next x or y-side
|
double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs();
|
||||||
double deltaDistX = (rayDirX == 0)
|
double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs();
|
||||||
? double.infinity
|
|
||||||
: (1.0 / rayDirX).abs();
|
|
||||||
double deltaDistY = (rayDirY == 0)
|
|
||||||
? double.infinity
|
|
||||||
: (1.0 / rayDirY).abs();
|
|
||||||
double perpWallDist;
|
double perpWallDist;
|
||||||
|
|
||||||
// Direction to step in x or y direction (+1 or -1)
|
|
||||||
int stepX;
|
int stepX;
|
||||||
int stepY;
|
int stepY;
|
||||||
|
|
||||||
bool hit = false;
|
bool hit = false;
|
||||||
// 0 for North/South (vertical) walls, 1 for East/West (horizontal) walls
|
bool hitOutOfBounds = false;
|
||||||
int side = 0;
|
int side = 0;
|
||||||
int hitWallId = 0;
|
int hitWallId = 0;
|
||||||
|
|
||||||
// Calculate step and initial sideDist
|
|
||||||
if (rayDirX < 0) {
|
if (rayDirX < 0) {
|
||||||
stepX = -1;
|
stepX = -1;
|
||||||
sideDistX = (player.x - mapX) * deltaDistX;
|
sideDistX = (player.x - mapX) * deltaDistX;
|
||||||
@@ -94,7 +79,6 @@ class RaycasterPainter extends CustomPainter {
|
|||||||
|
|
||||||
// 3. The True DDA Loop
|
// 3. The True DDA Loop
|
||||||
while (!hit) {
|
while (!hit) {
|
||||||
// Jump to next map square, either in x-direction, or in y-direction
|
|
||||||
if (sideDistX < sideDistY) {
|
if (sideDistX < sideDistY) {
|
||||||
sideDistX += deltaDistX;
|
sideDistX += deltaDistX;
|
||||||
mapX += stepX;
|
mapX += stepX;
|
||||||
@@ -105,34 +89,34 @@ class RaycasterPainter extends CustomPainter {
|
|||||||
side = 1;
|
side = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check bounds and wall collisions
|
|
||||||
if (mapY < 0 ||
|
if (mapY < 0 ||
|
||||||
mapY >= map.length ||
|
mapY >= map.length ||
|
||||||
mapX < 0 ||
|
mapX < 0 ||
|
||||||
mapX >= map[0].length) {
|
mapX >= map[0].length) {
|
||||||
hit = true;
|
hit = true;
|
||||||
perpWallDist = 20.0; // Out of bounds fallback
|
hitOutOfBounds = true; // Safely abort if we clip out of bounds
|
||||||
} else if (map[mapY][mapX] > 0) {
|
} else if (map[mapY][mapX] > 0) {
|
||||||
hit = true;
|
hit = true;
|
||||||
hitWallId = map[mapY][mapX];
|
hitWallId = map[mapY][mapX];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate distance projected on camera direction (No fisheye effect!)
|
// If the ray escaped the map, don't draw garbage, just draw the background!
|
||||||
|
if (hitOutOfBounds) continue;
|
||||||
|
|
||||||
if (side == 0) {
|
if (side == 0) {
|
||||||
perpWallDist = (sideDistX - deltaDistX);
|
perpWallDist = (sideDistX - deltaDistX);
|
||||||
} else {
|
} else {
|
||||||
perpWallDist = (sideDistY - deltaDistY);
|
perpWallDist = (sideDistY - deltaDistY);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Calculate exact wall hit coordinate for textures
|
|
||||||
double wallX;
|
double wallX;
|
||||||
if (side == 0) {
|
if (side == 0) {
|
||||||
wallX = player.y + perpWallDist * rayDirY;
|
wallX = player.y + perpWallDist * rayDirY;
|
||||||
} else {
|
} else {
|
||||||
wallX = player.x + perpWallDist * rayDirX;
|
wallX = player.x + perpWallDist * rayDirX;
|
||||||
}
|
}
|
||||||
wallX -= wallX.floor(); // Get just the fractional part (0.0 to 0.99)
|
wallX -= wallX.floor();
|
||||||
|
|
||||||
_drawTexturedColumn(
|
_drawTexturedColumn(
|
||||||
canvas,
|
canvas,
|
||||||
@@ -166,9 +150,10 @@ class RaycasterPainter extends CustomPainter {
|
|||||||
// Wolf3D stores textures in pairs. Even = N/S (Light), Odd = E/W (Dark).
|
// Wolf3D stores textures in pairs. Even = N/S (Light), Odd = E/W (Dark).
|
||||||
int texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
int texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
||||||
|
|
||||||
|
// INTERCEPT DOORS
|
||||||
if (hitWallId >= 90) {
|
if (hitWallId >= 90) {
|
||||||
texNum = 98; // 98 is the canonical index for the Wood Door in VSWAP
|
// Safely clamp the door texture index so it never crashes the paint loop!
|
||||||
// Optional: Doors don't usually have a dark E/W variant, so we don't add +1 for side
|
texNum = 98.clamp(0, textures.length - 1);
|
||||||
} else {
|
} else {
|
||||||
// Standard wall texture pairing
|
// Standard wall texture pairing
|
||||||
texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
||||||
|
|||||||
Reference in New Issue
Block a user