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();
|
||||
|
||||
// 2. Camera Plane Setup
|
||||
// Direction vector of the player
|
||||
double dirX = math.cos(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 planeY = dirX * math.tan(fov / 2);
|
||||
|
||||
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 rayDirX = dirX + planeX * cameraX;
|
||||
double rayDirY = dirY + planeY * cameraX;
|
||||
|
||||
// Current map box we are in
|
||||
int mapX = player.x.toInt();
|
||||
int mapY = player.y.toInt();
|
||||
|
||||
// Length of ray from current position to next x or y-side
|
||||
double sideDistX;
|
||||
double sideDistY;
|
||||
|
||||
// Length of ray from one x or y-side to next x or y-side
|
||||
double deltaDistX = (rayDirX == 0)
|
||||
? double.infinity
|
||||
: (1.0 / rayDirX).abs();
|
||||
double deltaDistY = (rayDirY == 0)
|
||||
? double.infinity
|
||||
: (1.0 / rayDirY).abs();
|
||||
double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs();
|
||||
double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs();
|
||||
double perpWallDist;
|
||||
|
||||
// Direction to step in x or y direction (+1 or -1)
|
||||
int stepX;
|
||||
int stepY;
|
||||
|
||||
bool hit = false;
|
||||
// 0 for North/South (vertical) walls, 1 for East/West (horizontal) walls
|
||||
bool hitOutOfBounds = false;
|
||||
int side = 0;
|
||||
int hitWallId = 0;
|
||||
|
||||
// Calculate step and initial sideDist
|
||||
if (rayDirX < 0) {
|
||||
stepX = -1;
|
||||
sideDistX = (player.x - mapX) * deltaDistX;
|
||||
@@ -94,7 +79,6 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
// 3. The True DDA Loop
|
||||
while (!hit) {
|
||||
// Jump to next map square, either in x-direction, or in y-direction
|
||||
if (sideDistX < sideDistY) {
|
||||
sideDistX += deltaDistX;
|
||||
mapX += stepX;
|
||||
@@ -105,34 +89,34 @@ class RaycasterPainter extends CustomPainter {
|
||||
side = 1;
|
||||
}
|
||||
|
||||
// Check bounds and wall collisions
|
||||
if (mapY < 0 ||
|
||||
mapY >= map.length ||
|
||||
mapX < 0 ||
|
||||
mapX >= map[0].length) {
|
||||
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) {
|
||||
hit = true;
|
||||
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) {
|
||||
perpWallDist = (sideDistX - deltaDistX);
|
||||
} else {
|
||||
perpWallDist = (sideDistY - deltaDistY);
|
||||
}
|
||||
|
||||
// 4. Calculate exact wall hit coordinate for textures
|
||||
double wallX;
|
||||
if (side == 0) {
|
||||
wallX = player.y + perpWallDist * rayDirY;
|
||||
} else {
|
||||
wallX = player.x + perpWallDist * rayDirX;
|
||||
}
|
||||
wallX -= wallX.floor(); // Get just the fractional part (0.0 to 0.99)
|
||||
wallX -= wallX.floor();
|
||||
|
||||
_drawTexturedColumn(
|
||||
canvas,
|
||||
@@ -166,9 +150,10 @@ class RaycasterPainter extends CustomPainter {
|
||||
// Wolf3D stores textures in pairs. Even = N/S (Light), Odd = E/W (Dark).
|
||||
int texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
||||
|
||||
// INTERCEPT DOORS
|
||||
if (hitWallId >= 90) {
|
||||
texNum = 98; // 98 is the canonical index for the Wood Door in VSWAP
|
||||
// Optional: Doors don't usually have a dark E/W variant, so we don't add +1 for side
|
||||
// Safely clamp the door texture index so it never crashes the paint loop!
|
||||
texNum = 98.clamp(0, textures.length - 1);
|
||||
} else {
|
||||
// Standard wall texture pairing
|
||||
texNum = ((hitWallId - 1) * 2).clamp(0, textures.length - 2);
|
||||
|
||||
Reference in New Issue
Block a user