Using Distance Sensors
-
Prerequisite
Introduction
A “distance sensor” can tell us the distance to another object, then we can use this information to make decisions automatically. For example, if a car is very close to a person or a wall in front of it, then we should stop the car.
In the CreatiCode playground, the sensors are represented by “rays” that emit out of the object:
For example, when the ray in the front is touching another object, we will be able to calculate the distance between the point of contact and the car’s center. Note that the distance is not between the obstacle’s center and the car’s center.
Note that the distance sensor depends on the sensor rays entirely. If the ray is not touching any object, then the distance sensor will not “see” that object, so it will not report the distance to that object. For example, it will not report its distance to the ring in front of it, because the sensor ray is not touching any part of the ring.
The “Set Distance Sensor” Block
You can install distance sensors on any object, such as boxes, cars or avatars. To turn on or off distance sensors to an object, you use the following block:
Parameters
-
front/back/left/right/down/up: you can use these 6 dropdowns to specify which directions to install the distance sensor. Since each sensor will require extra work, you should only switch on a sensor if you really need it. For example, if you only need to know the distance to obstacles in the front and back, then you should only turn on those 2 sensors.
-
z offset: by default, the center of the rays will be at the bottom of the object. You can move the center up using the “z offset” input. Usually, you should set this value to be half of the object height. For example, if the car’s height is 100, then you should set the “z offset” to 50.
-
max distance: this value controls the length of the rays. If this value is 500, then it means the rays will not touch any other object that is more than 500 units away from the object.
-
count: this dropdown allows you to specify how many rays to install in each direction. If you choose “five”, then there will be 5 rays in each direction, which will make it more likely to detect smaller objects.
- visible: the last dropdown controls whether you want to display the sensor rays. Usually, you should set the rays to visible when you are still working on the program, and then hide them when you are ready to share your project.
The “Distance to Nearest Obstacle” Block
After turning on the sensors, you can read their values using the following reporter block:
Note that if there are multiple obstacles in a direction, this block will only report the distance to the nearest obstacle. When there are no obstacles, a value of 100000000 will be reported.
Also, the object has to be moving at a non-zero speed. Otherwise, the distance sensor will not report any value.
The “Name of Nearest Obstacle” Block
You can also find out the name of the nearest obstacle in any of the 6 directions with this reporter block:
When there is no obstacle detected, this block will report a value of “none”.
Example
In this example, the police car is moving forward towards the box (the obstacle), and we repeatedly set the label widget to the name and distance of the obstacle.
Note that the distance sensor does not actually stop the car from crushing the obstacle. It is only a sensor for telling us the distance to the obstacle. You will still need to add additional code to make the car stop.
-