@ChrisCheng/

raycasting Engine (By @byonano)

Pygame

raycasting engine

fork
loading
Files
  • main.py
  • nohup.out
  • README.md
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import math
import pygame

pygame.init()

# Window size, window area, camera location X, camera location y, camera ocation z, the length of the direction vectors, resolution, field of view on the x-y plane, field of view on the z-axis, degrees per "pixel" for the phi angle, degrees per "pixel" for the theta angle, speed controller for the camera
wS = 600
wA = wS * wS
cameraLocX = 0
cameraLocY = 0
cameraLocZ = 0
r = 1
phiDirec = 0
thetaDirec = 90
res = 10000
fovPHI = 90
fovTHETA = 90
dppPHI = fovPHI/math.sqrt(res)
dppTHETA = fovTHETA/math.sqrt(res)
speedMult = .1

# all of the point lists
pX = [1, 0]
pY = [1, 10]
pZ = [0, 0]
pColor = [(0, 0, 0), (0, 255, 0)]

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)


def checkPoint(x0, y0, z0, x, y, z, r, phi, theta):
   a = math.cos(math.radians(phi)) * math.sin(math.radians(theta)) * r
   b = math.sin(math.radians(phi)) * math.sin(math.radians(theta)) * r
   c = math.cos(math.radians(theta)) * r

   #x-combined with x0, y-combined with y0, z-combined with z0
   xC = x0 - x
   yC = y0 - y
   zC = z0 - z

   i = (yC * c) - (zC * b)
   j = (xC * c) - (zC * a)
   k = (xC * b) - (yC * a)

   d = math.sqrt(((i * i) + (j * j) + (k * k)) / ((a * a) + (b * b) + (c * c)))

   if d <= 0.08:
       return True
   else:
       return False


window = pygame.display.set_mode((wS, wS))

#n is the diameter of each circle
n = math.sqrt(wA/res)
#pPerR is the pixels per row/column
pPR = wS/n

while True:
   keys = pygame.key.get_pressed()
   window.fill(WHITE)

   for event in pygame.event.get():
       pygame.draw.circle(window, BLACK, (wS - 5, 5), 5)
   if keys[pygame.K_ESCAPE]:
       break

   if keys[pygame.K_a]:
       phiDirec+=2
   elif keys[pygame.K_d]:
       phiDirec-=2

   if keys[pygame.K_w]:
       cameraLocX = (math.cos(math.radians(phiDirec)) * math.sin(math.radians(thetaDirec)) * r * speedMult) + cameraLocX
       cameraLocY = (math.sin(math.radians(phiDirec)) * math.sin(math.radians(thetaDirec)) * r * speedMult) + cameraLocY
       #cameraLocZ = (math.cos(math.radians(thetaDirec)) * r * speedMult) + cameraLocZ
       
   elif keys[pygame.K_s]:
       cameraLocX = (math.cos(math.radians(phiDirec)) * math.sin(math.radians(thetaDirec)) * -r * speedMult) + cameraLocX
       cameraLocY = (math.sin(math.radians(phiDirec)) * math.sin(math.radians(thetaDirec)) * -r * speedMult) + cameraLocY
       #cameraLocZ = (math.cos(math.radians(thetaDirec)) * -r * speedMult) + cameraLocZ
       

   #I want the middle to be pointing straight so I added half the field of view to the starting phi angle
   curPHI = phiDirec + (fovPHI/2)
   #I want the middle to be pointing straight so I subtracted half the theta field of view to make it start facing up
   curTHETA = thetaDirec - (fovTHETA/2)

   curX = n/2
   curY = n/2




   for f in range(int(pPR)):

       for l in range(int(pPR)):
           for u in range(0, len(pX)):
               if checkPoint(cameraLocX, cameraLocY, cameraLocZ, pX[u], pY[u], pZ[u], r, curPHI, curTHETA):
                   pygame.draw.circle(window, pColor[u], (int(curX), int(curY)), int(n/2))

           curPHI -= dppPHI
           curX += n
       curX = n/2
       curPHI = phiDirec + (fovPHI/2)
       curY += n
       curTHETA += dppTHETA







   pygame.display.update()

pygame.quit()
quit()