在 pygame 中计算玩家到光标的角度时遇到问题
Trouble with working out the angle of from player to cursor in pygame
所以我正在尝试制作小行星游戏,但在计算航天器应该观察的角度时遇到了问题。我知道三角学的基础知识,但是随着 y 轴的翻转,我得到了有点困惑。我已经制作了一个问题模型,因此您不必查看整个代码。
import pygame
import sys
import math
DIMENSIONS = (600, 600)
window = pygame.display.set_mode(DIMENSIONS, pygame.RESIZABLE)
window.fill((20, 20, 20))
pygame.display.set_caption("Pygame template")
clock = pygame.time.Clock()
player_pos = pygame.Vector2(275,275)
player = pygame.Rect(player_pos.xy, (50,50))
looking_vector = pygame.Vector2(1,1)
while True:
window.fill((20,20,20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
player = pygame.Rect(player_pos.xy, (50,50))
# Relative position of mouse
mouse_pos = pygame.mouse.get_pos()
delta = mouse_pos - player_pos
# Calculate the angle
angle_to_mouse = math.atan2(delta.y, delta.x)
looking_vector.xy = (100*math.cos(angle_to_mouse), 100*math.sin(angle_to_mouse))
# Player rect
pygame.draw.rect(window, (50, 50, 255), player)
# Line to mouse
pygame.draw.line(window, (255,50,50), player_pos + (25,25), pygame.mouse.get_pos())
# Line in direction to looking_vector
pygame.draw.line(window, (50,255,50), player_pos + (25,25), player_pos + looking_vector)
pygame.display.update()
clock.tick(60)
正如您在红线上方的图像中看到的那样 - 它只是矩形中心和鼠标位置之间的一条线 - 完全没问题,但当涉及到绿线时它有点偏离。
这几乎就是我在我的小行星游戏中获得的效果,只是如果玩家靠近轴,效果会很好。然而,如果玩家远离其中任何一个,效果会放大,并且观察向量完全在错误的方向上。
您的算法运行良好。但是,您需要使用播放器的中心而不是播放器的左上角来计算角度:
player = pygame.Rect(player_pos.xy, (50,50))
player_center = pygame.Vector2(player.center)
# Relative position of mouse
mouse_pos = pygame.mouse.get_pos()
delta = mouse_pos - player_center
# Calculate the angle
angle_to_mouse = math.atan2(delta.y, delta.x)
looking_vector.xy = (100*math.cos(angle_to_mouse), 100*math.sin(angle_to_mouse))
# Player rect
pygame.draw.rect(window, (50, 50, 255), player)
# Line to mouse
pygame.draw.line(window, (255,50,50), player_center, pygame.mouse.get_pos())
# Line in direction to looking_vector
pygame.draw.line(window, (50,255,50), player_center, player_center + looking_vector)
所以我正在尝试制作小行星游戏,但在计算航天器应该观察的角度时遇到了问题。我知道三角学的基础知识,但是随着 y 轴的翻转,我得到了有点困惑。我已经制作了一个问题模型,因此您不必查看整个代码。
import pygame
import sys
import math
DIMENSIONS = (600, 600)
window = pygame.display.set_mode(DIMENSIONS, pygame.RESIZABLE)
window.fill((20, 20, 20))
pygame.display.set_caption("Pygame template")
clock = pygame.time.Clock()
player_pos = pygame.Vector2(275,275)
player = pygame.Rect(player_pos.xy, (50,50))
looking_vector = pygame.Vector2(1,1)
while True:
window.fill((20,20,20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
player = pygame.Rect(player_pos.xy, (50,50))
# Relative position of mouse
mouse_pos = pygame.mouse.get_pos()
delta = mouse_pos - player_pos
# Calculate the angle
angle_to_mouse = math.atan2(delta.y, delta.x)
looking_vector.xy = (100*math.cos(angle_to_mouse), 100*math.sin(angle_to_mouse))
# Player rect
pygame.draw.rect(window, (50, 50, 255), player)
# Line to mouse
pygame.draw.line(window, (255,50,50), player_pos + (25,25), pygame.mouse.get_pos())
# Line in direction to looking_vector
pygame.draw.line(window, (50,255,50), player_pos + (25,25), player_pos + looking_vector)
pygame.display.update()
clock.tick(60)
正如您在红线上方的图像中看到的那样 - 它只是矩形中心和鼠标位置之间的一条线 - 完全没问题,但当涉及到绿线时它有点偏离。
这几乎就是我在我的小行星游戏中获得的效果,只是如果玩家靠近轴,效果会很好。然而,如果玩家远离其中任何一个,效果会放大,并且观察向量完全在错误的方向上。
您的算法运行良好。但是,您需要使用播放器的中心而不是播放器的左上角来计算角度:
player = pygame.Rect(player_pos.xy, (50,50))
player_center = pygame.Vector2(player.center)
# Relative position of mouse
mouse_pos = pygame.mouse.get_pos()
delta = mouse_pos - player_center
# Calculate the angle
angle_to_mouse = math.atan2(delta.y, delta.x)
looking_vector.xy = (100*math.cos(angle_to_mouse), 100*math.sin(angle_to_mouse))
# Player rect
pygame.draw.rect(window, (50, 50, 255), player)
# Line to mouse
pygame.draw.line(window, (255,50,50), player_center, pygame.mouse.get_pos())
# Line in direction to looking_vector
pygame.draw.line(window, (50,255,50), player_center, player_center + looking_vector)