3D - Build Towers and Castles with Custom Block (Difficulty: 4)
-
Introduction
In Minecraft, everything is built manually using 3D cubes. Although this is very easy to use, it takes a lot of work to build a more complex structure.
On the CreatiCode playground, everything is built by running code blocks. This is a much more powerful tool since you can use the same code to build a lot of different things automatically.
In this tutorial, you will learn how to create a custom block that builds a tower like this:
And then, you will be able to design a much more complex castle using many such towers:
Step 1 - Start with an Empty Project
On the CreatiCode playground, log in to your account, then create a new project named “castle”. Remove the “Sprite1” with the dog, and we will write code in the “Empty1” sprite.
Step 2 - Initialize the ‘Grass Land’ Scene
As usual, we will start with a simple scene like ‘Grass Land’, and also add the 3D axis:
Step 3 - Define a New Block ‘Build Tower’
Now let’s define a custom block called ‘Build Tower’. It will build a single tower with a given size and name, so that we can reuse it to build many towers.
Define this block with 3 parameters:
- width: the edge length of the top face of the tower, which is a square shape
- height: the height of the main body of the tower
- name: the name of the main body of the tower, so that we can select this tower by its name later.
Note that you can customize your block’s color as well, using the “set my block’s color” button under “My Blocks”:
Step 4 - Use This New Block
We will now run this new block after initializing the scene, though it won’t do anything yet. We will set the width to 250, height to 200, and name it as “t1”.
Step 5 - Add the Main Body
Now let’s start implementing this new block. First, we will add a box of the given shape. Both the X size and Y size are the same as the “width” argument, and the Z size is the “height” argument. Its name is the “name” argument.
When we call this block using the arguments of 250/200/t1, the block above is the same as this:
If we run the program now, we will get a box with half of its body below the ground:
Step 6 - Bring the Main Body Above Ground
To make sure the entire body is above ground, we need to move it up. Since half its body is below ground, we just need to move it up by half of its height.
Note that we are calculating the Z position as height / 2, so no matter what’s the height argument, this block will always work correctly. In this example, the height of the box is 200, so half of it is 100. After we move the box to Z of 100, its top face will be at Z of 200, and the bottom face will be at Z of 0.Now our box will be standing on the ground correctly:
Step 7 - Apply a Texture
Now please pick a texture for the tower, such as “Floor01” or “Brick07”.
And the main body of the tower is ready:
Step 8 - The Base of the Battlement
On top of a tower, we need a structure called “battlement”. It is a circle of walls with openings.
[source: https://nobodysuspectsthebutterfly.tumblr.com/post/167238166461/castle-walls]
Since our tower has a square-shaped surface, we will add a square-shaped frame around the top of the tower. The “rectangle tube” would be perfect for this job. Its size in X and Y dimensions will be the same as the tower’s width, and it will only need to be 10 units tall. Its thickness will also be 10.
After adding it, we will see that it is centered at the bottom of the box, overlapping with it:
Step 9 - Move the Base to the Top
Now let’s move this base to the top of the box. We will need to do a simple calculation. The top face of the box is at Z of height, and the height of the base is 10. So we need to move the base’s position to Z = height + 5.
For example, if the tower box’s height is 200, then the base will be at Z of 205, so that its bottom is at Z of 200, and its top is at Z of 210.
Now we can clearly see the rectangle box we have added:
Step 10 - Apply the Same Texture
Next, duplicate the “update texture” block, so that this base has the same texture as the tower body:
This is how it looks:
Step 11 - Set the Tower Body as Parent
Now we need to set the tower body as the parent of the base of the battlement. The reason is that we may need to move the tower to a new location later, and we need all parts of the tower to move as well.
We are using the name argument as the name of the parent since that’s the name of the tower body. And the sprite name is “Empty1”, which is the current sprite.
Step 12 - Plan for the Merlons
The last part of our tower is to add the merlons on top of the base. They will be represented by many 10 by 10 cubes arranged in a square shape.
Although we can add them one by one, there is a much faster way to add all of them at once, using the “copy by matrix” block. This step is relatively complex, so here is a preview of what we will do:
- We will add an orange semi-transparent box called the “exclusion box”. It will only cover the center area of the tower, and leave the 4 edge areas open.
- We will then add one 10 by 10 cube at the center of the top of the tower. We will call this box the “seed box” since we will make many copies of it.
- We will use the “copy by matrix” block to copy the “seed box” in all 4 directions around it. However, we will also specify that we do not want any cube to be copied inside the “exclusion box”. As a result, the seed box will only be copied to the 4 edges.
- Lastly, we will remove the “exclusion box” and the “seed box”.
Here is an illustration of all 4 steps:
Step 13 - Add the Exclusion Box
Now let’s add the orange exclusion box. Its width will be 20 less than that of the tower body. This way, it will leave a stripe of width 10 on each side. We will name it “exclusion box”. Its Z position will be the height of the tower body.
It should look like this when you run it. Check if you get the open edge areas as shown.
Step 13 - Add the Exclusion Box
Next, we will add the “seed box”, which is a 10 by 10 by 10 in size. It has the same texture as the tower body. We will need to move it up above the tower body as well. Recall that the rectangle tube for the base of the battlement is “height + 5”. The merlons will be stacked above the base, so their Z position will be higher by 10 more, which is “height + 15”. For our example, the “height” is 200, and the base’s Z position is 205, then the seed box’s Z position is 215.
To see this seed box clearly, you have to zoom-in the camera so it is inside the orange box:
Step 14 - Set the Tower Body as the Parent
Before we make many copies of the “seed box”, we need to set the tower body as its parent. This way, all the copies of it will also have the tower body as their parent, and when we move the tower body, all of these copied cubes will move the same way.
Step 15 - Copy by Matrix from Center
We are finally ready to use the “copy by matrix” block. It will use the “seed box” as the center of a matrix, and make copies of it in both the X and Y directions we specify.
To help you visualize this process, take a look at the illustration below. Suppose we are not excluding any area for now. The seed box is at the center, and we will need to expand it by 6 rows or columns in all 4 directions. The distance between 2 boxes is 20 units, which ensures the width of the opening between 2 boxes is 10:
So how do we derive the number 6 from the width of 250? We can calculate it in 3 steps:- The total width of the tower body is 250, so the distance between the box on the left and right edge is 250 - 10 = 240
- The distance between the seed box at the center and the box on the right edge is half, which is 240 / 2 = 120
- The distance between 2 copies is 20, so there are 120 / 20 = 6 copies.
- So the overall calculation is ((width - 10) / 2) / 20 = (width - 10) / 40
With that, here is how we use the “copy by matrix” block:
Specifically, the key inputs are:- Count in X and Y directions: (width-10)/40
- Count in Z direction: 0 since we only want one layer of copies
- Spacing in X and Y directions: 20 between neighbor copies
- Exclusion: we only want to add copies if it is outside the exclusion box.
After this block runs, we will see copies around the 4 edges of the tower’s top:
Note: since the number of copies in each direction is calculated as (width-10)/40, that means the width argument has to be multiple of 40 plus 10, such as 50, 90, 130, etc.
Step 16 - Remove the Exclusion Box
To clean up, we will remove the exclusion box. Also, we will set it to fully transparent, so it will never be seen as the tower is created:
Step 17 - Select the Tower Body at the End
Now our tower is complete. We will select the tower body by its name again. That way, after this tower is built, we can immediately move it to another location.
Step 18 - Set the Exclusion Box Transparent
Now our code is working, we no longer need to show the exclusion box. We can set it to 100% transparent so the user won’t see it when the tower is built:
Step 19 - Set the Exclusion Box Transparent
Currently, the shape of the tower is not easy to see unless the camera is very close. We can improve it by changing the lights in the scene.
We will broadcast a new message of “lights” before the tower is added:
Then, in a new sprite called “Lights”, we will handle this message. We will first remove the existing light in the scene, then add 2 directional lights in 2 different directions. Lastly, we add an ambient light that’s brighter than normal (150 brightness):
Now let’s try to run it again, and you will find the tower’s structure is clearly shown:
Step 20 - Try to Create Towers of Different Sizes
Now, we can play with our new block and create all kinds of towers. Note that the width has to be multiple of 40 plus 10. For example, we can add 2 more towers and overlay them together:
This is the result:
Step 21 - Copy to Mirror Positions
The towers can also be added at corners of other towers, and we can use the “copy to mirror position” block to quickly duplicate a tower to all corners.
For example, the program below adds the “t4” tower at x of -125 and y of -125, then it copies this tower to the other 3 corners:
Here is the final result:
Creative Ideas
Now, please try to extend this project to build your own castle. As shown above, use the tower structure as a building block and combine many of them.
For another interesting direction, try to create a similar structure of “wall” based on the tower structure. Then you can build new scenes using both walls and towers.
-