Abusing DuckDB-WASM by making SQL draw 3D graphics (Sort Of)Building a SQL-Powered Doom Clone in the Browser I had this slightly crazy idea: Could I ditch most of the conventional JavaScript game loop and rendering logic and build a 3D game engine where SQL queries did the heavy lifting? Naturally, I decided to try building a primitive, text-based Doom clone to see how far I could push it using DuckDB-WASM. Spoiler: It kind of works, it was often painful, but I learned a ton about the surprising power (and quirks) of running an analytical database engine in the browser for tasks it was definitely not designed for. The Setup: SQL Isn't Just for SELECT * Anymore Forget managing game state in JavaScript objects or drawing pixels with Canvas/WebGL. My approach looked like this: The Database is the World: The 16x16 map, player coordinates (x, y, dir), enemy/bullet positions, game settings – everything lives in DuckDB tables, right there in the browser tab. SQL Dictates Reality: Want to move forward? UPDATE player SET x = x + COS(dir)*step, y = y + SIN(dir)*step; Bullet hits a wall? DELETE FROM bullets WHERE EXISTS (SELECT 1 FROM map WHERE ...) Enemy fragged? A JOIN between bullets and enemies followed by DELETE statements. The Renderer is a SQL VIEW: This is where it gets wild. I defined a SQL VIEW named render_3d_frame that actually performs raycasting and renders the 3D scene. This beast uses recursive CTEs to cast rays for each screen column, calculates wall distances (with fish-eye correction!), determines the height of the wall slice for that column, and then uses string_agg to stitch together the characters (' ', ., █, ▓, ▒, ░) for each row of the final text frame. Here's the core of the raycasting algorithm in SQL: Yes, SQL is calculating perspective and drawing characters. DuckDB's recursive CTE capabilities are unexpectedly powerful for this kind of work. JavaScript Glues It Together (and Handles Sprites): My JS code became the orchestrator. It handles keyboard ...
First seen: 2025-04-22 14:41
Last seen: 2025-04-23 07:44