22 Feb 2023, 04:45

Introduction

When you use a predefined 3D scene such as “city” or “castle”, it is a common problem that the player avatar may run through walls of buildings or other models.

In this tutorial, you will learn how to prevent this from happening.

 
 

Step 1 - Remix the Starting Project

A starting project has been prepared for you. It loads the "city’ scene, adds an avatar, and then moves/turns the avatar based on key press events. Please open this project in the CreatiCode playground and remix it to your own copy.

https://play.creaticode.com/projects/be54a401230d6f874cbc9aaf

 
 

Step 2 - Add a Wall Sprite

To prevent the avatar from walking through an object, the easiest way is to use a simple shape (such as a box or a cylinder) to cover up that object, then set this shape to block the avatar. To get started, create a new sprite named “walls”, and add the following blocks to create a box in front of the avatar:

70b4bb37-f306-44d2-8da0-abd962b81d04-image.png

 
You will see this big purple wall when you click on these 3 blocks to run them alone:

0375e927-5ef7-4fb1-b26f-3585559cfd97-image.png

 
 

Step 3 - Block the Avatar with the Walls

Now switch back to the avatar sprite, and insert these 2 blocks. They will send the “add walls” message, and also make any object created in the “walls” sprite block the avatar.

8098be9a-7d1e-4162-a68f-dac0d0e679f8-image.png

 
Now if you run the project again, you will not be able to move the avatar through the purple wall.

e4.gif

 
 

Step 4 - Make the Purple Wall Transparent

Now let’s switch to the “walls” sprite, and change the color of the purple box to 100% transparent:

e5.gif

 
Now when you restart the program, the avatar will still be blocked by that box, but we won’t see it anymore. It will look as if the avatar is blocked by the building or the road.

e7.gif

 
 

Step 5 - Add a Marker Box

Now you know how to prevent the avatar from running into any building, we just need to add more boxes in the “walls” sprite to cover up more buildings. Therefore, the only question left is how to find out the size and position of these wall boxes. Although we can find out by trial and error, it is much easier if we use a “marker" box.

To get started, let’s go to the “avatar” sprite, detach the blocks for adding the avatar, and add a new yellow box.

f76f2d9a-ab39-4006-8593-9f1f33b825df-image.png

 
It should look like this:

ye.gif

 
 

Step 6 - Turn on Mouse Picking

Next, we want to turn on mouse picking for the city scene, so that we can click anywhere in the scene to find out its position. In addition, in the “3D Action” category, check the checkbox for these 3 reporter blocks “picked point x pos”, “picked point y pos” and “picked point z pos”, so that they are shown on the stage. As we click on different parts of the scene, we get the exact x/y/z positions of those points.

e8.gif

 
 

Step 7 - Move the Marker Box to the Picked Point

To make it easier to view and adjust the point we have picked, let’s move the marker box to the picked position whenever we make a new pick:

e6c0d065-b0bc-41a3-a374-50da59211c92-image.png

 
You can try to click on different points to move the marker box:

e10.gif

 
 

Step 8 - Use the Marker Box to Find the Corner Points of Buildings

Now we are ready to take some measurements. The basic idea is to place the marker box at 2 diagonal corners of a building (or a group of buildings) you need to cover up, and write down the x and y positions of them. We will use these numbers to calculate the size and position of the wall box in the next step.

For example, suppose we want to cover up the street block with 3 buildings in front of the avatar. Zoom out the camera, and shift it to look down at this street block. Then click on the bottom left corner of this street block, which gives us the x position of -1582 and y position of 417. Similarly, click on the top right corner of this street block, which gives us the x position of 2140 and the y position of 1433.

f2.gif

 
 

Step 9 - Calculate the Wall Box’s Size and Position

Now let’s switch to the “walls” sprite, and update the size and position of the wall box.

For its size in the x direction, we can use the difference between the 2 corner points’ x positions of -1582 and 2140, which give us 3722. Similarly, the length of the box in the y direction is the difference between 417 and 1433, which is 1016.

For the wall’s x position, it should be the average of the 2 corner points’ x positions: the average of -1582 and 2140 is 279. Similarly, for the y position, the average of 417 and 1433 is 925.

Therefore, we can put these 4 new numbers into our program. For debugging purposes, we can set the box’s transparency back to 0 temporarily.

18fe1ebc-886b-4e17-91ba-f6b5034f006a-image.png

 
Now you should get a wall box that properly covers up this street block:

f3.gif

 
Note that we don’t need to make the wall box too tall. So long as it is taller than the avatar’s jumping height, we know the avatar won’t pass through it.

 
 

Step 10 - Add More Wall Boxes

For the remaining work, you just need to add more boxes in the “walls” sprite using the same measurement and calculation methods. If each street block takes you 1 minute, you will be done with the entire “city” scene within 20 minutes.

After you have added all these wall boxes, make sure they are reset back to fully transparent, and remove the code blocks for the marker box and the picking event in the “avatar” sprite.

 
 

Practice

For some more practice with this technique, you can try to apply it to the “castle” scene as well. In addition, you can try to use different shapes of wall boxes, such as cylinders.