https://frankforce.com/city-in-a-bottle-a-256-byte-raycasting-system/ Killed By A Pixel Building Universes One Pixel at a Time [city4-2-940x198] Skip to content * Home * Art * Games * Tools * Code * Resume * About - Happy Pi Day City In A Bottle - A 256 Byte Raycasting System Posted on 05.19.24 by Frank Hello size coding fans. Today, I have something amazing to share: A tiny raycasting engine and city generator that fits in a standalone 256 byte html file. In this post I will share all the secrets about how this magical program works. Here's my tweet that contains the code and a video of the output... A City in a Bottle pic.twitter.com/N3WElPqtMY -- Frank Force (@KilledByAPixel) April 22, 2022 You may have already seen this post on my twitter timeline. After I originally released it about 2 years ago it ended up being one of my most popular tweets ever. Thank you for supporting this strange obsession of mine. Keep reading and I will try my best to explain how and why it works the way it does... This amazing program uses many different concepts in a very small space, understanding it is kind of like solving a puzzle. There are several main pieces including the html code, frame update loop, rendering system, raycasting engine, and the city itself. You might think that it would require some advanced math to unravel this mystery, but actually no, the code is fairly simple and only uses basic algebra, not even any trig functions are used. Though there are a few tricks that allow everything to come together with an impressive result. All Of The Code First we can take a quick look at all the code before we dive in. This is not just a JavaScript snippet but an entire valid HTML program. This is a crazy tight 256 byte block of minified code so it will take some work to make it readable. The HTML Code Let's look at the HTML part of the code before we move onto the JavaScript. Here is just the HTML by itself... It is just a canvas element with an onclick event. I splurged by setting the CSS width to 99%, though it still works fine without so there is some extra space to play around with for future remixes. The canvas id is set to c which gives us a way to access it from JavaScript. The onclick event is what starts the program. The setInterval call is a bit of JavaScript that creates the update loop. The interval time is 9 milliseconds which is a little faster than 60 but close enough that it doesn't matter. The time variable t is also initialized to 9 here to save space. There is a slight bug that will occur if the canvas is clicked multiple times, the interval will also run multiple times causing it to slow down. It's not much of an issue here but something to be aware of. There are several other ways to craft the HTML part of this code, each with their own tradeoffs. A normal script block works fine too, that would just require a little more space. The JavaScript Code Next up we have the 199 byte payload of JavaScript that runs when the canvas is clicked... for(c.width=w=99,++t,i=6e3;i--;c.getContext`2d`.fillRect(i%w,i/w|0,1-d*Z/w+s,1))for(a=i%w/50-1,s=b=1-i/4e3,X=t,Y=Z=d=1;++Z>6)**3^X>>6)*9%w+16>R?R+=S:S>.1?R-=S /=2:x.fillRect(--i%w,T=i>>7,.4+R/w-(Z%8>7?1:X%8>7?3:2)/2*C(Z/8+7| X/8+7|R/4)**2,S=R=1))A=i%w/64*R-R,Z=T/64*R-R*2+A+w*8,X=Z-2*A+w*t Reply on Twitter 1791512622598127968 Retweet on Twitter 1791512622598127968 11 Like on Twitter 1791512622598127968 67 Twitter 1791512622598127968 Load More * KilledByAPixel's GitHub + KilledByAPixel pushed to main in KilledByAPixel/LittleJS + KilledByAPixel starred exoticorn/microw8 + KilledByAPixel pushed to master in KilledByAPixel/ZzFX + KilledByAPixel starred whymirror/camping + KilledByAPixel starred imerso/facecap * 3d 48 hour game art automata barfcade candy code codepen delta smoothing delta time devlog doom dwitter educational game experimental gameplay faster blaster font fractal frank engine fxhash game game dev game engine gamejam Game Jam generative GIF github indie speed run infinite javascript js1k js13k json littlejs ludum dare MAGFest music Panel piroot pixel Postmortem programming puzzle rapid prototype recursive rendering rogue-like screensaver secret formula sifteo Squaresville thought experiment threejs time loop tinycode UE4 wallpaper words zzfx Killed By A Pixel Proudly powered by WordPress. %d