Trying to make a spinning cube but only shows a small white dot spinning?
Question:
I’m using pygame to make a 3D cube and make it spin but for some reason the output is just a small white dot. I think the cube is so small it looks like a dot.
import pygame
from pygame.locals import *
from math import sin, cos
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Define cube vertices
vertices = [(x, y, z) for x in (-1, 1) for y in (-1, 1) for z in (-1, 1)]
edges = [(0, 1), (1, 3), (3, 2), (2, 0), (4, 5), (5, 7), (7, 6), (6, 4), (0, 4), (1, 5), (2, 6), (3, 7)]
# Define rotation function
def rotate(x, y, angle):
return x*cos(angle) - y*sin(angle), x*sin(angle) + y*cos(angle)
# Set initial rotation angles
angle_x, angle_y, angle_z = 0, 0, 0
# Main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
# Clear screen
screen.fill((0, 0, 0))
# Apply rotation to vertices
rotated = []
for x, y, z in vertices:
# Rotate around x-axis
y, z = rotate(y, z, angle_x)
# Rotate around y-axis
x, z = rotate(x, z, angle_y)
# Rotate around z-axis
x, y = rotate(x, y, angle_z)
# Perspective projection
f = 200
scale = f / (f - z)
x, y = x*scale, y*scale
# Translate to screen coordinates
x, y = int(x + screen.get_width() / 2), int(y + screen.get_height() / 2)
rotated.append((x, y))
# Draw edges
for i, j in edges:
pygame.draw.line(screen, (255, 255, 255), rotated[i], rotated[j], 2)
# Update rotation angles
angle_x += 0.01
angle_y += 0.02
angle_z += 0.03
# Update screen
pygame.display.flip()
So I thought the windows was small so I made it bigger but it seems the cube isn’t getting bigger.
Answers:
Your cube is too small. You have to scale the vertices:
s = 20
for x, y, z in vertices:
x, y, z = x*s, y*s, z*s
# [...]
Alternatively can change the 2D coordinates when the geometry is projected on the display:
for x, y, z in vertices:
# [...]
x, y = int(x * 20 + screen.get_width() / 2), int(y * 20 + screen.get_height() / 2)
rotated.append((x, y))
I’m using pygame to make a 3D cube and make it spin but for some reason the output is just a small white dot. I think the cube is so small it looks like a dot.
import pygame
from pygame.locals import *
from math import sin, cos
pygame.init()
screen = pygame.display.set_mode((800, 600))
# Define cube vertices
vertices = [(x, y, z) for x in (-1, 1) for y in (-1, 1) for z in (-1, 1)]
edges = [(0, 1), (1, 3), (3, 2), (2, 0), (4, 5), (5, 7), (7, 6), (6, 4), (0, 4), (1, 5), (2, 6), (3, 7)]
# Define rotation function
def rotate(x, y, angle):
return x*cos(angle) - y*sin(angle), x*sin(angle) + y*cos(angle)
# Set initial rotation angles
angle_x, angle_y, angle_z = 0, 0, 0
# Main loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
# Clear screen
screen.fill((0, 0, 0))
# Apply rotation to vertices
rotated = []
for x, y, z in vertices:
# Rotate around x-axis
y, z = rotate(y, z, angle_x)
# Rotate around y-axis
x, z = rotate(x, z, angle_y)
# Rotate around z-axis
x, y = rotate(x, y, angle_z)
# Perspective projection
f = 200
scale = f / (f - z)
x, y = x*scale, y*scale
# Translate to screen coordinates
x, y = int(x + screen.get_width() / 2), int(y + screen.get_height() / 2)
rotated.append((x, y))
# Draw edges
for i, j in edges:
pygame.draw.line(screen, (255, 255, 255), rotated[i], rotated[j], 2)
# Update rotation angles
angle_x += 0.01
angle_y += 0.02
angle_z += 0.03
# Update screen
pygame.display.flip()
So I thought the windows was small so I made it bigger but it seems the cube isn’t getting bigger.
Your cube is too small. You have to scale the vertices:
s = 20
for x, y, z in vertices:
x, y, z = x*s, y*s, z*s
# [...]
Alternatively can change the 2D coordinates when the geometry is projected on the display:
for x, y, z in vertices:
# [...]
x, y = int(x * 20 + screen.get_width() / 2), int(y * 20 + screen.get_height() / 2)
rotated.append((x, y))