3d引擎旋转使物体变形[重复]

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

当我运行它时,立方体被压扁,看起来像一个 2d 正方形,里面有一个较小的正方形,并且在两个方向上转动,尽管它只应该沿着 z 轴转动。如果我在 rx 和 rz 中转动它,那么它是 3d,但它只是在变形,当只在 rx 中旋转时它几乎完美地工作,但仍然有一点变形,只是在一个方向上拉伸自己。

import pygame
from math import sin, cos
def f2dto3d(coord3d, scale=1, offset=(0, 0, 0), rotx=0, rotz=0):
    cx = rotateX(coord3d, rotx)[0]
    cy = rotateX(coord3d, rotx)[1]
    cz = rotateX(coord3d, rotx)[2]
    cx = rotateZ((cx, cy, cz), rotz)[0]
    cy = rotateZ((cx, cy, cz), rotz)[1]
    cz = rotateZ((cx, cy, cz), rotz)[2]


    #offset points by offset
    cx += offset[0]
    cy += offset[1]
    cz += offset[2]

    #project points onto 2d plane
    return (cx/cz*scale + 250, cy/cz*scale + 250)

def rotateX(coord3d, rotx=0):
    cx = coord3d[0]
    cy = coord3d[1]
    cz = coord3d[2]
    #rotate points around x axis
    rcx = cx
    rcy = cy*cos(rotx) - cz*sin(rotx)
    rcz = cz*cos(rotx) - cy*sin(rotx)  
    return (rcx, rcy, rcz)

def rotateZ(coord3d, rotz=0):
    cx = coord3d[0]
    cy = coord3d[1]t
    cz = coord3d[2]

    #rotate points around z axis
    rcx = cx*cos(rotz) - cy*sin(rotz)
    rcy = cy*cos(rotz) - cx*sin(rotz)
    rcz = cz
    return (rcx, rcy, rcz)


pygame.init()
screen = pygame.display.set_mode([500, 500])

points = [(-0.5, -0.5, 0.5),
          (0.5, -0.5, 0.5),
          (0.5, -0.5, -0.5),
          (-0.5, -0.5, -0.5),
          (-0.5, 0.5, 0.5),
          (0.5, 0.5, 0.5),
          (0.5, 0.5, -0.5),
          (-0.5, 0.5, -0.5)]
lines = [(0, 1), (1, 2), (2, 3), (3, 0), 
         (0, 4), (1, 5), (2, 6), (3, 7),
         (4, 5), (5, 6), (6, 7), (7, 4)]
size = 100
dist=(0, 0, 2.5)

running = True
rx=0
rz=0
while running:
    rx+=0.001
    rz+=0.001
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    screen.fill((255, 255, 255))
    for point in points:
        pygame.draw.circle(screen, (0, 0, 0),f2dto3d(point, size, dist, 0, rz), 2)
    for line in lines:
        pygame.draw.line(screen, (0, 0, 0), f2dto3d(points[line[0]], size, dist, 0, rz), f2dto3d(points[line[1]], size, dist, 0, rz), width=1)
    
    pygame.display.flip()
pygame.quit()

我已经有一段时间遇到这个问题了,我已经让旋转函数使用与输入不同的值,这让它变得更好了一点,但是,这个问题仍然存在。

python pygame 3d rotation rendering
© www.soinside.com 2019 - 2024. All rights reserved.