Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 3D Blocks
  4. Using Distance Sensors

Using Distance Sensors

Scheduled Pinned Locked Moved 3D Blocks
1 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Prerequisite

    • Speed of 3D Objects

     

    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:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ab28b65a-e616-4a41-bfd4-9f7b396082cd.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

    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.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/587a725c-6ff5-4cdf-b4f8-5a808da3a724.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

    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.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3bfce61a-d3d6-420a-98f4-d8903c500234.gif" width="450" style="border-radius: 5px; border: 1px solid #29622d;">

     

    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:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2a1df7d9-9bc4-4171-b173-4ccd24e00f57.png" width="1000" style="">

    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.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c204c99e-d573-48a0-bd7c-735b4a9a030c.gif" width="470" style="margin-left: 40px; border-radius: 5px; border: 1px solid #29622d;">

    • 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:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fd1f270a-86c2-4eac-ba4d-af095c79344e.png" width="500" style="">

    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:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/8f2c07d4-fbda-4b09-b1a7-7e6af38b15bc.png" width="500" style="">

    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.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/acad1f05-0002-4426-b53c-cc60eff2081b.png" width="1000" style="">

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a79bc623-3d66-4254-95ba-a0dbf1226b18.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

    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.

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode