-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraHandler.py
More file actions
121 lines (86 loc) · 3.26 KB
/
Copy pathCameraHandler.py
File metadata and controls
121 lines (86 loc) · 3.26 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
"""
Handler for the Camera Class
Class
-----
Camera - This Class handles the Client's Camera
"""
from pygame import key, K_w, K_s, K_a, K_d, K_q, K_e
import UIHandler
from BlockHandler import HighlightBlock
from degreesMath import cos, sin
from math import radians
from pygame.mouse import set_pos, get_pos
from glm import vec3, vec2, mat4, rotate, translate, normalize, length
class Camera(object):
def __init__(self, shader=None, uiPlainShader=None, startPos: vec3=None, displayCentre: vec2=None, **kwargs):
super(Camera, self).__init__(**kwargs)
self.headPos = startPos
self.lookRelPos = vec3()
self.upVector = vec3(0, 1, 0)
self.rotX = 0
self.rotY = 0
self.lookAtMatrix = self.getLookAtMatrix()
self.speed = 5
self.sensitivity = 0.1
self.highlightBlock = HighlightBlock(shader)
self.crosshair = UIHandler.Crosshair(uiPlainShader, displayCentre*2)
self.displayCentre = displayCentre
self.displaySize = (displayCentre[0] * 2, displayCentre[1] * 2)
def rotateCamera(self):
currentMousePosition = vec2(*get_pos())
set_pos(self.displayCentre.to_tuple())
deltaVector = (currentMousePosition - self.displayCentre) * self.sensitivity
self.rotY -= deltaVector.y
self.rotX += deltaVector.x
if self.rotX >= 360:
self.rotX -= 360
if self.rotY < -85:
self.rotY = -85
elif self.rotY > 80:
self.rotY = 80
self.lookRelPos = vec3(
cos(self.rotY) * sin(self.rotX),
sin(self.rotY),
-cos(self.rotY) * cos(self.rotX),
)
#print("LookPos", self.lookRelPos, length(self.lookRelPos))
#print(self.lookRelPos)
self.lookAtMatrix = self.getLookAtMatrix()
def moveCamera(self, dt):
keysPressed = key.get_pressed()
directionalXVector = vec3(
sin(-self.rotX),
0,
cos(-self.rotX)
)
directionalZVector = vec3(
sin(-self.rotX - 90),
0,
cos(-self.rotX - 90)
)
frameDistance = self.speed * dt
directionalXVector = normalize(directionalXVector) * frameDistance
directionalZVector = normalize(directionalZVector) * frameDistance
if keysPressed[K_w]:
self.headPos -= directionalXVector
if keysPressed[K_s]:
self.headPos += directionalXVector
if keysPressed[K_a]:
self.headPos += directionalZVector
if keysPressed[K_d]:
self.headPos -= directionalZVector
if keysPressed[K_q]:
self.headPos.y -= frameDistance
if keysPressed[K_e]:
self.headPos.y += frameDistance
def getLookAtMatrix(self):
viewMatrix = mat4()
yRotationMat = rotate(viewMatrix, radians(-self.rotY), (1, 0, 0))
xRotationMat = rotate(viewMatrix, radians(self.rotX), (0, 1, 0))
translationMat = translate(viewMatrix, self.headPos * -1)
return yRotationMat * xRotationMat * translationMat
#return lookAt(self.headPos, self.headPos+self.lookRelPos, self.upVector)
def draw(self):
self.highlightBlock.draw()
def drawCrosshair(self):
self.crosshair.draw()