temposabel

Axel Svendsen GA

Apr 25th, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1.  
  2. from logging import raiseExceptions
  3. from math import *
  4. from operator import truediv
  5. import os
  6. from multiprocessing import set_forkserver_preload
  7. import re
  8. from time import sleep
  9. from tracemalloc import start
  10. from weakref import ref
  11.  
  12.  
  13. #asciiscale
  14. asciicontroll = 13
  15.  
  16. #asciiscale = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,^`'."
  17. #asciiscale = [i for i in reversed("$hz-:,`.")]
  18. #asciiscale = ".-^z$"
  19. asciiscale = ".-^^zzzz$$$$$$"
  20. #index 0 - 67
  21.  
  22. #start
  23.  
  24. filetosavein = "run3"
  25.  
  26. startpos = [-6,1,0]
  27.  
  28. #physics
  29.  
  30. stepa = 100*5
  31. stepsize = 0.05
  32.  
  33. #rendering
  34. doFloor = True
  35. doReflections = True
  36. doFloorlight = True
  37. #doReflight = True
  38. doLighting = False
  39.  
  40. lightpos = [0,0,3]
  41.  
  42. fovx = 90
  43. fovy = 60
  44.  
  45. resx = 90*3*2
  46. resy = 30*3*2
  47.  
  48. #physicslist
  49.  
  50. itemlist = []
  51.  
  52. itemlist.append(["circle", [0,5,0], 2])
  53.  
  54. def distance(point1,point2):
  55.  
  56.     d = sqrt(pow(point2[0] - point1[0], 2) +
  57.                 pow(point2[1] - point1[1], 2) +
  58.                 pow(point2[2] - point1[2], 2)* 1.0)
  59.     return d
  60.  
  61. def check(pos):
  62.     if doFloor:
  63.         if pos[1] <= -2:
  64.            
  65.             if (pos[0] % 4 <= 2 and pos[2] % 4 <= 2) or ((pos[0]+2) % 4 <= 2 and (pos[2]+2) % 4 <= 2) :
  66.                 if doFloorlight:
  67.                     sim = lighting(pos,vectoradd(pos,[0,-1,0]),lightpos)
  68.                     char = asciiscale[round(sim*asciicontroll)]
  69.  
  70.                     return [True, "floor" , char]  
  71.  
  72.                 return [True, "floor" , "#"]  
  73.            
  74.             else:return[True, "floor" , " "]  
  75.  
  76.     for item in itemlist:
  77.         if item[0] == "circle":
  78.             if distance(pos,item[1]) < item[2]:
  79.                 return [True, item]                                            
  80.             else:
  81.                 return [False]    
  82.        
  83. def vectoradd(obj1,obj2):
  84.     return [obj1[0]+obj2[0],obj1[1]+obj2[1],obj1[2]+obj2[2]]
  85.  
  86. def vectorsub(obj1,obj2):
  87.     return [obj1[0]-obj2[0],obj1[1]-obj2[1],obj1[2]-obj2[2]]
  88.  
  89. def intersect(startpos,xdif,ydif,zdif):
  90.     pos = startpos
  91.     pos = [pos[0] + xdif * stepsize, pos[1] + ydif * stepsize, pos[2] + zdif * stepsize]
  92.     pos = [pos[0] + xdif * stepsize, pos[1] + ydif * stepsize, pos[2] + zdif * stepsize]
  93.    
  94.    
  95.     for _ in range(stepa):
  96.         #print(pos)
  97.         res = check(pos)
  98.         if res[0]:
  99.             return [True,pos,res]
  100.         else:
  101.             pos = [pos[0] + xdif * stepsize, pos[1] + ydif * stepsize, pos[2] + zdif * stepsize]
  102.     return [False]
  103.  
  104. def vectortoangle(vector):
  105.     xrot = 0
  106.     yrot = 0
  107.    
  108.     #vector = nornalize(vector)
  109.  
  110.     xrot = degrees(atan2(vector[2],vector[0]))
  111.  
  112.     xlen = sqrt(vector[2]**2 + vector[0]**2)
  113.  
  114.     yrot = degrees(atan2(vector[1],xlen))
  115.    
  116.     return(xrot,yrot)
  117.  
  118.  
  119.  
  120. def angletovector(anglex,angley):
  121.     xcoefficient = cos(radians(angley))
  122.     ydif = sin(radians(angley))
  123.     xdif = cos(radians(anglex)) * xcoefficient
  124.     zdif = sin(radians(anglex)) * xcoefficient
  125.     return [xdif,ydif,zdif]
  126.  
  127. def sray(startpos,anglex,angley):
  128.     v = angletovector(anglex,angley)
  129.     return intersect(startpos,v[0],v[1],v[2])
  130.  
  131. def nornalizetovalue(v,am):
  132.     a = sqrt(v[1]**2+v[2]**2+v[0]**2)
  133.     a = a/am
  134.     return [v[0]/a,v[1]/a,v[2]/a]
  135.    
  136.  
  137. def nornalize(v):
  138.     a = sqrt(v[1]**2+v[2]**2+v[0]**2)
  139.    
  140.     return [v[0]/a,v[1]/a,v[2]/a]
  141.    
  142. def dotproduct(v1,v2):
  143.     v1 = nornalize(v1)
  144.     v2 = nornalize(v2)
  145.    
  146.     return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]
  147.  
  148.  
  149. def lighting(colisionpos,objpos,lightpos):
  150.     normal = vectorsub(colisionpos,objpos)
  151.                
  152.     directiontowardslight = vectorsub(lightpos,colisionpos)
  153.    
  154.     sim = dotproduct(normal,directiontowardslight)
  155.  
  156.     return sim
  157.  
  158. def reflect(colisionpos,objpos,raydirectionx,raydirectiony):
  159.     #print(objpos)
  160.     raydirectionx = 180+raydirectionx
  161.     raydirectiony = -raydirectiony
  162.  
  163.     normal = vectorsub(colisionpos,objpos)
  164.     normangle = vectortoangle(normal)
  165.  
  166.     diff = normangle[0] - raydirectionx , normangle[1] - raydirectiony
  167.  
  168.     newdirection = raydirectionx + diff[0]*2 , raydirectiony + diff[1]*2
  169.    
  170.     res = sray(colisionpos,newdirection[0],newdirection[1])
  171.    
  172.     if res[0]:
  173.  
  174.         try:
  175.             if res[2][2] == " ": return "´"
  176.             else:
  177.                 return res[2][2]
  178.  
  179.         except IndexError:
  180.             return "4"
  181.     else: return "´"
  182.  
  183.  
  184.  
  185. def render2(anglex,angley):
  186.     frame = []
  187.     fovystep = fovy/resy
  188.     fovxstep = fovx/resx
  189.     for y in range(resy):
  190.         currentline = []
  191.         for x in range(resx):
  192.             #print((-fovx/2)+fovxstep*x)
  193.             #print(anglex+(-fovx/2)+fovxstep*x,angley+fovy/2-fovystep*y)
  194.             res = sray(startpos, anglex+(-fovx/2)+fovxstep*x , angley+fovy/2-fovystep*y)
  195.             if res[0]:
  196.                 doRest = True
  197.                 obj = res[1]
  198.  
  199.                 if doFloor:
  200.                     floorcheck = res[2][1]
  201.                     if floorcheck == "floor":
  202.                         doRest = False
  203.                         frame.append(res[2][2])
  204.  
  205.  
  206.                 if doRest:
  207.                     doRest = True
  208.                     #obj[1] is the pos of the object we colided with
  209.                     #res[1] is the pos of the colosion
  210.                     #together these are used to calculate the normal of the position on the object we colided with
  211.                    
  212.                     if doReflections == True:
  213.                         #print(anglex+(-fovx/2)+fovxstep*x,angley+fovy/2-fovystep*y)
  214.                         #print(res[1])
  215.                         frame.append(reflect(res[1],itemlist[0][1],anglex+(-fovx/2)+fovxstep*x,angley+fovy/2-fovystep*y))
  216.                         doRest = False
  217.  
  218.                     if doRest:
  219.                         doRest = False
  220.  
  221.                         if doLighting == True:
  222.                             sim = lighting(res[1],obj[1],lightpos)
  223.                         else:
  224.                             frame.append("#")
  225.                            
  226.                         if doLighting == True:
  227.                             if sim > 0.1:
  228.  
  229.                                 frame.append(asciiscale[round(sim*asciicontroll)])
  230.                             else:
  231.                                 frame.append("`")
  232.  
  233.             else: frame.append(" ")
  234.            
  235.         frame.append("\n")
  236.    # sleep(1)    
  237.     #os.system("cls")
  238.     f = open(filetosavein,"a")
  239.     print("".join(frame))
  240.     f.write("".join(frame))
  241.     f.close()
  242.  
  243. def reftest(colisionpos,objpos,raydirectionx,raydirectiony):
  244.     raydirectionx = 180+raydirectionx
  245.     raydirectiony = -raydirectiony
  246.  
  247.     normal = vectorsub(colisionpos,objpos)
  248.     normangle = vectortoangle(normal)
  249.  
  250.     diff = normangle[0] - raydirectionx , normangle[1] - raydirectiony
  251.  
  252.     newdirection = raydirectionx + diff[0]*2 , raydirectiony + diff[1]*2
  253.    
  254.     res = sray(colisionpos,newdirection[0],newdirection[1])
  255.    
  256.  
  257.     #print(res)
  258.  
  259. a= 0
  260. b= -17
  261. print(sray([-6,0,0.2],a,b))
  262.  
  263. print(reflect([-1.4097371713774294, -1.4033841826691358, 0.2],[0, 0, 0],a,b))
  264.  
  265. v = 20
  266. startpos = [-6,5,-2]
  267. for i in range(24):
  268.     startpos[2] = -2 + (4/24) * i
  269.     v -= 40/24
  270.     render2(v,0)
  271.  
  272.  
Add Comment
Please, Sign In to add comment