🇬🇧 English Version | 🇫🇷 Version Française
A pedagogical project to understand the basics of 3D: rotation, projection and animation.
This program displays a 3D cube that rotates continuously. It is designed to be simple and educational, showing fundamental concepts of 3D programming without complex libraries.
- 🔄 3D Rotation: How to rotate points in space
- 📐 Perspective projection: Transforming 3D coordinates into 2D for display
- 🎬 Animation: Creating smooth animation by refreshing the screen
- Python 3.x
- Tkinter (included by default with Python)
python test3D.pyA window opens with a green cube rotating on a black background.
The cube is defined by:
- 8 corners (vertices) in 3D coordinates
(x, y, z) - 12 edges (lines) connecting these corners
# Example: front-bottom-left corner
(-1, -1, -1)
# x y zRotation around the Y axis uses a rotation matrix:
x' = x × cos(θ) - z × sin(θ)
z' = x × sin(θ) + z × cos(θ)
y' = y (no change)
This makes the cube spin like a top.
To display 3D on a 2D screen, we use division by depth:
x_screen = x / z
y_screen = y / z
The farther an object is (large z), the smaller it appears. It's like looking through a window!
The cube is redrawn every 16 milliseconds (~60 frames/second):
- Clear the screen
- Calculate new positions (rotation)
- Draw edges
- Increment angle
- Repeat
test3D.py
├── Configuration (colors, size)
├── Cube3D Class
│ ├── __init__() → Initialize window and data
│ ├── tourner() → Apply 3D rotation
│ ├── projeter() → Convert 3D → 2D
│ ├── animer() → Animation loop
│ └── demarrer() → Launch application
└── Main program
You can easily modify:
# Window size
LARGEUR = 800
HAUTEUR = 800
# Colors
COULEUR_FOND = "#101010" # Black background
COULEUR_LIGNE = "#50FF50" # Green lines
# Rotation speed (in animer())
self.angle += 0.05 # Larger = faster- Add X-axis rotation: Make the cube rotate on two axes
- Change colors: Color each face differently
- Add controls: Use the keyboard to control rotation
- Draw other shapes: Pyramid, tetrahedron, etc.
- Add depth: Darken distant edges
- 3D Mathematics - Rotation matrices
- Perspective projection - Theory
- Tkinter Canvas - Documentation
3D may seem intimidating, but it relies on simple mathematical principles:
- Geometry: Points and lines
- Trigonometry: Sine and cosine for rotations
- Perspective: Division to simulate depth
This code shows that with a hundred lines, you can create something visually impressive!
Code free to use for learning and teaching.
This README and the detailed code comments were written with the help of an AI LLM (Large Language Model) to make the project more accessible and educational.
Happy learning 3D! 🎓