A 3D Rubik's Cube simulator and solver built with SwiftUI and Metal for iOS and macOS.
- 3D Visualization: Real-time 3D rendering using Metal shaders
- Interactive Controls: Manual cube manipulation with standard notation (U, R, L, F, B, D, H, V)
- Scrambling: Random cube scrambling with configurable move count
- Smooth Animations: Fluid rotation animations for all cube moves
- Cross-Platform: Supports both iOS 17+ and macOS 15+
The project follows a clean architecture pattern with three main components that work together to provide a smooth, interactive 3D Rubik's Cube experience:
┌──────────────────┐ ┌──────────────────┐ ┌───────────────────┐
│ Solver Module │───►──│ Animation Queue │───►──│ Renderer │
│ (CPU – Swift) │ │ (Swift) │ │ (GPU – Metal) │
└──────────────────┘ └──────────────────┘ └───────────────────┘
▲ ▲ ▲
│ │ │
│ └─── Model (CubeState) ───┘
│ │
└────────── User Input/UI ──────────────┘
The brain of the application that handles all cube logic and state management:
-
RubicCubeSolver: Core solver class implementing theSolverprotocol- Manages cube state through a
Cubeinstance - Applies moves using 3D rotation mathematics with SIMD vectors
- Updates cubie positions, orientations, and face colors
- Supports debug mode for move validation and logging
- Manages cube state through a
-
CubeModel: Represents the 3D cube state- Contains 27
Cubieobjects in a 3x3x3 grid - Each cubie stores position, transform matrix, and face colors
- Uses
RotatableGrid3Dfor efficient 3D array operations - Maintains both current and original positions for state tracking
- Contains 27
-
MoveSystem: Defines cube rotations- Axis-based rotations (X, Y, Z)
- Layer specification (0-2 for each axis)
- Direction control (clockwise/counter-clockwise)
- Mathematical rotation matrices for precise 3D transformations
Manages smooth transitions and user experience:
-
CubeAnimator: Handles move animations- Interpolates between cube states over time
- Provides smooth rotation transitions
- Manages animation timing and completion callbacks
- Supports configurable animation speeds
-
RubiksCubeViewModel: Coordinates the entire system- Manages move queue and animation pipeline
- Handles user input and button interactions
- Controls display link for 60fps rendering
- Implements scrambling algorithms with move validation
- Bridges between UI and core logic
-
Display Link Integration: Ensures smooth 60fps updates
- Uses
CADisplayLinkfor frame-perfect timing - Processes animation frames and applies completed moves
- Triggers UI updates through
@Publishedproperties
- Uses
High-performance 3D visualization:
-
RubiksCubeMetalView: Metal-based 3D rendering- Custom Metal shaders for cube visualization
- Real-time 3D transformations and lighting
- Efficient GPU-based rendering pipeline
- Support for both iOS and macOS Metal APIs
-
Shader System: Custom Metal shaders
- Vertex shaders for 3D transformations
- Fragment shaders for lighting and coloring
- Optimized for mobile and desktop GPUs
- Handles 27 cubies with individual transforms
-
3D Mathematics: SIMD-based calculations
- Uses
simd_float4x4matrices for transformations - Efficient vector operations for rotations
- Precise 3D positioning and orientation
- Cross-platform mathematical consistency
- Uses
Modern, responsive user interface:
-
ContentView: Main application interface- Clean, intuitive button layout
- Real-time cube visualization
- Responsive design for different screen sizes
- Accessibility support built-in
-
State Management: Reactive UI updates
@StateObjectfor view model binding- Automatic UI updates on cube state changes
- Smooth integration with animation system
- Cross-platform UI consistency
- User Input: Button presses trigger move enqueueing
- Move Processing: Animation queue processes moves sequentially
- State Updates: Solver applies completed moves to cube state
- Rendering: Metal renderer displays updated cube in 3D
- UI Refresh: SwiftUI updates interface based on new state
- Protocol-Oriented Programming:
Solverprotocol for extensibility - MVVM Architecture: Clear separation between view and business logic
- Dependency Injection: Solver and animator injected into view model
- Reactive Programming: Combine framework for state management
- Resource Management: Efficient Metal resource handling
- iOS 17.0+ / macOS 15.0+
- Xcode 15.0+
- Swift 6.1+
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/yourusername/RubicCube.git", from: "1.0.0")
]- Clone the repository:
git clone https://github.com/yourusername/RubicCube.git- Open
RubicCube.xcworkspacein Xcode - Build and run the project
- U, R, L, F, B, D: Standard Rubik's cube face rotations
- H, V: Horizontal and vertical middle layer rotations
- Scramble: Randomly scrambles the cube with 20 moves
- Reset: Returns the cube to solved state
To use RubicCube in your own project:
import SwiftUI
import RubicCubeSolver
struct MyView: View {
var body: some View {
ContentView()
}
}Sources/RubicCubeSolver/
├── Animators/ # Animation system
├── Helpers/ # Utility functions
├── Shaders/ # Metal shader files
├── Solver/ # Core cube logic
│ ├── Model/ # Data structures
│ └── RubicCubeSolver.swift
└── View/ # SwiftUI components
The cube is represented as a 3x3x3 grid of 27 cubies, each containing:
- Position and orientation transforms
- Face color information
- Original position tracking
Moves are defined by:
- Axis: X, Y, or Z rotation axis
- Layer: Which layer to rotate (0-2)
- Direction: Clockwise or counter-clockwise
- CPU: Move calculation and state updates
- Animation: Smooth interpolation between states
- GPU: Metal shader-based 3D rendering
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with SwiftUI and Metal
- Inspired by classic Rubik's Cube mechanics
- Thanks to the Swift community for excellent documentation and examples
