Optimize raycast painter and fix rotation
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_dart/classes/coordinate_2d.dart';
|
||||
import 'package:wolf_dart/classes/matrix.dart';
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
import 'package:wolf_dart/features/player/player.dart';
|
||||
@@ -27,7 +28,6 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
// Disable anti-aliasing for the background to prevent edge bleeding
|
||||
final Paint bgPaint = Paint()..isAntiAlias = false;
|
||||
|
||||
// 1. Draw Ceiling & Floor
|
||||
@@ -40,36 +40,36 @@ class RaycasterPainter extends CustomPainter {
|
||||
bgPaint..color = Colors.brown[900]!,
|
||||
);
|
||||
|
||||
// --- OPTIMIZATION: Lock to Retro Resolution ---
|
||||
const int renderWidth = 320;
|
||||
|
||||
double columnWidth = size.width / renderWidth;
|
||||
|
||||
// CRITICAL FIX: Disable anti-aliasing so edges remain perfectly sharp
|
||||
final Paint columnPaint = Paint()
|
||||
..isAntiAlias = false
|
||||
..strokeWidth = columnWidth + 0.5;
|
||||
|
||||
List<double> zBuffer = List.filled(renderWidth, 0.0);
|
||||
|
||||
double dirX = math.cos(player.angle);
|
||||
double dirY = math.sin(player.angle);
|
||||
double planeX = -dirY * math.tan(fov / 2);
|
||||
double planeY = dirX * math.tan(fov / 2);
|
||||
// --- Coordinate2D Camera Vectors ---
|
||||
Coordinate2D dir = Coordinate2D(
|
||||
math.cos(player.angle),
|
||||
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 ---
|
||||
for (int x = 0; x < renderWidth; x++) {
|
||||
double cameraX = 2 * x / renderWidth - 1.0;
|
||||
double rayDirX = dirX + planeX * cameraX;
|
||||
double rayDirY = dirY + planeY * cameraX;
|
||||
Coordinate2D rayDir = dir + (plane * cameraX);
|
||||
|
||||
int mapX = player.x.toInt();
|
||||
int mapY = player.y.toInt();
|
||||
|
||||
double sideDistX;
|
||||
double sideDistY;
|
||||
double deltaDistX = (rayDirX == 0) ? 1e30 : (1.0 / rayDirX).abs();
|
||||
double deltaDistY = (rayDirY == 0) ? 1e30 : (1.0 / rayDirY).abs();
|
||||
double deltaDistX = (rayDir.x == 0) ? 1e30 : (1.0 / rayDir.x).abs();
|
||||
double deltaDistY = (rayDir.y == 0) ? 1e30 : (1.0 / rayDir.y).abs();
|
||||
double perpWallDist;
|
||||
|
||||
int stepX;
|
||||
@@ -81,14 +81,14 @@ class RaycasterPainter extends CustomPainter {
|
||||
double doorOffset = 0.0;
|
||||
Set<String> ignoredDoors = {};
|
||||
|
||||
if (rayDirX < 0) {
|
||||
if (rayDir.x < 0) {
|
||||
stepX = -1;
|
||||
sideDistX = (player.x - mapX) * deltaDistX;
|
||||
} else {
|
||||
stepX = 1;
|
||||
sideDistX = (mapX + 1.0 - player.x) * deltaDistX;
|
||||
}
|
||||
if (rayDirY < 0) {
|
||||
if (rayDir.y < 0) {
|
||||
stepY = -1;
|
||||
sideDistY = (player.y - mapY) * deltaDistY;
|
||||
} else {
|
||||
@@ -96,6 +96,7 @@ class RaycasterPainter extends CustomPainter {
|
||||
sideDistY = (mapY + 1.0 - player.y) * deltaDistY;
|
||||
}
|
||||
|
||||
// DDA Loop
|
||||
while (!hit) {
|
||||
if (sideDistX < sideDistY) {
|
||||
sideDistX += deltaDistX;
|
||||
@@ -122,8 +123,8 @@ class RaycasterPainter extends CustomPainter {
|
||||
? (sideDistX - deltaDistX)
|
||||
: (sideDistY - deltaDistY);
|
||||
double wallXTemp = (side == 0)
|
||||
? player.y + perpWallDistTemp * rayDirY
|
||||
: player.x + perpWallDistTemp * rayDirX;
|
||||
? player.y + perpWallDistTemp * rayDir.y
|
||||
: player.x + perpWallDistTemp * rayDir.x;
|
||||
wallXTemp -= wallXTemp.floor();
|
||||
if (wallXTemp < currentOffset) {
|
||||
ignoredDoors.add(doorKey);
|
||||
@@ -147,12 +148,9 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
zBuffer[x] = perpWallDist;
|
||||
|
||||
double wallX;
|
||||
if (side == 0) {
|
||||
wallX = player.y + perpWallDist * rayDirY;
|
||||
} else {
|
||||
wallX = player.x + perpWallDist * rayDirX;
|
||||
}
|
||||
double wallX = (side == 0)
|
||||
? player.y + perpWallDist * rayDir.y
|
||||
: player.x + perpWallDist * rayDir.x;
|
||||
wallX -= wallX.floor();
|
||||
|
||||
double drawX = x * columnWidth;
|
||||
@@ -173,21 +171,23 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
// --- 2. DRAW SPRITES ---
|
||||
List<Entity> activeSprites = List.from(entities);
|
||||
|
||||
// Sort sprites from furthest to closest using Coordinate2D
|
||||
activeSprites.sort((a, b) {
|
||||
double distA =
|
||||
math.pow(player.x - a.x, 2) + math.pow(player.y - a.y, 2).toDouble();
|
||||
double distB =
|
||||
math.pow(player.x - b.x, 2) + math.pow(player.y - b.y, 2).toDouble();
|
||||
double distA = player.position.distanceTo(a.position);
|
||||
double distB = player.position.distanceTo(b.position);
|
||||
return distB.compareTo(distA);
|
||||
});
|
||||
|
||||
for (Entity entity in activeSprites) {
|
||||
double spriteX = entity.x - player.x;
|
||||
double spriteY = entity.y - player.y;
|
||||
// Relative position to player
|
||||
Coordinate2D spritePos = entity.position - player.position;
|
||||
|
||||
double invDet = 1.0 / (planeX * dirY - dirX * planeY);
|
||||
double transformX = invDet * (dirY * spriteX - dirX * spriteY);
|
||||
double transformY = invDet * (-planeY * spriteX + planeX * spriteY);
|
||||
// Transform sprite with the inverse camera matrix
|
||||
double invDet = 1.0 / (plane.x * dir.y - dir.x * plane.y);
|
||||
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) {
|
||||
int spriteScreenX = ((renderWidth / 2) * (1 + transformX / transformY))
|
||||
@@ -217,7 +217,6 @@ class RaycasterPainter extends CustomPainter {
|
||||
int colorByte = spritePixels[texX][ty];
|
||||
|
||||
if (colorByte != 255) {
|
||||
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
|
||||
double endY = startY + stepY + 0.5;
|
||||
|
||||
if (endY > 0 && startY < size.height) {
|
||||
@@ -276,7 +275,6 @@ class RaycasterPainter extends CustomPainter {
|
||||
|
||||
paint.color = ColorPalette.vga[colorByte];
|
||||
|
||||
// ADDED 0.5 OVERLAP TO PREVENT VERTICAL SEAMS
|
||||
double endY = startY + stepY + 0.5;
|
||||
|
||||
if (endY > 0 && startY < size.height) {
|
||||
@@ -286,15 +284,10 @@ class RaycasterPainter extends CustomPainter {
|
||||
paint,
|
||||
);
|
||||
}
|
||||
|
||||
startY += stepY;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(RaycasterPainter oldDelegate) {
|
||||
// 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;
|
||||
}
|
||||
bool shouldRepaint(RaycasterPainter oldDelegate) => true;
|
||||
}
|
||||
|
||||
@@ -183,6 +183,13 @@ class _WolfRendererState extends State<WolfRenderer>
|
||||
|
||||
// 2. Explicit State Updates
|
||||
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);
|
||||
_updateEntities(elapsed);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user