-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcamera.py
68 lines (42 loc) · 1.93 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
from pyrr import Vector3, vector, vector3, matrix44
from math import sin, cos, radians
class Camera:
def __init__(self):
self.camera_pos = Vector3([0.0, 4.0, 3.0])
self.camera_front = Vector3([0.0, 0.0, -1.0])
self.camera_up = Vector3([0.0, 1.0, 0.0])
self.camera_right = Vector3([1.0, 0.0, 0.0])
self.mouse_sensitivity = 0.25
self.jaw = -90
self.pitch = 0
def get_view_matrix(self):
return matrix44.create_look_at(self.camera_pos, self.camera_pos + self.camera_front, self.camera_up)
def process_mouse_movement(self, xoffset, yoffset, constrain_pitch=True):
xoffset *= self.mouse_sensitivity
yoffset *= self.mouse_sensitivity
self.jaw += xoffset
self.pitch += yoffset
if constrain_pitch:
if self.pitch > 45:
self.pitch = 45
if self.pitch < -45:
self.pitch = -45
self.update_camera_vectors()
def update_camera_vectors(self):
front = Vector3([0.0, 0.0, 0.0])
front.x = cos(radians(self.jaw)) * cos(radians(self.pitch))
front.y = sin(radians(self.pitch))
front.z = sin(radians(self.jaw)) * cos(radians(self.pitch))
self.camera_front = vector.normalise(front)
self.camera_right = vector.normalise(vector3.cross(self.camera_front, Vector3([0.0, 1.0, 0.0])))
self.camera_up = vector.normalise(vector3.cross(self.camera_right, self.camera_front))
# Camera method for the WASD movement
def process_keyboard(self, direction, velocity):
if direction == "FORWARD":
self.camera_pos += self.camera_front * velocity
if direction == "BACKWARD":
self.camera_pos -= self.camera_front * velocity
if direction == "LEFT":
self.camera_pos -= self.camera_right * velocity
if direction == "RIGHT":
self.camera_pos += self.camera_right * velocity