source: framspy/gui/visual/camera.py @ 1300

Last change on this file since 1300 was 1198, checked in by Maciej Komosinski, 23 months ago

Added simple Python GUI for Framsticks library/server

File size: 2.9 KB
RevLine 
[1198]1import glm
2from gui.visual.mouse import Mouse
3from gui.visual.player import Player
4
5class Camera:
6    def __init__(self, player: Player) -> None:
7        self.position = glm.vec3(0, 0, 1)
8        self.pith = 10
9        self.yaw = 0
10        self.roll = 0
11        self.angleAroundPlayer = 0
12        self.player = player
13        self.distanceFromPlayer = 10
14        self.mx = 0
15        self.my = 0
16        self.scale = 1
17
18    def setPosition(self, x, y):
19        self.player.position = glm.vec3(x, y, 0)
20
21    def setZoomScale(self, scale):
22        self.scale = scale
23
24    def setZoom(self, zoom):
25        self.distanceFromPlayer = 0.70140280561122 * zoom - 0.028056112224449 #values from linear interpolation
26
27    def move(self):
28        self.calculateZoom()
29        self.calculatePitchAndAngleAroundPLayer()
30        horizontalDistance = self.calculateHorizotalDistance()
31        verticalDistance = self.calculateVerticalDistance()
32        self.calculateCameraPosition(horizontalDistance, verticalDistance)
33
34    def calculateZoom(self):
35        zoomLevel = Mouse.getDWheel() * self.scale
36        self.distanceFromPlayer -= zoomLevel
37        if self.distanceFromPlayer < 0.1:
38            self.distanceFromPlayer = 0.1
39
40    def calculateCameraPosition(self, horizontalDistance, verticalDistance):
41        theta = self.player.rotY + self.angleAroundPlayer
42        offsetX = horizontalDistance * glm.sin(glm.radians(theta))
43        offsetY = horizontalDistance * glm.cos(glm.radians(theta))
44        self.position.x = self.player.position.x - offsetX
45        self.position.y = self.player.position.y + offsetY
46        self.position.z = self.player.position.z + verticalDistance
47        yaw = 180 - theta
48
49    def calculateHorizotalDistance(self):
50        return self.distanceFromPlayer * glm.cos(glm.radians(self.pith))
51
52    def calculateVerticalDistance(self):
53        return self.distanceFromPlayer * glm.sin(glm.radians(self.pith))
54
55    def calculatePitchAndAngleAroundPLayer(self):
56        if Mouse.isButtonDown(Mouse.MOUSE_LEFT):
57            dx = Mouse.getX() - self.mx
58            dy = Mouse.getY() - self.my
59            pitchChange = dy * 0.1
60            self.pith += pitchChange
61            if self.pith < -90:
62                self.pith = -89.99
63            if self.pith > 90:
64                self.pith = 89.99
65
66            angleChange = dx * 0.3
67            self.angleAroundPlayer -= angleChange
68        elif Mouse.isButtonDown(Mouse.MOUSE_RIGHT):
69            dx = Mouse.getX() - self.mx
70            dy = Mouse.getY() - self.my
71            theta = glm.radians(self.player.rotY + self.angleAroundPlayer)
72            scale = self.distanceFromPlayer * 10 / 250
73            self.player.position.x += (dx * glm.cos(theta) + dy * glm.sin(theta)) * 0.1 * scale
74            self.player.position.y += (dx * glm.sin(theta) - dy * glm.cos(theta)) * 0.1 * scale
75        self.mx = Mouse.getX()
76        self.my = Mouse.getY()
Note: See TracBrowser for help on using the repository browser.