A Rubik's cube implementation in Three.js
Join the DZone community and get the full member experience.Join For Free
in 2003, i took the time to put together a page that explains how to solve the rubik’s cube with a set of formulas that are easy to memorize. the idea was not necessarily to solve the cube quickly (it takes about 50-60 seconds to solve the cube with this approach) but to make it easy for anyone to beat the cube with little effort, as opposed to the hundreds of formulas that speed cubists have to memorize to remain competitive.
back then, i used a java applet that represents a cube with formulas and then playing these formulas to explain to the reader how exactly they work. as everybody knows, java applets have fallen out of favor even more today than they already were ten years ago, so i’ve been wanting to update my page with more modern technologies for a while, especially if these technologies don’t show a scary warning to everyone who reaches my web site.
while it’s easy to find rapidly implemented rubik’s cubes in three.js , i couldn’t find anything that came remotely close to what i needed, namely, being able to configure a cube directly from the html page along with the formula, and playing this formula at the click of a button. three.js turned out to be a great match for this, with the perfect amount of abstraction and power. and with three.js came a few free bonus tools, such as being able to move the cube around and also effortlessly zooming in and out. of course, it’s equally trivial to modify the size of the cube, the position of the camera, the field of view, and just as easy to add more fancy stuff such as lighting, shadows and fancy materials.
the implementation is open source but i want to write some proper documentation before publishing it.
implementing the cube itself was the most interesting part: there are so many ways you can model a rubik’s cube that such a problem is a software designer’s dream. i must have had three different iterations of the data model before i settled on the version you see now (and i’m already thinking of ways i could improve it).
three.js has come a long way since i gave it a try two years ago. the web is still filled with incorrect information referencing an old api that has since then changed, but it’s very intuitive and, most importantly, it allowed me to completely avoid having to deal with the webgl madness. i don’t know if it’s my brain that’s just not wired for this, but i have tried to read countless opengl tutorials over the past years and every time, i give up after an hour, my eyes glazing over the intricate, effect-littered, abstraction empty details of opengl. the api is probably too low level for someone like me with just a passing interest for gory graphical details.
my knowledge of computer graphics and animation is abysmal overall, so this was a great opportunity to move myself out of my comfort zone and force myself to confront problems that i usually never encounter, such as finding tricks to counter floating point rounding errors and revising matrix multiplications and other miscellaneous linear algebra and 3d geometry concepts such as quaternions and gimbal locks .
Published at DZone with permission of Cedric Beust, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.