pygame中的三维投影

2024-05-16 21:54:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个简单的立方体三维渲染。正如这段来自编码列车的视频:https://www.youtube.com/watch?v=p4Iz0XJY-Qk在第14分钟。我一度陷入困境。因为我对这一切都很陌生,我不确定是什么导致了我的问题。当我启动项目时,立方体会按照我的要求旋转,但会从屏幕向左移动,看起来像是在做一个圆

import pygame
import numpy as np
import os
import math

WHITE = (255,255,255)
width, height = 700, 700
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()

points = []
angle = 0

points.append(np.array([[300], [250], [1]]))
points.append(np.array([[300], [350], [1]]))
points.append(np.array([[400], [250], [1]]))
points.append(np.array([[400], [350], [1]]))

projectionMatrix = np.array([[1, 0, 0],
                             [0, 1, 0]])

while True:
    clock.tick(30)
    screen.fill((0,0,0))

    rotation = np.array([[math.cos(angle), -math.sin(angle)],
                         [math.sin(angle), math.cos(angle)]])

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                os._exit(1)

    for point in points:
        projected2d = np.dot(projectionMatrix, point)
        rotated = np.dot(rotation, projected2d)
        pygame.draw.circle(screen, WHITE, (int(rotated[0][0]), int(rotated[1][0])), 5)

    angle += 0.01
    pygame.display.update()

我真的很感激任何帮助,以解释为什么会发生这种情况,以及我如何修复它,使它只是旋转


Tags: importeventosnpmathwidtharrayscreen
1条回答
网友
1楼 · 发布于 2024-05-16 21:54:51

这段代码中没有bug。这些点围绕左上角(0,0)旋转。注意,在3D模式下,p5.js使用的坐标系与pygame不同

如果要围绕窗口中心旋转点,请在范围[-1,1](标准化设备空间:

points.append(np.matrix([ 0.5,  0.5, 1]))
points.append(np.matrix([ 0.5, -0.5, 1]))
points.append(np.matrix([-0.5,  0.5, 1]))
points.append(np.matrix([-0.5, -0.5, 1]))

定义从[-1,1]到窗口空间的投影矩阵:

projectionMatrix = np.matrix([[height/2, 0, width/2],
                              [0, height/2, height/2]])

指定3x3旋转矩阵:

rotation = np.array([[math.cos(angle), -math.sin(angle), 0],
                     [math.sin(angle), math.cos(angle), 0],
                     [0, 0, 1]])

首先旋转点,然后将其投影到窗口:

projected2d = projectionMatrix * rotation * point.reshape((3, 1))

完整示例:

import pygame
import numpy as np
import os
import math

WHITE = (255,255,255)
width, height = 400, 300
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()

points = []
angle = 0

points.append(np.matrix([ 0.5,  0.5, 1]))
points.append(np.matrix([ 0.5, -0.5, 1]))
points.append(np.matrix([-0.5,  0.5, 1]))
points.append(np.matrix([-0.5, -0.5, 1]))

projectionMatrix = np.matrix([[height/2, 0, width/2],
                             [0, height/2, height/2]])

while True:
    clock.tick(30)
    screen.fill((0,0,0))

    rotation = np.matrix([[math.cos(angle), -math.sin(angle), 0],
                         [math.sin(angle), math.cos(angle), 0],
                         [0, 0, 1]])

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                os._exit(1)

    for point in points:
        projected2d = projectionMatrix * rotation * point.reshape((3, 1))
        pygame.draw.circle(screen, WHITE, (int(projected2d[0][0]), int(projected2d[1][0])), 5)

    angle += 0.01
    pygame.display.update()

相关问题 更多 >