3D/AI - SDG 7 - Light Control with Distance Sensors (Difficulty: 2)
-
Key Topics Covered
- Initializing 3D scenes
- Using models
- Using cylinders and tubes
- Using lights
- Updating textures
- AI distance sensor
- Broadcasting messages
- Using boxes
- Updating scale
- 3D coordinates and positions
- Speeds of objects
- 3D rotations
- The follow camera
Introduction
The SDGs are 17 goals adopted by the United Nations in 2015. They are a to-do list for everyone to work on together.
This tutorial is about saving electricity, which is related to the 7th goal of “affordable and clean energy”.
In this 3D animation, we use a distance sensor at the entrance point to a tunnel to detect if any car is entering the tunnel, and automatically turn on the lights inside the tunnel:
Step 1 - An Empty Scene with the 3D Axis
Please create a new project, remove the “Cody” sprite, and rename the “Empty1” sprite as “Main”.
Add the “initialize 3D scene” block to load the “Empty” scene, and also add the 3D axis to show the 3 dimensions.
Step 2 - A Long Road on a Green Land
Next, let’s add a really long road (represented by a box) over a huge green land (represented by a plane). Note that the road will be 100 units wide.
It should look like this:
Step 3 - A Tunnel Tube
Next, add a tube to represent the tunnel. You need to rotate it to point along the Y axis, and move it forward to start at the origin point. Its diameter will also be 100 units, so the top half of it will cover the road exactly.
Here is the result you get from running each block:
Step 4 - A Darker Light
Next, let’s remove all the lights in the scene, then add a much darker light:
Now the tunnel looks much darker, so it will show a good contrast when we add lights inside it:
Step 5 - Add A Sensor Box Using a New Sprite
Now we can add a sensor box in front of the tunnel. It’s a good idea to create a new sprite to manage the sensor. It will be easier to find your code, and also all blocks in that sprite will be applied to the sensor.
First, broadcast a message from the “Main” sprite to “add sensor”:
Second, in the new sprite named “Sensor”, add a small box of size 5. Move it to Y position of -50, so it can be used to detect the car before it enters the tunnel.
The box will look like this:
Step 6 - Add the Distance Sensor To the Left
In the “Sensor” sprite, turn on the distance sensor on the box. Note that we have placed the box on the right side of the road, so we only need to test for objects running by its left side. Note the block is broken into 2 rows for better display:
You can see the sensor ray of length 60 that shoots out in the left direction of the box. Any car passing by will report a distance smaller than 60.
Step 7 - Check the Sensor Reading Periodically
To use the distance sensor, we just need to read its value repeatedly forever. Whenever we find the distance to any object on the left is less than 60, that means a car is driving by, so we need to broadcast a message to “turn on lights”:
Note that we can wait 0.5 seconds after each reading, so that we don’t read too many times. So long as it takes more than 0.5 seconds for the entire car to pa*s through our sensor, we will detect it. Of course, if you want to handle cars that run really fast, you can shorten this waiting time.
Step 8 - Add a Light in the Tunnel
Next, we will use another new sprite to add a light inside the tunnel.
First, broadcast another message from the “Main” sprite to “add lights”:
Next, add a new sprite called “Lights”. When it receives the message, add a spot light in the ceiling of the entrance point of the tunnel.We will be showing the cone that represents the light, but we need to scale its size down to 5%.
Also, since the spotlight will shoot forward by default, we need to rotate it 90 degrees around the X-axis to point downwards.
Here is a step-by-step illustration of the 3 blocks:
Step 9 - Add 4 Lights Using a For Loop
Now we have added one light successfully, let’s make 4 of them using a for-loop. We will use a new variable “i”, and make it go from 1 to 4. The 4 lights will be named “light1”, “light2”, “light3” and “light4”. Also, they will be moved to Y positions of 100, 200, 300 and 400. The variable “i” is used to calculate both the names and the
Y positions.
Now we get 4 lights inside the tunnel:
Step 10 - Switch the Lights Off and On
To save electricity, we would want to keep the lights in the tunnel off by default. This can be done by adding a new block to switch them off right after each light is added:
After that, whenever the sensor sends a message to “turn on lights”, we can go through each of the lights and switch them on:
Step 11 - Add the Car
Lastly, we just need to add a car. Again, it’s better to add it in a new sprite named “Car”.
First, broadcast a new message to “add car” from the “Main” sprite:
Second, add a new sprite named “Car”, and when it receives the “add car” message, add the car object. Let’s set the car to start at Y position of -200, so it will drive pa*s the sensor box. We can use a forward speed of 50, so that it doesn’t pa*s by the sensor box too fast:
Now we can observe how the car would turn on the lights as it drives by the sensor box:
Step 12 - Use a Follow Camera
To view the lighting effects from the car’s perspective, we can use a “follow camera” that follows the car from behind it. We just need to add this block in the “Car” sprite:
This is the view we get from behind the car:
Step 13 - Clean Up
Before we release our project, let’s clean it up by removing some unnecessary objects.
First, hide the 3D axis in the “Main” sprite:
Second, in the “Sensor” sprite, make the sensor box much smaller, since no one needs to see it:
Third, also in the “Sensor” sprite, make the sensor ray invisible:
Here is the final demo:
Creative Ideas
There are many ways you can extend this project with your own creative ideas. Here are some examples:
-
Switching the Lights Off: You can use another distance sensor at the exit point of the tunnel to turn off the lights when the car pa*ses by it.
-
More than one cars: If there are multiple cars, then you can not turn off the lights until all cars have exited the tunnel.
-
2-way Traffic: If there are cars driving on the 2 sides of the road in opposite directions, how would you change the sensors to handle that?
-