Pygame 鼠标位置不够精确,无法旋转 3D 场景
我需要使用鼠标位置旋转 3D 场景或角色:例如,如果我将鼠标移动到右侧,我想向右转/角色应该向右转。
我见过很多游戏将鼠标放在屏幕中央,然后获取其相对于前一帧的位置,相应地更新游戏并将鼠标移回中央。
我尝试复制这一点,但遇到了 pygame 无法可靠旋转的问题:帧速率较高时,我往往会旋转得较慢。在 2D 游戏中旋转角色时这不是问题,但如果有更多对象需要绘制,3D 场景的旋转方式会有所不同,这就需要考虑了。
我使用这个简单的代码来确保问题实际上来自于此:
import pygame
from pygame.locals import *
pygame.init()
WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
font = pygame.font.SysFont('consolas', 20)
clock = pygame.time.Clock()
angle = 0
time_passed = 0
pygame.mouse.set_pos((WIDTH//2, HEIGHT//2))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
# I did not use the MOUSEMOTION event because it used to glitch a bit
x, y = pygame.mouse.get_pos()
angle += (WIDTH//2 - x) * 360 / WIDTH # 360° every WIDTH pixels
pygame.mouse.set_pos((WIDTH//2, HEIGHT//2))
screen.fill(0)
text = font.render('Angle: %d°' %angle, True, (255, 255, 255))
w, h = text.get_size()
screen.blit(text, (WIDTH//2 - w//2, HEIGHT//2 - h//2))
pygame.display.flip()
time_passed = clock.tick(60)/1000
我认为鼠标或操作系统会根据鼠标速度调整行进距离,因此我以类似且相对恒定的速度进行了以下测试:
假设是 5 厘米例如,帧速率限制为 60 FPS 时,即使使用具有高 DPI 的同一鼠标以相同的速度移动鼠标,也不会产生与 120 FPS 相同的最终角度。
例如,我是否需要实现一个刻度系统,每隔几帧更新一次位置?我认为用较慢的帧速率无法解决问题,而且运动的响应速度可能不会像我想要的那样。
或者也许有 pygame.mouse.get_pos()
/ event.pos
的替代方案,从鼠标获取原始的、更精确的位置?
我将不胜感激任何帮助。
I need to rotate a 3D scene or a character using the mouse position: for example, if I move the mouse to the right, I want to turn to the right / the character should turn right.
I've seen lots of games place the mouse in the center of the screen, then get its position relatively to the previous frame, update the game accordingly and move the mouse back in the center.
I tried to replicate this but I ran into the issue that pygame does not rotate reliably: with higher framerates, I tend to get a slower rotation. This is not a problem when rotating a character in a 2D game, but this is concerning when a 3D scene rotates differently if there are more objects to draw.
I used this simple code to make sure the problem actually came from this:
import pygame
from pygame.locals import *
pygame.init()
WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
font = pygame.font.SysFont('consolas', 20)
clock = pygame.time.Clock()
angle = 0
time_passed = 0
pygame.mouse.set_pos((WIDTH//2, HEIGHT//2))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
# I did not use the MOUSEMOTION event because it used to glitch a bit
x, y = pygame.mouse.get_pos()
angle += (WIDTH//2 - x) * 360 / WIDTH # 360° every WIDTH pixels
pygame.mouse.set_pos((WIDTH//2, HEIGHT//2))
screen.fill(0)
text = font.render('Angle: %d°' %angle, True, (255, 255, 255))
w, h = text.get_size()
screen.blit(text, (WIDTH//2 - w//2, HEIGHT//2 - h//2))
pygame.display.flip()
time_passed = clock.tick(60)/1000
I think either the mouse or the OS tweak the travelled distance according to the mouse speed, so I did the following tests at similar and relatively constant speed:
Going say 5 centimeters with the framerate limited to 60 FPS did not result in the same final angle as with 120 FPS for example, even when moving the mouse at the same speed, with the same mouse with high DPI.
Do I need to implement a tick system for example, where I only update the position every few frames? I think the problem wouldn't be solved with slower framerates then, and maybe the movement would not be as responsive as I want.
Or perhaps there is an alternative to pygame.mouse.get_pos()
/ event.pos
, getting the raw, more precise position from the mouse?
I would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 pygame.mouse.get_pos() 获取鼠标的位置,并使用 pygame.mouse.set_pos() 重置位置。如果
pygame.mouse.get_pos()
和pygame.mouse.set_pos()
之间存在事件怎么办?另外 pygame.mouse.get_pos() 只告诉你鼠标光标的当前位置,但它不会告诉你鼠标是如何到达那里的。我建议使用
MOUSEMOTION
事件。事件队列为您提供鼠标的所有移动。可以使用rel
属性查询自上次移动以来鼠标指针的相对移动:最小示例
You get the position of the mouse with
pygame.mouse.get_pos()
and you reset the position withpygame.mouse.set_pos()
. What if there was an event betweenpygame.mouse.get_pos()
andpygame.mouse.set_pos()
? Alsopygame.mouse.get_pos()
only tells you the current position of the mouse curosr, but it does not tell you how the mouse got there.I suggest to use the
MOUSEMOTION
event. The event queue gives you all movements of the mouse. The relative movement of the mouse pointer since the last movement can be queried with therel
attribute:Minimal example