pygame 中的 3D 投影

3D Projection in pygame

我正在尝试创建立方体的简单 3d 渲染。正如编码火车的这段视频: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()

对于为什么会发生这种情况以及如何解决它以便它只是旋转,我非常感谢任何帮助。

这段代码没有错误。这些点围绕左上角 (0, 0) 旋转。注意,在 3D 模式下,p5.js uses a different coordinate system than pygame.

如果要围绕 window 的中心旋转点,则定义范围 [-1, 1] 中的点(归一化设备 space:

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]到window的投影矩阵space:

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]])

先旋转点,然后投影到window:

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()