Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
ReadMe.txt
ReadMe: CubePuzzle |
This application models a Rubik's Cubeª using Cocoa and OpenGL. |
Features: |
+ Keyboard manipulation of each cube face and of each cube axis. |
+ Mouse manipulation of the camera. |
+ Undo/Redo support. |
+ Built in cube solver. |
+ Asynchronous animation via NSTimers. |
+ Multiple windows can be opened to view the cube from different angles. |
Details: |
Keyboard manipulation of the cube is as follows: |
l: Rotate left face clockwise |
L: Rotate left face counter-clockwise |
r: Rotate right face clockwise |
R: Rotate right face counter-clockwise |
f: Rotate front face clockwise |
F: Rotate front face counter-clockwise |
p: Rotate posterior face clockwise |
P: Rotate posterior face counter-clockwise |
t: Rotate top face clockwise |
T: Rotate top face counter-clockwise |
b: Rotate bottom face clockwise |
B: Rotate bottom face counter-clockwise |
x: Rotate cube on it's x axis clockwise |
X: Rotate cube on it's x axis counter-clockwise |
y: Rotate cube on it's y axis clockwise |
Y: Rotate cube on it's y axis counter-clockwise |
z: Rotate cube on it's z axis clockwise |
Z: Rotate cube on it's Z axis counter-clockwise |
s: Solve the cube. |
The cube can be rotated around via the mouse with a simple x,y mapping of heading/pitch. Sorry, no trackball manipulation yet. ;) |
The cube solver is based on the book "The Simple Solution to Rubik's Cubeª" by James G. Nourse. It's the same book I used to learn to solve the cube back in 5th grade or so, and was pretty straightforward to implement. Internally I use a textual representation of the moves (which mirror those used in the book) since it was very compact and makes the code easier to read. Note that the solution generated by the book is supposed to be easy to memorize. It does not generate anything even close to an optimal solution. That being said, there is a solution "optimizer" in the code that just tries to eliminate redundant moves in a very simplistic way, mainly to make the solution animation look a little less silly. |
The current cube state is always updated immediately, while there is an "asynchronous" state that works it's way towards the current cube state. This was done in order to simplify the undo/redo logic. Originally all user input and solution movies were queued up and there was only one cube state struct, but this really made it hard to use the standard undo/redo stuff in Cocoa. |
The Cube class is broken up into three categories. The main class handles all of the cube manipulation primitives and is where the undo/redo code is handled. The CubeRendering category implements all of the drawing code via OpenGL, including the animation. The CubeSolving category implements the solver logic. |
The cube is drawn using standard OpenGL immediate mode primitives. This is not the most efficient way it could be done, but it made the code simpler to write as the cube's geometry is generated by code rather than from a static model. Implementing faster rendering is left as an exercise for the reader. |
Other random notes: |
There is one "master" OpenGL context created by the Controller object in order to facilitate texture sharing. Note that this context is never attached to a drawable and has no rendering surface of it's own. |
Contact Info: |
If you have any questions about the code, you may contact me at kdyke@apple.com. |
-Ken |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-04-21