/
camera.py
271 lines (198 loc) · 7.04 KB
/
camera.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Handler for the Camera Class
Class
-----
Camera - This Class handles the Client's Camera
"""
import pygame.key as key
from OpenGL.GL import *
from pygame import K_w, K_a, K_s, K_d, K_q, K_e
from pygame.mouse import set_pos, get_pos
from degreesMath import *
from vector import Vector3, Vector2
class Camera:
"""
This Class handles the player Camera
Parameters
----------
startPos : Vector3
Start Position of the Camera
displayCentre : tuple
Centre of the window size
Attributes
----------
currentCameraPosition : Vector3
A Vector3 of the current camera position
lookVector : Vector3
A Normalised Vector3 of the camera looking direction
raycastUpdateLength : float
The Accuracy of the highlight-block raycasting
maxDist : int/float
Block Edit Range from the Camera Position
deltaVector : Vector2
Change in Position of the Mouse
lastMousePosition : Vector3
A Vector2 of the mouse's current position on the window
distance : flaot
The distance moved per frame - sensitive to change
sensitivity : float
Mouse Sensitivity when moving the mouse
upDownAngle : int
The Up-Down angle of the Camera - Foward:0, Up:90, Dowm:-90
leftRightAndle : int
The Left-Right angle of the Camera - Forward:0, Left: -90, Right: 90
displayCentre: tuple
Centre of the window size
displaySize : tuple
Double the displayCentre - Size of window
updatePos : Vector2
Vector2 used for postion of the crosshair
"""
def __init__(self, startPos: Vector3, displayCentre: tuple):
self.currentCameraPosition = startPos
self.lookVector = Vector3(0, 0, 0)
self.deltaVector = Vector2(0, 0)
self.distance = 0.005 # 0.005
self.sensitivity = 0.1
self.upDownAngle = 0
self.leftRightAngle = 0
self.displayCentre = displayCentre
self.displaySize = (displayCentre[0] * 2, displayCentre[1] * 2)
self.updatePos = Vector2(*self.displayCentre)
def updateLookVector(self):
"""
Updates the Look Vector of the Camera and stores it in self.lookVector
Returns
-------
None
"""
xzLength = cos(self.upDownAngle)
trigAddVector = Vector3(sin(-self.leftRightAngle) * xzLength,
sin(self.upDownAngle),
cos(-self.leftRightAngle) * xzLength)
self.lookVector = trigAddVector
def turnCamera(self, firstStage: bool):
"""
Handles the Turning of the Camera
Parameters
----------
firstStage : bool
A bool which represents whether the Y or X part os the camera should be turned.
Returns
-------
None
"""
if firstStage:
currentMousePosition = Vector2(*get_pos())
set_pos(self.displayCentre)
self.deltaVector = (currentMousePosition - self.displayCentre) * self.sensitivity
self.upDownAngle += self.deltaVector.Y
self.leftRightAngle += self.deltaVector.X
if self.upDownAngle < -80:
self.upDownAngle = -80
elif self.upDownAngle > 80:
self.upDownAngle = 80
self.updateLookVector()
glRotatef(self.upDownAngle, 1, 0, 0)
else:
glRotatef(self.deltaVector.X, 0.0, 1.0, 0.0)
def moveCamera(self, dt: float, moveConstraints: dict):
"""
Handles the Turning of the Camera
Parameters
----------
dt : float
Time since the last frame in ms
Returns
-------
None
"""
directionalXVector = Vector3(
sin(-self.leftRightAngle),
0,
cos(-self.leftRightAngle)
)
directionalZVector = Vector3(
sin(-self.leftRightAngle - 90),
0,
cos(-self.leftRightAngle - 90)
)
frameDistance = self.distance * dt
directionalXVector = directionalXVector.unit * frameDistance
directionalZVector = directionalZVector.unit * frameDistance
moveVector = Vector3(0, 0, 0)
keysPressed = key.get_pressed()
if keysPressed[K_w] and not moveConstraints["w"]:
self.currentCameraPosition -= directionalXVector
moveVector.Z += frameDistance
if keysPressed[K_s] and not moveConstraints["s"]:
self.currentCameraPosition += directionalXVector
moveVector.Z -= frameDistance
if keysPressed[K_a] and not moveConstraints["a"]:
self.currentCameraPosition += directionalZVector
moveVector.X += frameDistance
if keysPressed[K_d] and not moveConstraints["d"]:
self.currentCameraPosition -= directionalZVector
moveVector.X -= frameDistance
if keysPressed[K_q] and not moveConstraints["q"]:
self.currentCameraPosition.Y -= frameDistance
moveVector.Y += frameDistance
if keysPressed[K_e] and not moveConstraints["e"]:
self.currentCameraPosition.Y += frameDistance
moveVector.Y -= frameDistance
glTranslatef(*moveVector.tuple)
def move(self, dt, viewMatrix, moveConstraints):
"""
Controls the Movement of the Camera
Parameters
----------
dt : float
Time passed since last frame
viewMatrix : OpenGL.arrays.ctypesarrays.c_float_Array_4_Array_4
Used to transform from world-space into view-space
Returns
-------
OpenGL.arrays.ctypesarrays.c_float_Array_4_Array_4
"""
glLoadIdentity()
self.turnCamera(True)
glPushMatrix()
glLoadIdentity()
self.moveCamera(dt, moveConstraints)
self.turnCamera(False)
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
return viewMatrix
def drawCrosshair(self):
"""
Draws the Crosshair in the Centre of the Screen
Returns
-------
None
"""
def Crosshair(x, y, w):
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_LINES)
glVertex2f(x - w, y)
glVertex2f(x + w, y)
glVertex2f(x, y - w)
glVertex2f(x, y + w)
glEnd()
# https://stackoverflow.com/questions/54084020/how-to-display-a-2d-shape-over-opengl-with-python-and-pygame
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0.0, self.displaySize[0], 0.0, self.displaySize[1], -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
Crosshair(self.updatePos.X, self.updatePos.Y, 20)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()