如何让 Pygame 中的相机移动更加自然?

问题描述 投票:0回答:1

我正在努力让相机的移动感觉更自然。目前,相机的移动是僵化的,不能平滑地跟随用户输入或动画。

这是我的代码的简化版本:

import pygame
from pygame.locals import *
running = True
mouse_down = False
while running:
    for event in pygame.event.get((QUIT,MOUSEBUTTONUP,MOUSEBUTTONDOWN)):
        if event.type == QUIT:
            running = False
        elif event.type == MOUSEBUTTONUP:
            mouse_down = False
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = True
            xcoor = pygame.mouse.get_pos()
    if mouse_down and (pygame.mouse.get_pos()[0] - xcoor[0] != 0 or pygame.mouse.get_pos()[1] - xcoor[1]):
        condition = True
        camera[1].z += (pygame.mouse.get_pos()[0] - xcoor[0]) * 0.1
        camera[1].y -= (pygame.mouse.get_pos()[1] - xcoor[1]) * 0.1
        xcoor = pygame.mouse.get_pos()
    else:
        condition = False
    keys = pygame.key.get_pressed()
    if keys[K_LEFT]:
        camera[0].x -= 0.1
        condition = True
    if keys[K_RIGHT]:
        camera[0].x += 0.1
        condition = True
    if keys[K_UP]:
        camera[0].z -= 0.1
        condition = True
    if keys[K_DOWN]:
        camera[0].z += 0.1
        condition = True
    if keys[K_w]:
        camera[0].y += 0.1
        condition = True
    if keys[K_s]:
        camera[0].y -= 0.1
        condition = True

我使用一组向量和角度初始化相机位置和方向。然而,当我尝试移动或旋转相机时,动作很突然而且一点也不流畅。

我可以使用哪些技术或方法来提高 Pygame 光线追踪器中相机移动的流畅性和自然度?是否有特定的算法或库可以帮助解决这个问题?

任何见解或建议将不胜感激。预先感谢!

python pygame
1个回答
-1
投票

我找到答案了

虽然它通常不是 pygame 代码(通常是数学的)

    if mouse_down and (pygame.mouse.get_pos()[0] - xcoor[0] != 0 or pygame.mouse.get_pos()[1] - xcoor[1]):
        condition = True
        x -= (pygame.mouse.get_pos()[0] - xcoor[0]) * 3
        y -= (pygame.mouse.get_pos()[1] - xcoor[1]) * 3
        xcoor = pygame.mouse.get_pos()
        camera[1].x = camera[0].x + sin(radians(x)) * cos(radians(y))
        camera[1].y = camera[0].y + sin(radians(y))
        camera[1].z = camera[0].z + cos(radians(x)) * cos(radians(y))
© www.soinside.com 2019 - 2024. All rights reserved.