2018-12-28

Amazing

Around 18 months ago I made a Russian Doll Puzzle Box on thingiverse. This was after initially doing a simple two part box. The idea was pretty simple - the box has a maze on it, so the lid has to be moved around the maze to close or open the box.

It is surprisingly popular, with over 150,000* views on thingiverse and no idea how many from my site that makes the random versions on the fly.


The original code was almost all done in OpenSCAD, with a small bit of C code to create a random maze and set the various parameters used by the OpenSCAD script. It worked, but rendering the OpenSCAD was quite slow.

This Christmas I thought I would have a bit of fun re-doing it. There have been various requests and suggestions since the original design was posted. So I have incorporated a lot of those. The big one was having the maze on the inside of the box so making it way harder to solve! I have also added a helical option, and ability to emboss an initial on the ends of the box.

I decided to take a new approach. Instead of designing in OpenSCAD, I designed it all in C code, making OpenSCAD from scratch from the C. This is only subtly different, and it uses OpenSCAD to carve the maze from a cylinder in much the same way as before - just more of a long list of functions generated by C code rather than iterative OpenSCAD script. This worked, after a lot of tinkering, and after a couple of days I had a whole new set of test mazes. There are loads of tweaks once you print something for real on a 3D printer, and that takes a lot of time, so the test cycle is slow...

Last night I ended up re-writing a whole chunk of it in my sleep! Today I coded the changes, and it works. The change was simple in concept - instead of using OpenSCAD to carve shapes from a cylinder, I would construct a polyhedron from scratch in the C code to make the maze surface.

This was not as easy as it was in my sleep as I learned a bit about manifold polyhedrons, but it works. It makes the OpenSCAD unreadable as it is a huge list of 3D points and faces.

The end result is a much neater maze, and it is away quicker. The most complex test I had was taking OpenSCAD over an hour to convert to STL, but the new version is just over a minute, and most tests are a few seconds.


The other big difference is I have also released the C code on github. Enjoy.

See https://www.me.uk/puzzlebox for a custom maze SCAD.

P.S. Updated many times since I started on Christmas day. Always one extra tweak :-)

*I do not trust the download count - the file download count goes up way faster, so even counting "download all" as one download it is way off, and there are 50 files. It seems someone downloads every minute or two, and just as many access my web site to make custom mazes - amazing!

P.P.S. One more tweak - I added a mode to alternate "inside" and "outside" maze :-


P.P.P.S. One of the things I have done in this case (as you can see from the source) is make use of popt more than usual. Apart from just using as command line arguments, the same structure is then used to parse QUERY_STRING args, generate the comments in the header of the output, and even generate a web form. This means I can add new options in one place and not repeat myself.

P.P.P.P.S. I worked out a way to make the maze more challenging: The maze is drawn by a path from a point, taking random turns (with some bias) and back tracking when no options. I have now made it run multiple paths from the starting point concurrently so that there should be multiple reasonably long paths from that point, all but one of which will ultimately be a dead end.

No comments:

Post a Comment

Comments are moderated purely to filter out obvious spam, but it means they may not show immediately.