반응형

파이게임 수학 벡터 함수 중 유용한 내용.

 

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
import pygame
import math
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
 
# pygame.math.Vector2.rotate
# rotates a vector by a given angle in degrees.
 
# pygame.math.Vector2.rotate_rad
# rotates a vector by a given angle in radians.
 
vec = pygame.math.Vector2(math.sqrt(2), 0)
print(vec.rotate(45))
# (sqrt(2), 0) 벡터를 반시계방향으로 45도 회전한 벡터.
# 벡터의 길이는 그대로 유지된다.
 
v1 = pygame.math.Vector2(300400)
 
# project(Vector2) -> Vector2
# Returns the projected vector. This is useful for collision
# detection in finding the components in a certain direction.
# (e.g. in direction of the wall).
v2 = v1.project(pygame.math.Vector2(10))
print(v2)
# (3, 4) 벡터가 X축(1, 0)에 투영되었을 때의 벡터.
 
v3 = v1.project(pygame.math.Vector2(01))
print(v3)
# (3, 4) 벡터가 Y축(0, 1)에 투영되었을 때의 벡터.
 
running = True
 
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
 
    screen.fill("black")
 
    pygame.draw.line(screen, "red", (00), v1, 20)
    pygame.draw.line(screen, "green", (00), v2, 20)
    pygame.draw.line(screen, "blue", (00), v3, 20)
 
    pygame.display.flip()
 
    clock.tick(60)
 
pygame.quit()
 

 

 

벡터의 회전(rotate)과 투영(project)

 

 

Project

 

Projection of a on b (a1), and rejection of a from b (a2).

 

When 90&deg; < &theta; &le; 180&deg;, a1 has an opposite direction with respect to b.

 

반응형
Posted by J-sean
: