Optimize raycast painter and fix rotation

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 01:04:02 +01:00
parent d58f799aa7
commit 251b759d26
2 changed files with 39 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:wolf_dart/classes/coordinate_2d.dart';
import 'package:wolf_dart/classes/matrix.dart'; import 'package:wolf_dart/classes/matrix.dart';
import 'package:wolf_dart/features/entities/entity.dart'; import 'package:wolf_dart/features/entities/entity.dart';
import 'package:wolf_dart/features/player/player.dart'; import 'package:wolf_dart/features/player/player.dart';
@@ -27,7 +28,6 @@ class RaycasterPainter extends CustomPainter {
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
// Disable anti-aliasing for the background to prevent edge bleeding
final Paint bgPaint = Paint()..isAntiAlias = false; final Paint bgPaint = Paint()..isAntiAlias = false;
// 1. Draw Ceiling & Floor // 1. Draw Ceiling & Floor
@@ -40,36 +40,36 @@ class RaycasterPainter extends CustomPainter {
bgPaint..color = Colors.brown[900]!, bgPaint..color = Colors.brown[900]!,
); );
// --- OPTIMIZATION: Lock to Retro Resolution ---
const int renderWidth = 320; const int renderWidth = 320;
double columnWidth = size.width / renderWidth; double columnWidth = size.width / renderWidth;
// CRITICAL FIX: Disable anti-aliasing so edges remain perfectly sharp
final Paint columnPaint = Paint() final Paint columnPaint = Paint()
..isAntiAlias = false ..isAntiAlias = false
..strokeWidth = columnWidth + 0.5; ..strokeWidth = columnWidth + 0.5;
List<double> zBuffer = List.filled(renderWidth, 0.0); List<double> zBuffer = List.filled(renderWidth, 0.0);
double dirX = math.cos(player.angle); // --- Coordinate2D Camera Vectors ---
double dirY = math.sin(player.angle); Coordinate2D dir = Coordinate2D(
double planeX = -dirY * math.tan(fov / 2); math.cos(player.angle),
double planeY = dirX * math.tan(fov / 2); math.sin(player.angle),
);
// The camera plane is perpendicular to the direction vector, scaled by FOV
Coordinate2D plane = Coordinate2D(-dir.y, dir.x) * math.tan(fov / 2);
// --- 1. CAST WALLS --- // --- 1. CAST WALLS ---
for (int x = 0; x < renderWidth; x++) { for (int x = 0; x < renderWidth; x++) {
double cameraX = 2 * x / renderWidth - 1.0; double cameraX = 2 * x / renderWidth - 1.0;
double rayDirX = dirX + planeX * cameraX; Coordinate2D rayDir = dir + (plane * cameraX);
double rayDirY = dirY + planeY * cameraX;
int mapX = player.x.toInt(); int mapX = player.x.toInt();
int mapY = player.y.toInt(); int mapY = player.y.toInt();
double sideDistX; double sideDistX;
double sideDistY; double sideDistY;
double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs(); double deltaDistX = (rayDir.x == 0) ? 1e30 : (1.0 / rayDir.x).abs();
double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs(); double deltaDistY = (rayDir.y == 0) ? 1e30 : (1.0 / rayDir.y).abs();
double perpWallDist; double perpWallDist;
int stepX; int stepX;
@@ -81,14 +81,14 @@ class RaycasterPainter extends CustomPainter {
double doorOffset = 0.0; double doorOffset = 0.0;
Set<String> ignoredDoors = {}; Set<String> ignoredDoors = {};
if (rayDirX < 0) { if (rayDir.x < 0) {
stepX = -1; stepX = -1;
sideDistX = (player.x - mapX) * deltaDistX; sideDistX = (player.x - mapX) * deltaDistX;
} else { } else {
stepX = 1; stepX = 1;
sideDistX = (mapX + 1.0 - player.x) * deltaDistX; sideDistX = (mapX + 1.0 - player.x) * deltaDistX;
} }
if (rayDirY < 0) { if (rayDir.y < 0) {
stepY = -1; stepY = -1;
sideDistY = (player.y - mapY) * deltaDistY; sideDistY = (player.y - mapY) * deltaDistY;
} else { } else {
@@ -96,6 +96,7 @@ class RaycasterPainter extends CustomPainter {
sideDistY = (mapY + 1.0 - player.y) * deltaDistY; sideDistY = (mapY + 1.0 - player.y) * deltaDistY;
} }
// DDA Loop
while (!hit) { while (!hit) {
if (sideDistX < sideDistY) { if (sideDistX < sideDistY) {
sideDistX += deltaDistX; sideDistX += deltaDistX;
@@ -122,8 +123,8 @@ class RaycasterPainter extends CustomPainter {
? (sideDistX - deltaDistX) ? (sideDistX - deltaDistX)
: (sideDistY - deltaDistY); : (sideDistY - deltaDistY);
double wallXTemp = (side == 0) double wallXTemp = (side == 0)
? player.y + perpWallDistTemp * rayDirY ? player.y + perpWallDistTemp * rayDir.y
: player.x + perpWallDistTemp * rayDirX; : player.x + perpWallDistTemp * rayDir.x;
wallXTemp -= wallXTemp.floor(); wallXTemp -= wallXTemp.floor();
if (wallXTemp < currentOffset) { if (wallXTemp < currentOffset) {
ignoredDoors.add(doorKey); ignoredDoors.add(doorKey);
@@ -147,12 +148,9 @@ class RaycasterPainter extends CustomPainter {
zBuffer[x] = perpWallDist; zBuffer[x] = perpWallDist;
double wallX; double wallX = (side == 0)
if (side == 0) { ? player.y + perpWallDist * rayDir.y
wallX = player.y + perpWallDist * rayDirY; : player.x + perpWallDist * rayDir.x;
} else {
wallX = player.x + perpWallDist * rayDirX;
}
wallX -= wallX.floor(); wallX -= wallX.floor();
double drawX = x * columnWidth; double drawX = x * columnWidth;
@@ -173,21 +171,23 @@ class RaycasterPainter extends CustomPainter {
// --- 2. DRAW SPRITES --- // --- 2. DRAW SPRITES ---
List<Entity> activeSprites = List.from(entities); List<Entity> activeSprites = List.from(entities);
// Sort sprites from furthest to closest using Coordinate2D
activeSprites.sort((a, b) { activeSprites.sort((a, b) {
double distA = double distA = player.position.distanceTo(a.position);
math.pow(player.x - a.x, 2) + math.pow(player.y - a.y, 2).toDouble(); double distB = player.position.distanceTo(b.position);
double distB =
math.pow(player.x - b.x, 2) + math.pow(player.y - b.y, 2).toDouble();
return distB.compareTo(distA); return distB.compareTo(distA);
}); });
for (Entity entity in activeSprites) { for (Entity entity in activeSprites) {
double spriteX = entity.x - player.x; // Relative position to player
double spriteY = entity.y - player.y; Coordinate2D spritePos = entity.position - player.position;
double invDet = 1.0 / (planeX * dirY - dirX * planeY); // Transform sprite with the inverse camera matrix
double transformX = invDet * (dirY * spriteX - dirX * spriteY); double invDet = 1.0 / (plane.x * dir.y - dir.x * plane.y);
double transformY = invDet * (-planeY * spriteX + planeX * spriteY); double transformX = invDet * (dir.y * spritePos.x - dir.x * spritePos.y);
double transformY =
invDet * (-plane.y * spritePos.x + plane.x * spritePos.y);
if (transformY > 0) { if (transformY > 0) {
int spriteScreenX = ((renderWidth / 2) * (1 + transformX / transformY)) int spriteScreenX = ((renderWidth / 2) * (1 + transformX / transformY))
@@ -217,7 +217,6 @@ class RaycasterPainter extends CustomPainter {
int colorByte = spritePixels[texX][ty]; int colorByte = spritePixels[texX][ty];
if (colorByte != 255) { if (colorByte != 255) {
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
double endY = startY + stepY + 0.5; double endY = startY + stepY + 0.5;
if (endY > 0 && startY < size.height) { if (endY > 0 && startY < size.height) {
@@ -276,7 +275,6 @@ class RaycasterPainter extends CustomPainter {
paint.color = ColorPalette.vga[colorByte]; paint.color = ColorPalette.vga[colorByte];
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
double endY = startY + stepY + 0.5; double endY = startY + stepY + 0.5;
if (endY > 0 && startY < size.height) { if (endY > 0 && startY < size.height) {
@@ -286,15 +284,10 @@ class RaycasterPainter extends CustomPainter {
paint, paint,
); );
} }
startY += stepY; startY += stepY;
} }
} }
@override @override
bool shouldRepaint(RaycasterPainter oldDelegate) { bool shouldRepaint(RaycasterPainter oldDelegate) => true;
// Because the Player object instance remains the same, a pure equality check fails.
// Given that your Ticker loop calls setState every frame, returning true is safest.
return true;
}
} }

View File

@@ -183,6 +183,13 @@ class _WolfRendererState extends State<WolfRenderer>
// 2. Explicit State Updates // 2. Explicit State Updates
player.updateWeaponSwitch(); player.updateWeaponSwitch();
player.angle += inputResult.dAngle;
// Keep the angle neatly clamped between 0 and 2*PI
if (player.angle < 0) player.angle += 2 * math.pi;
if (player.angle >= 2 * math.pi) player.angle -= 2 * math.pi;
_applyMovementAndCollision(inputResult.movement); _applyMovementAndCollision(inputResult.movement);
_updateEntities(elapsed); _updateEntities(elapsed);