-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (121 loc) · 5.35 KB
/
Copy pathmain.py
File metadata and controls
148 lines (121 loc) · 5.35 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
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
import pygame
import geometria
import numpy as np
import math
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
LIGHT_BLUE = (0, 100, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (50, 50, 50)
BLACK = (0,0,0)
CIRCLE_NUMBER_SEGMENTS = 16
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gimbal Lock Demo")
deltaYaw = deltaPitch = deltaRoll = 0.0
deltaAngulo = 0.1
clock = pygame.time.Clock()
# Parâmetros da câmera esférica
cam_dist = 10.0
cam_theta = 0 # azimute (em radianos)
cam_phi = 0 # elevação (em radianos)
mouse_sens = 0.005
running = True
circuloVermelho = geometria.Objeto.PoligonoRegular(CIRCLE_NUMBER_SEGMENTS, 1.6)
eixoVermelho1 = geometria.Objeto.Segmento(geometria.Ponto(0, -1.6, 0), geometria.Ponto(0, -1.8, 0))
eixoVermelho2 = geometria.Objeto.Segmento(geometria.Ponto(0, 1.6, 0), geometria.Ponto(0, 1.8, 0))
circuloVermelho = circuloVermelho + eixoVermelho1 + eixoVermelho2
circuloVerde = geometria.Objeto.PoligonoRegular(CIRCLE_NUMBER_SEGMENTS, 1.4)
eixoVerde1 = geometria.Objeto.Segmento(geometria.Ponto(-1.4, 0, 0), geometria.Ponto(-1.6, 0, 0))
eixoVerde2 = geometria.Objeto.Segmento(geometria.Ponto(1.4, 0, 0), geometria.Ponto(1.6, 0, 0))
circuloVerde = circuloVerde + eixoVerde1 + eixoVerde2
matriz_verde = circuloVerde.Rotacionar(0, 0, math.pi / 2)
circuloAzul = geometria.Objeto.PoligonoRegular(CIRCLE_NUMBER_SEGMENTS, 1.2)
eixoAzul1 = geometria.Objeto.Segmento(geometria.Ponto(0, -1.2, 0), geometria.Ponto(0, -1.4, 0))
eixoAzul2 = geometria.Objeto.Segmento(geometria.Ponto(0, 1.2, 0), geometria.Ponto(0, 1.4, 0))
circuloAzul = circuloAzul + eixoAzul1 + eixoAzul2
matriz_azul = circuloAzul.Rotacionar(0, math.pi / 2, 0, matriz_verde)
setaBranca = geometria.Objeto.Seta(1, math.pi / 8)
setaBranca.Rotacionar(math.pi / 2, 0, 0, matriz_azul)
'''
SemiEixoXPositivo = geometria.SemiEixo(geometria.Ponto(0,0,0), geometria.Ponto(1, 0, 0))
SemiEixoYPositivo = geometria.SemiEixo(geometria.Ponto(0,0,0), geometria.Ponto(0, 1, 0))
SemiEixoZPositivo = geometria.SemiEixo(geometria.Ponto(0,0,0), geometria.Ponto(0, 0, 1))
'''
Camera = geometria.Camera(
(WIDTH, HEIGHT),
geometria.Ponto(0, 0, cam_dist),
geometria.Ponto(0, 0, -1),
90
)
while running:
dt = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_a):
deltaYaw += deltaAngulo
if (event.key == pygame.K_d):
deltaYaw -= deltaAngulo
if (event.key == pygame.K_w):
deltaPitch += deltaAngulo
if (event.key == pygame.K_s):
deltaPitch -= deltaAngulo
if (event.key == pygame.K_q):
deltaRoll += deltaAngulo
if (event.key == pygame.K_e):
deltaRoll -= deltaAngulo
elif event.type == pygame.KEYUP:
if (event.key == pygame.K_a):
deltaYaw -= deltaAngulo
if (event.key == pygame.K_d):
deltaYaw += deltaAngulo
if (event.key == pygame.K_w):
deltaPitch -= deltaAngulo
if (event.key == pygame.K_s):
deltaPitch += deltaAngulo
if (event.key == pygame.K_q):
deltaRoll -= deltaAngulo
if (event.key == pygame.K_e):
deltaRoll += deltaAngulo
# Controle do mouse
'''
mouse_buttons = pygame.mouse.get_pressed()
if mouse_buttons[0]:
mouse_dx, mouse_dy = pygame.mouse.get_rel()
cam_theta -= mouse_dx * mouse_sens
cam_phi -= mouse_dy * mouse_sens
cam_phi = max(0.01, min(math.pi - 0.01, cam_phi)) # limita o ângulo para não inverter
else:
pygame.mouse.get_rel()
'''
# Converte coordenadas esféricas para cartesianas
cam_x = cam_dist * math.sin(cam_phi) * math.cos(cam_theta)
cam_y = cam_dist * math.sin(cam_phi) * math.sin(cam_theta)
cam_z = cam_dist * math.cos(cam_phi)
Camera.posicao = geometria.Ponto(cam_x, cam_y, cam_z)
Camera.direcao = geometria.Ponto(0, 0, 0) - Camera.posicao # sempre aponta para o centro
matriz_vermelho = circuloVermelho.Rotacionar(0, deltaPitch, 0)
matriz_verde = circuloVerde.Rotacionar(0, 0, deltaRoll, matriz_vermelho)
matriz_azul = circuloAzul.Rotacionar(0, deltaYaw, 0, matriz_verde)
setaBranca.Rotacionar(0, 0, 0, matriz_azul)
screen.fill(BLACK)
'''
SemiEixoXPositivo.Desenhar(Camera, screen, MAGENTA)
SemiEixoYPositivo.Desenhar(Camera, screen, CYAN)
SemiEixoZPositivo.Desenhar(Camera, screen, YELLOW)
'''
circuloVermelho.Desenhar(Camera, screen, RED)
circuloVerde.Desenhar(Camera, screen, GREEN)
circuloAzul.Desenhar(Camera, screen, LIGHT_BLUE)
setaBranca.Desenhar(Camera, screen, WHITE)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()