-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3D_Render.py
232 lines (177 loc) · 6.36 KB
/
3D_Render.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
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
import pygame,math,random
from pygame import Vector2
Map = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,1,1,0,0,0,1],
[1,0,0,0,1,1,0,0,0,1],
[1,0,0,0,0,0,0,0,0,1],
[1,0,1,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1]
]
pygame.init()
screen = pygame.display.set_mode((800,400))
screen.set_alpha()
clock = pygame.time.Clock()
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
class Boundary:
def __init__(self,point1,point2):
self.a = Vector2(point1)
self.b = Vector2(point2)
def Show(self):
a = (self.a.x + 400,self.a.y )
b = (self.b.x + 400,self.b.y)
pygame.draw.line(screen,(255,255,255),a,b)
FOV = 40
class Particle:
def __init__(self):
self.pos = Vector2(200,200)
self.dir = Vector2(1,0)
self.CreateRays()
def CreateRays(self):
self.rays = []
angle = math.sqrt(self.dir[0]**2 + self.dir[1]**2)
for x in range(0,FOV*3,1):
self.rays.append(Ray(self.pos,angle + x/3))
def rotate(self,angle):
self.dir = self.dir.rotate(angle)
for ray in self.rays:
ray.Rotate(angle)
def Move(self,mag):
self.pos += self.rays[len(self.rays)//2].Dir * mag
for ray in self.rays:
ray.Move(self.pos)
def look(self,walls):
width = 400//len(self.rays)
dists = []
for x,ray in enumerate(self.rays):
closest = None
record = 400
for wall in walls:
pt = ray.cast(wall)
if pt:
d = Dist(self.pos, pt)
a = math.atan2(ray.Dir[0],ray.Dir[1]) - math.atan2(self.dir[0],self.dir[1])
#print(a)
#d *= math.cos(a)
if d < record:
record = d
closest = pt
if closest:
#pygame.draw.line(screen,(255,255,255),(int(self.pos.x),int(self.pos.y)),(int(closest.x),int(closest.y)))
height = translate(record,0,400,400,0)
col = translate(record**2,0,400**2,255,0)
pygame.draw.rect(screen,(col,col,col),(x*width,200 - (height/2),width,height))
def update(self,x,y):
self.pos.x = x
self.pos.y = y
def Show(self):
pygame.draw.circle(screen,(255,255,255),(int(self.pos.x + 400),int(self.pos.y)),5)
self.rays[0].Show()
self.rays[-1].Show()
#for x in range(len(self.rays)):
#self.rays[x].Show()
def Dist(pt1, pt2):
return math.sqrt(abs(pt1.x - pt2.x)**2 + abs(pt1.y - pt2.y)**2)
#return abs(pt1.x - pt2.x) + abs(pt1.y - pt2.y)
class Ray:
def __init__(self,pos,angle):
self.pos = pos
self.Dir = Vector2(1,0)
self.Dir = self.Dir.rotate(angle)
def Show(self):
pos = (self.pos.x + 400,self.pos.y)
pygame.draw.line(screen,(255,255,255),pos,pos + (self.Dir*50))
def Move(self,point):
self.pos = point
def lookAt(self,x,y):
self.Dir.x = x - self.pos.x
self.Dir.y = y - self.pos.y
self.Dir = self.Dir.normalize()
def Rotate(self,angle):
self.Dir = self.Dir.rotate(angle)
def cast(self,wall):
x1 = wall.a.x
y1 = wall.a.y
x2 = wall.b.x
y2 = wall.b.y
x3 = self.pos.x
y3 = self.pos.y
x4 = self.pos.x + self.Dir.x
y4 = self.pos.y + self.Dir.y
den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if den == 0:
return
t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den
u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den
if t > 0 and t < 1 and u > 0:
pt = Vector2()
pt.x = x1 + t * (x2 - x1)
pt.y = y1 + t * (y2 - y1)
return pt
return
walls = []
for x in range(len(Map)):
for y in range(len(Map[x])):
if (y == 0 or y == len(Map)-1) and (x == 0 or x == len(Map[x])-1):
continue
if Map[x][y]:
if x != len(Map) -1:
if Map[x+1][y] == False:
walls.append(Boundary((y*40,(x+1)*40),((y+1)*40,(x+1)*40)))
if x != 0:
if Map[x-1][y] == False:
walls.append(Boundary((y*40,x*40),((y+1)*40,x*40)))
if y != len(Map[x])-1:
if Map[x][y+1] == False:
walls.append(Boundary(((y+1)*40,x*40),((y+1)*40,(x+1)*40)))
if y != 0:
if Map[x][y-1] == False:
walls.append(Boundary((y*40,(x)*40),(y*40,(x+1)*40)))
#for x in range(5):
#walls.append(Boundary((random.randint(0,400),random.randint(0,400)),(random.randint(0,400),random.randint(0,400))))
#walls.append(Boundary((0,0),(0,400)))
#walls.append(Boundary((0,0),(400,0)))
#walls.append(Boundary((400,0),(400,400)))
#walls.append(Boundary((0,400),(400,400)))
particle = Particle()
key = ""
running = True
while running:
screen.fill((0,0,0))
mouseX,mouseY = pygame.mouse.get_pos()
#particle.update(mouseX,mouseY)
for wall in walls:
wall.Show()
particle.Show()
particle.look(walls)
p = False
clock.tick(40)
#print(clock.get_fps())
if key == "a":
particle.rotate(-4)
if key == "d":
particle.rotate(4)
if key == "w":
particle.Move(2)
if key == "s":
particle.Move(-2)
pygame.display.update()
for e in pygame.event.get():
if e.type == 12:
pygame.quit()
running = False
if e.type == 2:
key = e.unicode
if e.type == 3:
key = ""