Big Bad Beetle Blocks

A Scratch-like language for 3D

By

Beetle Blocks is an interesting twist on the “block-building programming language” idea. Instead of a flat, boring plane, you move a beetle around 3D space, drawing cubes, spheres and all sorts of other shapes.

Beetle Blocks [1] lets you build 3D objects by ordering the Beetle Blocks beetle around. You do this in true Scratch-like fashion by dropping blocks containing commands such as movego to and rotate onto the workspace. You can then chain several blocks together to build programs, just like in any block-based program (Figure 1).

Figure 1: Follow the Beetle Blocks beetle around in 3D space to draw objects.

The difference here is that with Beetle Blocks everything happens in 3D. In Scratch, for instance, you can turn the kitty sprite on only one axis only – it's yaw axis (see Figure 2 on the next page). In Beetle Blocks on the other hand you can rotate the beetle, three axes, on its yaw, pitch and roll axes, using a block like this:

Figure 2: You can rotate an object on any of these axes in 3D space.

Likewise, while you only needed two coordinates, x and y, to place your sprites in Scratch, Beetle Blocks needs three to position your beetle: xy and z.

You can also decide to draw lines and curves, or extrude them. This in practice means that as the beetle walks it you can have it leave a 3D sausage-like trail behind it.

Beetle Pi

Despite what the main website says, Beetle Blocks works fine in Firefox. Sadly, it does not work in the Pi's default browser Chromium ; the editor site won't even load. So, if you haven't got it yet, go grab yourself Firefox for your Pi with the command:

sudo apt-get install firefox-esr

Firefox will install in the Internet submenu. Open it and go to the online editor [2].

Wait for the interface to completely appear, including the little picture of the cyan-colored beetle, before you start working. Otherwise, the interface may freeze for a few seconds.

Dragging and dropping blocks onto the workspace is generally fine on a Raspberry Pi 3, although sometimes it can be sluggish. Performance is better if you use openSUSE (included in this month's DVD) instead of Raspbian. This may be because openSUSE, being a true 64-bit operating system, makes the most of the Pi's hardware.

If you want to work offline, you can download all the Beetle Blocks framework from the project's GitHub with:

git clone https://github.com/ericrosenbaum/BeetleBlocks

Open Firefox, press [Ctrl]+[O], and navigate to the directory where you downloaded Beetle Blocks. Inside the run/ subdirectory, click on the index.html file, and a local editor will open in your browser.

Beetle Server

Beetle Blocks is written in pure JavaScript, so doesn't need a server or any external dependencies to run. However, if you want other people such as students in your classroom to access the Beetle Blocks web page, you can run your own basic web server locally.

The web server lighttpd is a good choice since, as it's name suggests, it's very light and will not overload your Pi. Besides, for serving a bunch of JavaScript it's all you'll really need.

Open a terminal window and type:

$ sudo apt-get install lighttpd

Once installation is complete, the server will launch automatically. You can check by opening Firefox on your Pi and navigating to 127.0.0.1 (or, from another computer, put your Pi's IP into a web browser). If you see something like Figure 3, everything is fine.

Figure 3: With lighttpd you'll be able to access a local version of Beetle Blocks from anywhere.

Next copy other the contents of Beetle Blocks' run/ subdirectory to a subdirectory under /var/www/html/ like so:

$ sudo mkdir /var/www/html/bb
$ cd /download/directory/BeetleBlocks
$ sudo cp -Rv run/* /var/www/bb/html/

Now, using a web browser on another computer, go to http://<RPi's IP>/bb (Figure 4).

Figure 4: You can run Beetle Blocks from a server so several people can make their own 3D designs at the same time.

Most of the code for Beetle Blocks runs in the browser anyway, so loading the program from a Pi for an entire classroom is quite doable.

Basic Shapes

One of the cool things you can do with Beetle Blocks is draw a shape at the beetle's position. Open the Shapes category and drag out…

… then click on it. The beetle will draw a sphere. This may not sound like much, but think of the applications: this is probably the easiest way to draw and visualize 3D objects.

Take for example, the code, in Listing 1. It uses this technique of moving the beetle and drawing an object to generate a complex 3D function. It essentially consists of a loop within a loop, the innermost counts from -10 to 10 for y the position of the beetle.

Box 1: cos(y) * sin(x)

The outer does the same for the x position. z, the vertical position of the beetle, is the result of multiplying the cosine of y by the sine of x. You then use a go to block with your xy and z values to simply draw a sphere.

This generates the rather marvelous-looking 3D graph shown in Figure 5.

Figure 5: The cos(y) * sin(x) function shown in 3D.

The other thing you can do is extrude. This makes the beetle leave a 3D trail as it "walks". The program in Listing 2 shows how to do this and builds the "sausage pyramid" seen in Figure 6.

Box 2: sausage pyramid

Figure 6: The rather unsettling "sausage pyramid" is created using Beetle Blocks' extrude feature.

Your own shapes

Beetle Blocks is based on Snap! [3] and, as we explained in issue 6 [4], this means you can combine blocks to build your own instructions. Right click on an empty region in the central workspace and a menu will pop-up. Choose Make a block and Beetle Blocks will then bring up a wizard which will allow you to create your own by combining several basic blocks. This means you can use the block editor to create more and more complex shapes from basic shapes and embed them in your own blocks to simplify your programs.

At some stage you will want to save your programs permanently to your hard disk. You can do this by clicking on the file icon in the upper toolbar and selecting Download project as…. You can then download your work as an XML file. You can open your program later in any other instance of Beetle Blocks by choosing Import project or blocks from the File menu. The XML file also saves all the custom blocks you may have created for the project.

You can also extract your custom blocks from the project and save them independently to re-use them later in other projects. Click on the File icon at the top of the screen and this time choose Download My Blocks as…. This will open a window showing the custom blocks you created for the project. You can then mark the ones you want to save. Import your blocks into a new project by selecting Importing project and blocks… from the File menu and the navigating to where you saved the XML file containing the blocks.

Another interesting entry within the File menu is the option to export your 3D object as an STL file. Click on File and go to Download 3D model as… and pick STLSTL is quickly becoming the standard file format used to exchange 3D objects between different applications (Figure 7). This has an exciting side effect; whatever you tell the beetle to draw can later be turned into a physical object thanks to the magic of home 3D printing.

Figure 7: A torus knot, created with Beetle Blocks, edited in Blender.

Conclusion

Beetle Blocks is fun, easy and 3D. Anyone familiar with Scratch will be able produce complex shapes and visualize 3D functions in minutes. It works well on the Raspberry Pi and you can even get real, physical objects from what you design thanks to its export to STL feature. store.

Info

  1. Beetle Blocks: http://beetleblocks.com
  2. Beetle Blocks online editor: http://beetleblocks.com/run/
  3. Snap!: https://snap.berkeley.edu/
  4. A Web-based Alternative to Scratch, Issue 6: http://www.raspberry-pi-geek.com/Archive/2014/06/A-web-based-alternative-to-Scratch

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News