write the code for pygame 4d cube and you can rotate it with the mouse
- User
write the code for pygame 4d cube and you can rotate it with the mouse
- Assistant
write the code for pygame 4d cube and you can rotate it with the mouse
Certainly! Below is a Python script that uses Pygame to render a 4D hypercube (tesseract) that you can rotate using the mouse. This script maps mouse movements to rotations in the 4D space, allowing interactive exploration of the hypercube.
import pygame
import sys
import math
import numpy as np
# Initialize Pygame
pygame.init()
# Screen dimensions
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("4D Hypercube Rotation")
# Define the 16 vertices of a tesseract (4D hypercube)
points = []
for x in (-1, 1):
for y in (-1, 1):
for z in (-1, 1):
for w in (-1, 1):
points.append(np.array([x, y, z, w]))
# Define the edges connecting the vertices
edges = []
for i in range(len(points)):
for j in range(i + 1, len(points)):
diff = np.abs(points[i] - points[j])
if np.sum(diff == 0) == 3:
edges.append((i, j))
# Rotation matrices for each of the six planes in 4D space
def rotate_xy(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ c, -s, 0, 0],
[ s, c, 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1],
])
def rotate_xz(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ c, 0, -s, 0],
[ 0, 1, 0, 0],
[ s, 0, c, 0],
[ 0, 0, 0, 1],
])
def rotate_xw(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ c, 0, 0, -s],
[ 0, 1, 0, 0],
[ 0, 0, 1, 0],
[ s, 0, 0, c],
])
def rotate_yz(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ 1, 0, 0, 0],
[ 0, c, -s, 0],
[ 0, s, c, 0],
[ 0, 0, 0, 1],
])
def rotate_yw(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ 1, 0, 0, 0],
[ 0, c, 0, -s],
[ 0, 0, 1, 0],
[ 0, s, 0, c],
])
def rotate_zw(angle):
c, s = math.cos(angle), math.sin(angle)
return np.array([
[ 1, 0, 0, 0],
[ 0, 1, 0, 0],
[ 0, 0, c, -s],
[ 0, 0, s, c],
])
# Projection functions
def project_4d_to_3d(point4d):
"""Perspective projection from 4D to 3D."""
w = 2 / (4 - point4d[3])
projection_matrix = np.array([
[w, 0, 0, 0],
[0, w, 0, 0],
[0, 0, w, 0],
])
point3d = projection_matrix @ point4d
return point3d
def project_3d_to_2d(point3d):
"""Perspective projection from 3D to 2D."""
distance = 5
z = 1 / (distance - point3d[2])
projection_matrix = np.array([
[z, 0, 0],
[0, z, 0],
])
point2d = projection_matrix @ point3d
return point2d
# Initialize rotation angles
angle_xy = 0
angle_xz = 0
angle_xw = 0
angle_yz = 0
angle_yw = 0
angle_zw = 0
# Main loop
clock = pygame.time.Clock()
mouse_down = False
last_mouse_pos = None
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Mouse controls for rotation
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
mouse_down = True
last_mouse_pos = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
mouse_down = False
elif event.type == pygame.MOUSEMOTION:
if mouse_down:
x, y = pygame.mouse.get_pos()
dx = x - last_mouse_pos[0]
dy = y - last_mouse_pos[1]
angle_xw += dy * 0.005
angle_yw += dx * 0.005
last_mouse_pos = (x, y)
# Clear the screen
screen.fill((0, 0, 0))
# Rotation matrices for current angles
rot = rotate_xy(angle_xy) @ rotate_xz(angle_xz) @ rotate_xw(angle_xw) \
@ rotate_yz(angle_yz) @ rotate_yw(angle_yw) @ rotate_zw(angle_zw)
# Transform and project all points
transformed_points = []
for point in points:
rotated_point = rot @ point
point3d = project_4d_to_3d(rotated_point)
point2d = project_3d_to_2d(point3d)
x = int(point2d[0] * width / 2 + width / 2)
y = int(point2d[1] * height / 2 + height / 2)
transformed_points.append((x, y))
# Draw edges
for edge in edges:
p1 = transformed_points[edge[0]]
p2 = transformed_points[edge[1]]
pygame.draw.line(screen, (255, 255, 255), p1, p2, 1)
# Update the display
pygame.display.flip()
clock.tick(60)
Instructions to Run the Script:
Install Pygame and NumPy: Ensure you have Pygame and NumPy installed in your Python environment.
pip install pygame numpy
Save the Script: Copy the script into a file named, for example, tesseract.py.
Run the Script: Execute the script using Python.
python tesseract.py
How to Use the Program:
angle_xw += dy * 0.005
angle_yw += dx * 0.005
Understanding the Code:
Vertices and Edges: The hypercube is defined by its vertices (points) and edges (edges). Edges connect vertices that differ by only one coordinate.
Rotations: Six rotation matrices (rotate_xy, rotate_xz, rotate_xw, rotate_yz, rotate_yw, rotate_zw) represent rotations in each of the six possible planes in 4D space.
Projections: The function project_4d_to_3d performs a perspective projection from 4D to 3D, and project_3d_to_2d from 3D to 2D, for rendering on the screen.
Mouse Interaction: Mouse movements adjust the rotation angles, allowing interactive rotation of the hypercube.
Rendering: Edges are drawn between the projected points to visualize the hypercube.
Note:
Rotating in higher dimensions can be non-intuitive. Experiment with the mouse controls to get a feel for how the hypercube behaves under different rotations.
This script provides a foundation for visualizing 4D objects and can be extended or modified for more advanced features, such as shading, coloring, or automatic rotations.
Enjoy exploring the fascinating world of four-dimensional geometry!
Not yet included in a published release.