Skip to content

Instantly share code, notes, and snippets.

@matt-main
Last active September 25, 2020 19:30
Show Gist options
  • Save matt-main/af067f6fe60bc15e7d0bb537adb2a468 to your computer and use it in GitHub Desktop.
Save matt-main/af067f6fe60bc15e7d0bb537adb2a468 to your computer and use it in GitHub Desktop.
Link to raytracing example at gifboy.io
Adapted from lodev's and yonaba's raytracing tutorial
https://gifboy.io/--gifboy-wolfenstein%0A--based%20on%20lodev's%20and%20Yonaba's%20raycasting%20tutorial%0A%0Amap=%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%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%0Aend%0Aif%20raydiry%3C0%20then%0Astepy=-1%0Asidedisty=(rayposy-mapy)*deltadisty%0Aelse%20stepy=1%0Asidedisty=(mapy+1-rayposy)*deltadisty%0Aend%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%0Aperp=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