Last active
May 12, 2022 16:05
-
-
Save Tunas1337/2010bdfab45c812b2ada1929c11a107e to your computer and use it in GitHub Desktop.
Raytrace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kandinsky | |
import math | |
import ion | |
from ion import * | |
global menu | |
menu = False | |
menu=False | |
fovy=60 | |
global camera | |
camera=[10,10,0.05] | |
w=320 | |
h=240 | |
planeX=0 | |
planeY=1 | |
walls=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1],[1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],[1,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1],[1,0,0,1,1,1,1,0,0,1,0,0,0,0,0,1],[1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1],[1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1],[1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1],[1,1,1,1,1,1,1,0,0,1,0,0,1,0,0,1],[1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1],[1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1],[1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1],[1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1],[1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1],[1,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1],[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]] | |
def racine(a): | |
x = 1 | |
y = 0.5*(1+a) | |
while abs(y-x) > 0.001: | |
x = y | |
y = 0.5*(x+a/x) | |
return y | |
def renderMap(walls, w, h, camera): | |
for x in range(0, w, 2): | |
dirX = math.cos(camera[2]) | |
dirY = math.sin(camera[2]) | |
planeX = math.cos(camera[2]-1.5707963267949) | |
planeY = math.sin(camera[2]-1.5707963267949) | |
cameraX=(x*2/w)-1 | |
rayPosX=camera[0] | |
rayPosY=camera[1] | |
rayDirX=dirX+planeX*cameraX | |
rayDirY=dirY+planeY*cameraX | |
mapX=math.floor(rayPosX) | |
mapY=math.floor(rayPosY) | |
deltaDistX=racine(1+(rayDirY*rayDirY)/(rayDirX*rayDirX)) | |
deltaDistY=racine(1+(rayDirX*rayDirX)/(rayDirY*rayDirY)) | |
hit=0 | |
if rayDirX<0: | |
stepX=-1 | |
sideDistX=(rayPosX-mapX)*deltaDistX | |
else: | |
stepX = 1 | |
sideDistX = (mapX+1-rayPosX)*deltaDistX | |
if rayDirY<0: | |
stepY=-1 | |
sideDistY=(rayPosY-mapY)*deltaDistY | |
else: | |
stepY=1 | |
sideDistY=(mapY+1-rayPosY)*deltaDistY | |
while hit==0: | |
if sideDistX<sideDistY: | |
sideDistX=sideDistX+deltaDistX | |
mapX=mapX+stepX | |
side=0 | |
else: | |
sideDistY=sideDistY+deltaDistY | |
mapY=mapY+stepY | |
side=1 | |
if walls[mapX][mapY]>0: | |
hit=1 | |
if side==0: | |
perpWallDist=abs((mapX-rayPosX+(1-stepX)/2)/rayDirX) | |
else: | |
perpWallDist=abs((mapY-rayPosY+(1-stepY)/2)/rayDirY) | |
lineHeight=abs(math.floor(h/perpWallDist)) | |
drawStart=math.floor(-lineHeight/2+h/2) | |
drawEnd=math.floor(lineHeight/2+h/2) | |
if drawStart<0: | |
drawStart=0 | |
if drawEnd>=h: | |
drawEnd=h-1 | |
color = kandinsky.color(100,10,10) | |
if side==1: | |
color = kandinsky.color(100,100,100) | |
kandinsky.fill_rect(x,drawStart,2,drawEnd-drawStart, color) | |
def onenterKey(): | |
renderMap(walls, 384, 216, camera) | |
def onarrowKey(): | |
turn=False | |
if ion.keydown(KEY_LEFT): | |
camera[2]=camera[2]+0.1 | |
onpaint() | |
if ion.keydown(KEY_RIGHT): | |
camera[2]=camera[2]-0.1 | |
onpaint() | |
if ion.keydown(KEY_UP): | |
if walls[math.floor(camera[0]+math.cos(camera[2])/3)][math.floor(camera[1]+math.sin(camera[2])/3)]==0: | |
camera[0]=camera[0]+math.cos(camera[2])/3 | |
camera[1]=camera[1]+math.sin(camera[2])/3 | |
onpaint() | |
if ion.keydown(KEY_DOWN) and walls[math.floor(camera[0]-math.cos(camera[2])/4)][math.floor(camera[1]-math.sin(camera[2])/4)]==0: | |
camera[0]=camera[0]-math.cos(camera[2])/4 | |
camera[1]=camera[1]-math.sin(camera[2])/4 | |
onpaint() | |
def onpaint(): | |
##kandinsky.fill_rect(0,0,320,240,"white") | |
color = kandinsky.color(200,200,200) | |
kandinsky.fill_rect(0,0,384,216,color) | |
renderMap(walls,384,216,camera) | |
while True: | |
#onpaint() | |
onarrowKey() | |
if ion.keydown(KEY_OK): | |
onenterKey() | |
if ion.keydown(KEY_ONOFF): | |
onescapeKey() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment