3D - A Self-Driving Car with Distance Sensors (Difficulty: 3)
-
Key Topics Covered
- Initializing 3D scenes
- Updating textures
- Using boxes
- Using models
- Speeds of objects
- AI distance sensor
- Broadcasting messages
- 3D rotations
- The follow camera
- Copying objects to positions in a matrix
Introduction
In this tutorial, we’ll install a “distance sensor” on a car, so that it can avoid obstacles automatically.
Step 1 - Initialize the “Grass Land” Scene
Please create a new project, and use the “initialize 3D scene” block to load the “Grass Land” scene. Please also add the 3D axis to show the 3 dimensions.
Step 2 - Add a New Sprite “Obstacles”
Please add a new sprite, and name it as “Obstacles”.
In the initial sprite, broadcast a message to “add obstacles”:
In the new sprite “Obstacles”, when it receives the message, please add a box of size 100, and move it to y position of 500 and z position of 50. Feel free to update its texture as well:
Now we get a box like this:
Step 3 - Add a Car
In the main sprite, please add a car model with a height of 60. Feel free to update its color as well.
The car will be placed at the center of the scene, facing the box in front of it:
Step 4 - Drive the Car Forward
Next, let’s drive the car forward by setting its “forward speed” to 100.
The car will move forward as expected, and it will go through the box.
Step 5 - Add a Follow Camera
As the car moves around, it will run out of our camera’s view soon. We can add a “follow camera” that follows the car as it moves or turns.
Note that you need to set the direction to “Target”, so that the camera will always face the same direction as the car. Also, the “60%” see-through rate will make obstacles semi-transparent when they block the car.
You should be able to keep the camera behind the car like this now:
Step 6 - Turn on the Front Distance Sensor
Next, we are going to add the front distance sensor to the car, so that it can find out how far is the obstacle if any.
As shown, we will only need to turn on the “front” sensor, and let’s set it to be visible for debugging purposes for now. Since the car’s height is 60, let’s set the “z offset” to 30. We will also only use one sensor ray for now.
You will be able to see the distance sensor ray in the front now:
Step 7 - Stop the Car If It is Too Close to Obstacle
Now let’s try to test if the distance sensor is working properly. We can use a forever loop to keep checking the distance to the obstacle in the front, and make the car stop when the distance is less than 250.
As expected, the car will stop after moving forward a bit:
Step 8 - Turn Left to Dodge the Obstacle
Instead of stopping the car, we can keep the car going forward, but make it turn left to dodge the obstacle.
To do that, we just need to remove the block to set “forward speed” to 0, and replace it with the “turn left 3 degrees” block.
As shown, the car will start to turn left when its distance to the box is less than 250, and it will keep turning left until the sensor can no longer detect the box:
Step 9 - Switch to the 5-Ray Distance Sensor
There is still a problem with our solution. The car will cut through the box. The reason is that although the sensor ray in the middle is no longer hitting the box, the right half of the car is still blocked by the box.
To fix this problem, we can switch to using 5 distance sensor rays instead of 1. This way, the sensors on the right corner of the car will hit the box, which will tell us there is still an obstacle.
Now our car will keep turning left until all 5 rays are clear from the obstacle:
Step 10 - A Second Obstacle
Our code should work for any number of obstacles now. To test it, please go to the “Obstacles” sprite, and add another obstacle. Note that we can just make a copy of the first box, which will be faster than adding a new one.
As shown, our car can avoid both boxes:
Step 11 - Many More Obstacles
Now let’s try to add a lot more obstacles, using the “copy by matrix” block.
In the “Obstacles” sprite, we can copy the box to a matrix of 20 rows and 20 columns along the X and Y directions. The distance between the boxes will be 500 in both X and Y directions. Some randomness can be added for the spacing/scaling/direction of the copies.
We also need to move the initial box to the left-bottom corner of the scene (negative X and Y positions), so that the car will start at the center of the obstacles.
Here is what it looks like from above, with the initial box on the bottom left and the car in the center.
When we run the program, the car will be able to navigate through all the obstacles:
Creative Ideas
There are many ways you can extend this project. Here are some ideas for your inspiration:
-
Different Shapes for the Obstacles: you can try other shapes like spheres or cylinders, or even models. The distance sensor should work for all kinds of obstacles
-
Turn left and right: currently the car always turns left to avoid an obstacle. Can you try to make it turn left or right randomly?
-
Slow Down and Turn: currently the car may still run into an obstacle if it can not avoid it in time. We can change it to slow down when it detects an obstacle, and resume the normal speed when there is no obstacle.
-
3-dimensional Maze: you can also change the vehicle to an airplane, and make it fly through a maze of obstacles in the sky. The airplane will be able to rise or fall to avoid obstacles as well.
-