Introduction

In this post, I introduce a project based on a Python program generated by ChatGPT’s o3‑mini and o3‑mini‑high. The program was originally created using the following prompt:

write a Python program that shows a ball bouncing inside a spinning hexagon. The ball should be affected by gravity and friction, and it must bounce off the rotating walls realistically

The original code was produced automatically by the AI, and afterward I manually enhanced it by increasing the number of balls and changing their sizes. These modifications enriched the visual presentation and created a more complex simulation of physical behavior—all while the core functionality remains a testament to the capability of o3‑mini.

Background of the Inspiration

This project was also inspired by posts and discussions on X (formerly Twitter). On X, I came across similar projects, technical ideas, and user feedback that spurred further improvements and new ideas. The realistic physics simulations and debates about AI-generated code on the platform provided valuable hints that influenced the enhancements made to this project.

https://www.instagram.com/p/DFjZpFbBuk_/

Overview of the Program

1. Intent Behind the Prompt

  • Physics Simulation:The goal was to simulate a ball that experiences gravity and friction, realistically depicting its bouncing and deceleration.
  • Rotating Hexagon:The ball must interact with the constantly moving walls of a hexagon, bouncing off them in a realistic manner.

2. Contributions of o3‑mini and o3‑mini‑high

  • Rapid Prototyping Through Automatic Generation:The initial code was swiftly created using the o3‑mini series.
  • Implementation of Realistic Physical Behavior:The simulation incorporates gravity, friction, and collision calculations so that the ball reflects off the walls accurately.

3. Enhancements – Increasing the Number and Changing the Sizes of the Balls

  • Multiple Balls Implementation:By allowing several balls to move within the scene simultaneously, the simulation now features complex interactions and enhanced visual effects.
  • Size Adjustments for Diverse Dynamics:Changing the sizes of the balls introduces varied dynamics in their bounces and frictional responses, adding depth to the simulation.

Modifying text snippet

I'm refining the request to adjust the provided sample code excerpt for clarity and accuracy.

#!/usr/bin/env python3
import math
import pygame
import sys
import random


# --- Functions for 4D Rotation and Projection ---


def rotate4d(v, angle_xy, angle_xz, angle_xw, angle_yz, angle_yw, angle_zw):
    """
    Rotate a 4D point v = (x, y, z, w) sequentially in the six planes (xy, xz, xw, yz, yw, zw).
    """
    x, y, z, w = v
    # Rotation in the xy plane
    x, y = x * math.cos(angle_xy) - y * math.sin(angle_xy), x * math.sin(angle_xy) + y * math.cos(angle_xy)
    # Rotation in the xz plane
    x, z = x * math.cos(angle_xz) - z * math.sin(angle_xz), x * math.sin(angle_xz) + z * math.cos(angle_xz)
    # Rotation in the xw plane
    x, w = x * math.cos(angle_xw) - w * math.sin(angle_xw), x * math.sin(angle_xw) + w * math.cos(angle_xw)
    # Rotation in the yz plane
    y, z = y * math.cos(angle_yz) - z * math.sin(angle_yz), y * math.sin(angle_yz) + z * math.cos(angle_yz)
    # Rotation in the yw plane
    y, w = y * math.cos(angle_yw) - w * math.sin(angle_yw), y * math.sin(angle_yw) + w * math.cos(angle_yw)
    # Rotation in the zw plane
    z, w = z * math.cos(angle_zw) - w * math.sin(angle_zw), z * math.sin(angle_zw) + w * math.cos(angle_zw)
    return (x, y, z, w)


def project4d_to_3d(v, distance4d):
    """
    Project a 4D point (x, y, z, w) into a 3D point using perspective projection.
    'distance4d' is the distance from the 4D viewpoint (determines the contraction due to w).
    """
    x, y, z, w = v
    factor = distance4d / (distance4d - w)
    return (x * factor, y * factor, z * factor)


def project3d_to_2d(v, viewer_distance, scale, screen_width, screen_height):
    """
    Project a 3D point (x, y, z) onto 2D screen coordinates.
    Points farther away (with larger z) are drawn smaller.
    """
    x, y, z = v
    factor = scale * viewer_distance / (viewer_distance + z)
    x2d = int(x * factor + screen_width / 2)
    y2d = int(-y * factor + screen_height / 2# Invert y-axis to match screen coordinates
    return (x2d, y2d, factor)


# --- Main Program ---


def main():
    pygame.init()
    screen_width, screen_height = 800, 600
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("10 Colored Balls Bouncing Inside a Tesseract")
    clock = pygame.time.Clock()
    
    # Projection parameters
    distance4d = 3          # Distance for 4D to 3D projection
    viewer_distance = 6     # Distance for 3D to 2D projection
    scale = 100             # Projection scale
    
    # Generate vertices of the tesseract (4D hypercube), each coordinate is -1 or 1
    vertices = []
    for i in range(16):
        point = []
        for j in range(4):
            point.append(1 if (i >> j) & 1 else -1)
        vertices.append(tuple(point))
    
    # Connect vertices with an edge if they differ in exactly one coordinate
    edges = []
    for i in range(len(vertices)):
        for j in range(i + 1, len(vertices)):
            if sum(1 for a, b in zip(vertices[i], vertices[j]) if a != b) == 1:
                edges.append((i, j))
    
    # Initial angles for 4D rotation (in radians)
    angle_xy = angle_xz = angle_xw = angle_yz = angle_yw = angle_zw = 0.0
    
    # Initial state of balls in 4D space (10 balls)
    ball_radius = 0.05  # Radius of each ball in 4D space
    num_balls = 10
    balls = []
    for _ in range(num_balls):
        pos = [random.uniform(-1 + ball_radius, 1 - ball_radius) for _ in range(4)]
        vel = [random.uniform(-0.5, 0.5) for _ in range(4)]
        # Random RGB color (each component between 50 and 255)
        color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255))
        ball = {
            'pos': pos,
            'vel': vel,
            'color': color,
            'radius': ball_radius
        }
        balls.append(ball)
    
    # Natural conditions (gravity and energy loss on bounce)
    gravity = -0.2    # Gravitational acceleration in the y-axis (index 1)
    restitution = 0.9 # Coefficient of restitution for collisions with walls
    
    running = True
    while running:
        dt = clock.tick(60) / 1000.0  # Elapsed time per frame (in seconds)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        # --- Update physics for each ball ---
        for ball in balls:
            for i in range(4):
                # Apply gravity only along the y-axis (index 1)
                if i == 1:
                    ball['vel'][i] += gravity * dt
                ball['pos'][i] += ball['vel'][i] * dt
                # The walls of the tesseract are at coordinates -1 and 1
                if ball['pos'][i] - ball['radius'] < -1:
                    ball['pos'][i] = -1 + ball['radius']
                    ball['vel'][i] = -ball['vel'][i] * restitution
                elif ball['pos'][i] + ball['radius'] > 1:
                    ball['pos'][i] = 1 - ball['radius']
                    ball['vel'][i] = -ball['vel'][i] * restitution
        
        # --- Clear the screen ---
        screen.fill((0, 0, 0))
        
        # --- Rotate and project the vertices of the tesseract ---
        projected_vertices = []
        for v in vertices:
            v_rot = rotate4d(v, angle_xy, angle_xz, angle_xw, angle_yz, angle_yw, angle_zw)
            v_3d = project4d_to_3d(v_rot, distance4d)
            x2d, y2d, _ = project3d_to_2d(v_3d, viewer_distance, scale, screen_width, screen_height)
            projected_vertices.append((x2d, y2d))
        
        # --- Draw the edges of the tesseract ---
        for edge in edges:
            i, j = edge
            pygame.draw.line(screen, (255, 255, 255), projected_vertices[i], projected_vertices[j], 1)
        
        # --- Rotate, project, and draw each ball's position in 4D ---
        for ball in balls:
            ball_rot = rotate4d(tuple(ball['pos']),
                                angle_xy, angle_xz, angle_xw,
                                angle_yz, angle_yw, angle_zw)
            ball_3d = project4d_to_3d(ball_rot, distance4d)
            ball_x, ball_y, proj_factor = project3d_to_2d(ball_3d, viewer_distance, scale, screen_width, screen_height)
            ball_screen_radius = max(1, int(ball['radius'] * proj_factor))
            pygame.draw.circle(screen, ball['color'], (ball_x, ball_y), ball_screen_radius)
        
        # --- Update the screen ---
        pygame.display.flip()
        
        # --- Update rotation angles ---
        angle_xy += 0.5 * dt
        angle_xz += 0.3 * dt
        angle_xw += 0.4 * dt
        angle_yz += 0.5 * dt
        angle_yw += 0.3 * dt
        angle_zw += 0.4 * dt


    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()


Conclusion

This project is a prime example of combining AI’s automatic code generation with manual refinements.

  • Base Generation: Rapid code creation was achieved using o3‑mini and o3‑mini‑high.
  • Enhancements: Manual modifications such as increasing the number of balls and adjusting their sizes improved both the visual effects and the complexity of the physics simulation.
  • Inspiration from X: Posts and feedback on X provided additional inspiration, guiding further improvements and innovative ideas.

This work demonstrates the exciting potential of collaborative efforts between AI tools and human creativity. I hope it inspires you to explore the possibilities of integrating AI and community feedback into your own projects. Please feel free to share your thoughts or questions via the Contact form.