Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. 3D - Rubik's Cube (Difficulty: 2)

3D - Rubik's Cube (Difficulty: 2)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Key Topics Covered

    • Initializing 3D scenes
    • Using variables
    • Using for loops
    • Using boxes
    • Print to console
    • 3D coordinates and positions

     
     

    Introduction

    In this tutorial, we’ll build a Rubik’s cube. Here is a preview:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/17d532aa-56a3-46e1-aba8-b2ac733e4b49.gif" width="440" style="border-radius: 5px; border: 1px solid #29622d;">

     

    Step 1 - An “Empty” Scene with 3D Axes

    Please create a new project, remove the Sprite1, and add these blocks in the “Empty1” sprite. You can find the “initialize 3D scene” and the “show 3D axis” blocks in the “3D Scene” category.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/43a6f24e-b195-4762-a219-822a46559d96.png" width="400" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
     
    As shown, the X-axis points to the right, the Y-axis points forward (into the screen), and the Z-axis points up. You can drag your mouse on the stage to rotate the camera around it:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dcebc38b-cc87-4eb8-a3f1-6252211315eb.gif" width="460" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 2 - Add A Box

    It’s time to add the first box of your Rubik’s cube. You can find the “add 6-colored box” block in the “3D Object” category, and then update the color of the 6 faces to anything you like. You can keep the size in all 3 dimensions at 100 for now.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a2557825-a946-4afd-9f59-d2bd0e944f79.png" width="800" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
     
    The box will be added at the position of (x=0, y=0, z=0), which means its center point will be at the root of the 3D axis. You can check it by zooming the camera into the box.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1ded3803-ef22-4fb2-be4d-c6344801a3fb.gif" width="400" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 3 - Add Another Box to the Right

    Now let’s add another box. This time we would need to move it to a different position, so that it does not overlap with the first box.

    As shown, you can first duplicate the “add 6-colored box” block, then set the new box’s X position to 100.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/d42676b0-7955-45b7-8f37-63de85958504.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">
     
    Note that although we have 2 boxes, they look like one wider box because there is no gap between them.

     
     

    Step 4 - Reduce the Size of the Boxes

    To show a small gap between the boxes, we can either reduce the box size, or increase the distance between the boxes. For example, we can reduce the box size from 100 to 98:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1b43923a-b75d-443e-a445-aa98be2544e8.png" width="600" style="">
     
    Now we get a nice gap between the 2 boxes:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/20c8ca23-c442-463e-88f8-f344cf0429fe.gif" width="400" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 5 - Add the Third Box

    The third box will be very similar to the second box, except it needs to be placed on the left, with an X position of -100:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b56a585b-bf35-41d1-9645-6613c4193606.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 6 - Use A For-Loop

    Although it is pretty easy to duplicate the blocks for more boxes, there are faster ways to write the program. For example, we can use a “for-loop”.

    You need to define a new variable “x”, and use the for-loop to make x go from -100 to 100, at a step size of 100. This way, x will start as -100, then change to 0, then 100.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cd6e78b8-9a7b-4d39-9e0e-ef5cdba8717c.gif" width="850" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Now we can reorganize our program to use the variable x to create and move boxes:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6a3882b1-e033-4811-82b8-96686035b3e1.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

    Now we get the same result, but the program is much easier to change. For example, to change the box color and size, you only need to do it in one block:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/38d35758-b404-41b6-b7f5-8c070c56cda6.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 7 - 3 Rows of Boxes

    Now we have worked out one row of 3 boxes, we will work on 3 rows (9 boxes) next. These rows will be positioned in different Y positions: -100, 0 and 100.

    You need to define a new variable “y”, and add a new for-loop to set y to -100, 0 and 100:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/82902131-7ea8-46a8-bf06-304d6d97f3b1.png" width="400" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
     
    Now we can place the new for-loop outside the existing for-loop, and move the boxes using both the x and the y variables:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9186bc3d-2295-49fd-8cbf-f88615d701c0.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 8 - Print out the X and Y Variables

    Since the program runs very quickly, it might be hard to grasp what’s happening. You can use a very handy tool to help you understand the sequence of events: printing logs.

    To do that, add the “print ( ) to console” block inside your for-loop, and then add a “join” block to read out the x and y variables.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4ee81ec0-7b2e-4097-93b3-a5eee38582f4.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 9 - 3 Layers of Boxes

    For the last step, we are going to create 3 layers of boxes (27 of them). They will be stacked vertically, so their Z positions will be -100, 0 and 100.

    The change is very similar, as we just need another for-loop for a new variable “z”. To move the box to a Z position, you need to use the “set z” block.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5940116b-c453-4686-b4ac-a5b98fa4210f.png" width="700" style="">

     
     

    Step 10 - 5 by 5 by 5

    Because we are using for-loops, it is very easy to change the range of the variables. For example, we can make the 3 variables go from -200 to 200, so each of them will take on 5 values (-200, -100, 0, 100, 200).

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/35cc8138-94d9-4732-85a3-808ca907350c.png" width="700" style="">
     
    Here is the 5-layer cube:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/17d532aa-56a3-46e1-aba8-b2ac733e4b49.gif" width="440" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Creative Ideas

    There are many ways you can enhance this basic cube project. Here are some ideas for your inspiration:

    1. Other Shapes: a Rubik’s cube doesn’t have to use boxes, right? You can try some other shapes, like spheres or cylinders.

    2. Random Rotations: each box can be facing a different direction randomly. You can use the “rotate to” block to make some random rotations when you add each box.

    3. Other Shapes: besides cubes, you can also use for-loops to stack the boxes in other ways, such as a pyramid or a Christmas tree.

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode