Pygame 鼠标位置不够精确,无法旋转 3D 场景

发布于 2025-01-16 09:27:18 字数 1518 浏览 1 评论 0原文

我需要使用鼠标位置旋转 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2025-01-23 09:27:18

您可以使用 pygame.mouse.get_pos() 获取鼠标的位置,并使用 pygame.mouse.set_pos() 重置位置。如果 pygame.mouse.get_pos()pygame.mouse.set_pos() 之间存在事件怎么办?另外 pygame.mouse.get_pos() 只告诉你鼠标光标的当前位置,但它不会告诉你鼠标是如何到达那里的。

我建议使用 MOUSEMOTION 事件。事件队列为您提供鼠标的所有移动。可以使用 rel 属性查询自上次移动以来鼠标指针的相对移动:

for event in pygame.event.get():
    if event.type == pygame.MOUSEMOTION:
        angle += event.rel[0] * 360 / screen.get_width()

最小示例

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
font = pygame.font.SysFont('consolas', 20)
clock = pygame.time.Clock()

angle = 0
text = font.render(f'Angle: {angle}°', True, (255, 255, 255))
pygame.mouse.set_pos(screen.get_rect().center)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            run = False
        if event.type == pygame.MOUSEMOTION:
            angle += event.rel[0] * 360 / screen.get_width()
            text = font.render(f'Angle: {angle}°', True, (255, 255, 255))
    
    pygame.mouse.set_pos(screen.get_rect().center)

    screen.fill(0)
    screen.blit(text, text.get_rect(center = screen.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

You get the position of the mouse with pygame.mouse.get_pos() and you reset the position with pygame.mouse.set_pos(). What if there was an event between pygame.mouse.get_pos() and pygame.mouse.set_pos()? Also pygame.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 the rel attribute:

for event in pygame.event.get():
    if event.type == pygame.MOUSEMOTION:
        angle += event.rel[0] * 360 / screen.get_width()

Minimal example

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
font = pygame.font.SysFont('consolas', 20)
clock = pygame.time.Clock()

angle = 0
text = font.render(f'Angle: {angle}°', True, (255, 255, 255))
pygame.mouse.set_pos(screen.get_rect().center)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            run = False
        if event.type == pygame.MOUSEMOTION:
            angle += event.rel[0] * 360 / screen.get_width()
            text = font.render(f'Angle: {angle}°', True, (255, 255, 255))
    
    pygame.mouse.set_pos(screen.get_rect().center)

    screen.fill(0)
    screen.blit(text, text.get_rect(center = screen.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文