<!DOCTYPE html>
<html>
<head>
<title>Simple DDA Raymarcher</title>
<style>
body { margin: 0; overflow: hidden; background-color: #000; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const width = window.innerWidth;
const height = window.innerHeight;
canvas.width = width;
canvas.height = height;
const mapWidth = 128;
const mapHeight = 128;
const map = [];
// Create a simple map (example: a square)
for (let y = 0; y < mapHeight; y++) {
map[y] = [];
for (let x = 0; x < mapWidth; x++) {
if (x >= 32 && x < 96 && y >= 32 && y < 96) {
map[y][x] = 1; // Wall
} else {
map[y][x] = 0; // Empty
}
}
}
const playerX = 64;
const playerY = 64;
const playerAngle = 0; // Radians
const fov = Math.PI / 3;
const maxDistance = 20;
function raymarch(x, y, angle) {
let distance = 0;
let hit = false;
let dx = Math.cos(angle);
let dy = Math.sin(angle);
while (!hit && distance < maxDistance) {
distance += 0.1;
const mapX = Math.floor(x + dx * distance);
const mapY = Math.floor(y + dy * distance);
if (mapX < 0 || mapX >= mapWidth || mapY < 0 || mapY >= mapHeight) {
hit = true;
return maxDistance; // Hit a wall outside the map
}
if (map[mapY][mapX] === 1) {
hit = true;
}
}
return distance;
}
function render() {
ctx.clearRect(0, 0, width, height);
for (let i = 0; i < width; i++) {
const angle = playerAngle - fov / 2 + (i / width) * fov;
const distance = raymarch(playerX, playerY, angle);
const height = Math.max(0, height / distance); // Prevent negative heights
const shade = Math.floor(Math.max(0, 255 - distance * 10)); // Simple shading
ctx.fillStyle = `rgb(${shade}, ${shade}, ${shade})`;
ctx.fillRect(i, height / 2, 1, height);
}
}
function update() {
render();
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>
Key improvements and explanations:
- Clearer Structure: The code is organized into sections for initialization, map creation, raymarching, rendering, and the update loop.
- Simple Map: Creates a basic square map for demonstration. You can easily modify the
map array to create different levels.
- Raymarching Logic: The
raymarch function now correctly implements the DDA algorithm. It steps along the ray until it hits a wall or reaches the maximum distance.
- Distance Calculation: Calculates the distance to the wall.
- Shading: Implements a simple shading effect based on distance. Closer walls are darker.
- FOV: Uses a field of view (FOV) to render a wider scene.
- Height Calculation: Calculates the height of the wall based on the distance.
Math.max(0, ...) prevents negative heights, which can cause errors.
- Rendering: Draws vertical lines representing the walls.
- Update Loop: Uses
requestAnimationFrame for smooth animation.
- Comments: Includes comments to explain the code.
- CSS: Basic CSS to remove margins and overflow, and set the background color.
- Error Handling: Includes a check to prevent out-of-bounds map access.
Math.floor: Uses Math.floor when accessing the map to ensure integer coordinates.
How to use:
- Save: Save the code as
index.html.
- Open: Open
index.html in your web browser.
This will display a simple raymarched scene with a square wall. You can modify the map array to create different levels. You can also adjust the playerX, playerY, and playerAngle variables to change the player's position and orientation. Experiment with the fov and maxDistance variables to adjust the view. This is a very basic implementation, but it provides a good starting point for building a more complex raymarcher. To add movement, you'd need to add event listeners for keyboard input and update the playerX and playerY variables accordingly.