Last semester I got my first experience with 3D programming – something I had wanted to learn how to do for quite a while. One of our first assignments was to get a handful of primitives moving around on screen (first screenshot below). I was motivated enough to continue expanding the project, and added 9 smaller cubes that formed a larger cube, along with a large sky-box-like cube.

Once I got to that point, I figured – why not take it further? I started thinking about how I could get the “Rubik’s cube” to rotate like the real thing.

After many hours of trying, and several note pad pages full of indices and diagrams …

… I still couldn’t get it to work.

I thought I’d take a break from the rotation code and fix the colours so that the inside faces were black.

But I could only prolong the inevitable long hall of getting stackable rotations working that was ahead of me – and long it was.

However, after many hours I found a solution! In the end I found rotation matrices to be the ideal tool for stacking multiple rotations together, combined with some rather messy index swapping after each rotation.

I decided to add a textured plane that cut through the cube which to show the controls for rotating the layers. Printing the controls out like this made clear just how ridiculous they were – the next step was to replace them with something more sane.

Fast forward several months and I’ve finally found the time to improve the input method. Now, instead of pressing one of a dozen keys to rotate a layer, you simply click on the layer you want to rotate; left click for clockwise rotations, right for anti-clockwise.

I also added the ability to begin randomly scrambling the cube on a key press (shown in the gif), and instantly solve it by pressing another.

Like with any project, there are things I’d like to fix and lots of things I’d like to add, but I have to call it at some point, and I think this is that point for this project. I have plenty of other projects going on at the moment, plus it feels great to have another one in the “finished” list.

Thanks for reading! I’ll be back soon with another post on my graphics API comparison project.