-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathray.py
More file actions
245 lines (213 loc) · 6.2 KB
/
ray.py
File metadata and controls
245 lines (213 loc) · 6.2 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import tkinter
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from math import radians, sin, cos, tan, sqrt
'''
Raycasting engine, written in python using OpenGl, GLU, and GLUT
'''
# Accesory function
def FixAng(angle):
a = angle
if(angle>359):
a -= 360
elif(angle<0):
a += 360
return a
# The map that player exists in
world = [
1,1,1,1,1,1,1,1,
1,1,0,1,0,0,0,1,
1,0,0,0,0,1,0,1,
1,1,1,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1
]
# Map variables
mapS = 64
mapX = 8
mapY = 8
# Draws 2D map
def drawMap2d():
for y in range(0, mapY):
for x in range(0 , mapX):
if(world[y*mapY+x] == 1):
glColor(1, 1, 1)
else:
glColor(0, 0, 0)
xo, yo = x*mapS, y*mapS
glBegin(GL_QUADS)
glVertex(xo+1, yo+1)
glVertex(xo+1, mapS+yo+1)
glVertex(mapS+xo-1, mapS+yo-1)
glVertex(mapS+xo-1, yo+1)
glEnd()
# Player position variables
px, py, pa, pdx, pdy = 0,0,0,0,0
# Draws 2D player
def drawPlayer2d(px, py, pa, pdx, pdy):
glColor(1, 1, 0)
glPointSize(8)
glLineWidth(4)
glBegin(GL_POINTS)
glVertex(px, py)
glEnd()
glBegin(GL_LINES)
glVertex(px, py)
glVertex(px+20*pdx, py+20*pdy)
glEnd()
# Handles keyboard input callbacks
def buttons(key, x, y):
global px, py, pa, pdx, pdy
if(ord(key) == ord('w')):
px += pdx*5
py += pdy*5
elif(ord(key) == ord('a')):
pa += 5
pa = FixAng(pa)
pdx=cos(radians(pa))
pdy=-sin(radians(pa))
elif(ord(key) == ord('d')):
pa -= 5
pa = FixAng(pa)
pdx=cos(radians(pa))
pdy=-sin(radians(pa))
elif(ord(key) == ord('s')):
px -= pdx*5
py -= pdy*5
elif(ord(key) == ord('q')):
px = x
py = y
glutPostRedisplay()
# Drawing all the rays
def drawRays2d():
# Draws sky
glColor3f(0,1,1)
glBegin(GL_QUADS)
glVertex(526, 0)
glVertex(1006, 0)
glVertex(1006,160)
glVertex(526,160)
glEnd()
#Draws floor
glColor3f(0,0,1)
glBegin(GL_QUADS)
glVertex2i(526,160)
glVertex2i(1006,160)
glVertex2i(1006,320)
glVertex2i(526,320)
glEnd()
#ra is the ray angle
ra = FixAng(pa + 30)
for r in range(1, 60): # We are drawing total 60 rays, for a 60 degree field of view
# Checking vertical wall intercept
dof, side, disV = 0, 0, 10000
Tan = tan(radians(ra))
if(cos(radians(ra)) > 0.001): # Looking leftwards
rx = ((int(px) >> 6) << 6) + 64 # First x-intercept
ry = (px - rx)*Tan+py
xo = 64
yo = -xo * Tan
elif(cos(radians(ra)) < -0.001): # Looking rightwards
rx = ((int(px) >> 6) << 6) - 0.001
ry = (px - rx)*Tan+py
xo = -64
yo = -xo * Tan
else: # No vertical hit
rx=px
ry=py
dof=8
while(dof < 8):
mx = int(rx) >> 6
my = int(ry) >> 6
mp = my*mapX + mx
if(mp > 0 and mp < mapX*mapY and world[mp] == 1): # Is the intercept a wall?
dof = 8
# disV = cos(radians(ra))*(rx-px)-sin(radians(ra))*(ry-py)
disV = sqrt((px-rx)**2 + (py-ry)**2) # Finding vertical distance
else: # Else, check next intercept
rx += xo
ry += yo
dof += 1
vx = rx
vy = ry
# Checking Horizontal wall intercept
dof, disH, Tan = 0, 10000, 1/Tan
if(sin(radians(ra)) > 0.001): # Looking up
ry = ((int(py) >> 6) << 6) - 0.0001
rx = (py-ry)*Tan+px
yo = -64
xo = -yo*Tan
elif(sin(radians(ra)) < -0.001): # Looking down
ry = ((int(py) >> 6) << 6) + 64
rx = (py-ry)*Tan+px
yo = 64
xo = -yo*Tan
while(dof < 8):
mx = int(rx) >> 6
my = int(ry) >> 6
mp = my*mapX + mx
if(mp > 0 and mp < mapX*mapY and world[mp] == 1): # Is intercept a wall?
dof = 8
# disH = cos(radians(ra)) * (rx-px) - sin(radians(ra))*(ry-py)
disH = sqrt((px-rx)**2 + (py-ry)**2)
else: # Now check next intercept
rx += xo
ry += yo
dof += 1
hx, hy = rx, ry
if(disV < disH): # If a Vertical wall is hit first
# print("yes")
rx, ry = vx, vy
disH = disV
else:
# print("no")
rx, ry = hx, hy
# Drawing 2D rays
glColor(0, 0.6, 0)
glLineWidth(2)
glBegin(GL_LINES)
glVertex(px, py)
glVertex(rx, ry)
glEnd()
# Drawing 3D scene
ca = FixAng(pa - ra) # This is to correct for Fisheye effect, which looks unnatural
disH = disH*cos(radians(ca))
lineH = mapS*320/disH
if(lineH > 320):
lineH = 320
lineOff = 160-(lineH // 2)
glLineWidth(9)
glBegin(GL_LINES)
glVertex(r*8+530,lineOff)
glVertex(r*8+530,lineOff+lineH)
glEnd()
# Looping to next ray
ra = FixAng(ra -1)
# Initializing basic window parameters
def init():
global px, py, pa, pdx, pdy
glClearColor(0.3,0.3,0.3,0)
gluOrtho2D(0,1024,510,0)
px=150; py=400; pa=90.1
pdx=cos(radians(pa))
pdy=-sin(radians(pa))
# Display callback function
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
drawMap2d()
drawPlayer2d(px, py, pa, pdx, pdy)
drawRays2d()
glutSwapBuffers()
# Defining all callbacks and windows.
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(1024, 510)
glutCreateWindow("pyopengl raycater")
init()
glutDisplayFunc(display)
glutIdleFunc(display)
glutKeyboardFunc(buttons)
glutMainLoop()