Skip to content

Instantly share code, notes, and snippets.

@triptych
Forked from formatt-c/gifboy-raytracer.lua
Created September 25, 2020 15:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save triptych/bc593850329a79e9e0faff3b4c6c0110 to your computer and use it in GitHub Desktop.
Link to raytracing example at gifboy.io
Adapter from Yonaba's lua raytracer example
https://gifboy.io/map=%7B%0Awidth=10,%0Aheight=10,%0A%7B1,0,0,0,1,1,1,1,1,1%7D,%0A%7B1,0,0,0,0,0,0,0,0,1%7D,%0A%7B1,0,0,0,0,0,0,0,0,1%7D,%0A%7B1,0,0,0,0,0,0,0,0,1%7D,%0A%7B1,0,0,0,0,0,0,0,0,1%7D,%0A%7B1,0,0,0,1,0,0,0,0,1%7D,%0A%7B1,1,0,0,1,1,0,0,0,1%7D,%0A%7B1,0,0,0,1,0,0,0,0,1%7D,%0A%7B1,1,0,0,1,1,0,0,0,1%7D,%0A%7B1,1,1,1,1,1,1,2,2,1%7D%0A%7D%0A%0Aposx=1%0Aposy=3%0Adirx=1%0Adiry=0%0Aplanex=0%0Aplaney=0.66%0Amove=.2%0Arotation=math.pi/2%0Adirection=1%0Ascreenx=160%0Ascreeny=160%0AcolorRoof=6%0AcolorSky=5%0AcolorStripes=10%0A%0A--3d%20loop%0Adelay(150)%0Afor%20i=1,24%20do%0Aclear()%0Ax=0%0Afor%20x=0,screenx%20do%0Acamx=2*x/screenx-1%0Arayposx=posx%0Arayposy=posy%0Araydirx=dirx+planex*camx%0Araydiry=diry+planey*camx%0Amapx=rayposx%0Amapy=rayposy%0Asidedistx=0%0Asidedisty=0%0Adeltadistx=math.sqrt(1+(raydiry*raydiry)/(raydirx*raydirx))%0Adeltadisty=math.sqrt(1+(raydirx*raydirx)/(raydiry*raydiry))%0Aperp=0%0Astepx=0%0Astepy=0%0Ahit=0%0Aside=0%0Aif%20raydirx%3C0%20then%0Astepx=-1%0Asidedistx=(rayposx-mapx)*deltadistx%0Aelse%20stepx=1%0Asidedistx=(mapx+1-rayposx)*deltadistx%20end%0Aif%20raydiry%3C0%20then%0Astepy=-1%0Asidedisty=(rayposy-mapy)*deltadisty%0Aelse%20stepy=1%0Asidedisty=(mapy+1-rayposy)*deltadisty%20end%0Awhile%20hit==0%20do%0Aif%20sidedistx%3Csidedisty%20then%0Asidedistx=sidedistx+deltadistx%0Amapx=mapx+stepx%0Aside=0%0Aelse%0Asidedisty=sidedisty+deltadisty%0Amapy=mapy+stepy%0Aside=1%0Aend%0Aif%20map%5Bmath.floor(mapx)%5D%5Bmath.floor(mapy)%5D%3E0%20then%20hit=1%20end%0Aend%0Aif%20side==0%20then%0Aperp=math.abs((mapx-rayposx+(1-stepx)/2)/raydirx)%0Aelse%20perp=math.abs((mapy-rayposy+(1-stepy)/2)/raydiry)%0Aend%0Alineheight=math.abs(screeny/perp)%0Adraw=-lineheight/2+screeny/2%0Aif%20draw%3C0%20then%20draw=0%20end%0AdrawE=lineheight/2+screeny/2%0Aif%20drawE%3E=screenx%20then%20drawE=screenx-1%20end%0Aa=65%0Ab=58%0Ac=63%0Aif%20side==1%20then%0Aa=a-30%0Ab=b-30%0Ac=c-30%0Aend%0Aif%20map%5Bmath.floor(mapx)%5D%5Bmath.floor(mapy)%5D==2%20then%20a,b,c=255,199,101%20end%0Apalette(14,a,b,c)%0AbandUp=screeny/2+drawE%0AbandDown=screeny/2+draw%20%20%20%20%0Arectfill(x,draw,1,drawE,14);%0Arectfill(x,drawE,1,272,colorRoof);%0Arectfill(x,0,1,draw,colorSky);%0Arectfill(x,bandUp/2,1,1,colorStripes)%0Arectfill(x,bandDown/2,1,1,colorStripes)%0Arectfill(x,screeny/2,1,1,colorStripes)%0Aend%0Arender()%0Aif%20frames()%3C5%20then%20posx=posx+1%20end%0Aif%20frames()%3E=5%20and%20frames()%3C11%20then%20posy=posy+1%20end%0Aif%20frames()%3E=15%20and%20frames()%3C20%20then%20posx=posx+1%20end%0Aend%0A%0A--%20scene%20end%0Adelay(2000)%0Aclear()%0Ascale(2,2)%0Atextb('Level%20Won','center','center',7,1)%0Arender()%0Astop().html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment