如何在 pygame 上使图像成为棱镜的面

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

我想将图像添加到棱镜的矩形面上,但我不知道该怎么做,我希望图像是棱镜的面,并且您可以看到它正在旋转,因此它是倾斜的 这是我到目前为止所拥有的:

import pygame #import module
from math import *
pygame.init() #initialise it
WINDOW_SIZE =  800
ROTATE_SPEED = 0.02
SCREEN = pygame.display.set_mode( (WINDOW_SIZE, WINDOW_SIZE) ) #create a screen
clock = pygame.time.Clock() #add clock 

projection_matrix = [[1,0,0],
                     [0,1,0],
                     [0,0,0]]

prism_points = [n for n in range(6)] #creating points of prism
prism_points[0] = [[-2],[-1],[2]]
prism_points[1] = [[2],[-1],[2]]
prism_points[2] = [[0],[2],[2]]
prism_points[3] = [[-2],[-1],[-2]]
prism_points[4] = [[2],[-1],[-2]]
prism_points[5] = [[0],[2],[-2]]

def multiply_m(a,b): #multiplication of matrices
    a_rows = len(a)
    a_cols = len(a[0])

    b_rows = len(b)
    b_cols = len(b[0])
    # Dot product matrix dimentions = a_rows x b_cols
    product = [[0 for _ in range(b_cols)] for _ in range(a_rows)]

    if a_cols == b_rows:
        for i in range(a_rows):
            for j in range(b_cols):
                for k in range(b_rows):
                    product[i][j] += a[i][k] * b[k][j]
    else:
        print("INCOMPATIBLE MATRIX SIZES") #dimension error
    return product   

def connect_points(i ,j ,points):
    pygame.draw.line(SCREEN, (255,255,255), (points[i][0],points[i][1]), (points[j][0],points[j][1])) #connecting points of the vertices
#main loop
scale = 100
angle_x = angle_y = angle_z = 0 #starting angles
while True: 
    clock.tick(60)
    SCREEN.fill ((0,0,0))
    #rotation matrices
    rotation_x = [[1, 0, 0],
                    [0, cos(angle_x), -sin(angle_x)],
                    [0, sin(angle_x), cos(angle_x)]]

    rotation_y = [[cos(angle_y), 0, sin(angle_y)],
                    [0, 1, 0],
                    [-sin(angle_y), 0, cos(angle_y)]]

    rotation_z = [[cos(angle_z), -sin(angle_z), 0],
                    [sin(angle_z), cos(angle_z), 0],
                    [0, 0, 1]]

    points = [0 for _ in range(len(prism_points))]
    i = 0
    for point in prism_points:
        rotate_x = multiply_m(rotation_x, point)
        rotate_y = multiply_m(rotation_y, rotate_x)
        rotate_z = multiply_m(rotation_z, rotate_y)
        point_2d = multiply_m(projection_matrix, rotate_z)
    
        x = (point_2d[0][0] * scale) + WINDOW_SIZE/2
        y = (point_2d[1][0] * scale) + WINDOW_SIZE/2

        points[i] = (x,y)
        i += 1

        pygame.draw.circle(SCREEN, (255,0,0), (x,y), 5)
    connect_points(0,1,points)
    connect_points(0,2,points)
    connect_points(0,3,points)
    connect_points(1,2,points)
    connect_points(1,4,points)
    connect_points(2,5,points)
    connect_points(3,4,points)
    connect_points(3,5,points)
    connect_points(4,5,points)
    

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_r]:
            angle_y = angle_x = angle_z = 0
        if keys[pygame.K_a]:
            angle_y += ROTATE_SPEED
        if keys[pygame.K_d]:
            angle_y -= ROTATE_SPEED      
        if keys[pygame.K_w]:
            angle_x += ROTATE_SPEED
        if keys[pygame.K_s]:
            angle_x -= ROTATE_SPEED
        if keys[pygame.K_q]:
            angle_z -= ROTATE_SPEED
        if keys[pygame.K_e]:
            angle_z += ROTATE_SPEED  
    pygame.display.update()

我只知道如何加载图像,我不知道之后该做什么 我想将图像与矩形的角对齐

python pygame 3d
1个回答
0
投票

要将图像添加到棱镜的矩形面并在 3D 空间中旋转它,您可以按照以下步骤操作:

加载要用于棱镜每个面的图像。 根据棱镜的顶点将图像映射到棱镜的相应面。 更新绘图代码以使用每个面的映射图像。 这是修改后的代码,其中包含每个步骤的说明:

import pygame
from math import *

# ... Your existing code ...

# Step 1: Load the images for each face of the prism
image_front = pygame.image.load("front.jpg")
image_back = pygame.image.load("back.jpg")
image_left = pygame.image.load("left.jpg")
image_right = pygame.image.load("right.jpg")
image_top = pygame.image.load("top.jpg")
image_bottom = pygame.image.load("bottom.jpg")

# Step 2: Map the images to the corresponding faces of the prism
prism_faces = [
    {'vertices': [0, 1, 2], 'image': image_front},
    {'vertices': [3, 4, 5], 'image': image_back},
    {'vertices': [0, 1, 4, 3], 'image': image_left},
    {'vertices': [2, 1, 4, 5], 'image': image_right},
    {'vertices': [0, 2, 5, 3], 'image': image_top},
    {'vertices': [1, 2, 5, 4], 'image': image_bottom}
]

# ... Your existing code ...

# Step 3: Modify the drawing code to use the mapped images for each face
def draw_face(face, points):
    vertices = face['vertices']
    image = face['image']
    vertex_list = [points[i] for i in vertices]

    # Get the average position of the vertices to determine the center of the face
    center_x = sum([v[0] for v in vertex_list]) / len(vertex_list)
    center_y = sum([v[1] for v in vertex_list]) / len(vertex_list)

    # Calculate the angle of the face based on the orientation of the vertices
    angle = atan2(vertex_list[1][1] - vertex_list[0][1], vertex_list[1][0] - vertex_list[0][0])

    # Rotate the image based on the calculated angle
    rotated_image = pygame.transform.rotate(image, degrees(angle))

    # Calculate the position to blit the image on the screen
    x = center_x - rotated_image.get_width() / 2
    y = center_y - rotated_image.get_height() / 2

    SCREEN.blit(rotated_image, (x, y))

# ... Your existing code ...

# Inside the main loop, update the drawing part:
while True:
    # ... Your existing code ...

    # Clear the screen
    SCREEN.fill((0, 0, 0))

    # Draw the faces of the prism with mapped images
    for face in prism_faces:
        draw_face(face, points)

    # ... Your existing code ...

    pygame.display.update()

确保将“front.jpg”、“back.jpg”、“left.jpg”、“right.jpg”、“top.jpg”和“bottom.jpg”替换为您想要的图像的文件名用于棱镜的每个相应面。此代码会将图像应用到棱镜的面上,并在 3D 空间中旋转棱镜,同时保持图像在面上的正确方向。

© www.soinside.com 2019 - 2024. All rights reserved.