三维 - 带距离感应器的自动驾驶汽车(难度:3级)
-
用到的相关知识
介绍
在本篇教程中,我们将在汽车上安装“距离感应器”,汽车就能自动躲避障碍物。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b9d233f4-9c99-4562-b29a-b59a9edf866f.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第1步 - 创建三维场景“草地”
新建一个项目,使用积木“创建三维场景”载入“草地”场景。还要添加三维坐标系以显示三维坐标轴。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/607dfe7b-cfc2-413d-83f9-150ce940d5e5.png" width="300" style="">
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0c482764-6bd0-42d2-8041-67ad1fc0a44b.gif" width="480" style="">
第2步 - 添加一个新角色“障碍物”
添加一个新角色,命名为“障碍物”。
在主角色中,广播一条消息来“添加障碍物”:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5fcf8497-f3c5-4389-8238-06dde24d7ca3.png" width="350" style="">
在新角色“障碍物”中,当收到广播消息,就添加一个盒子,大小为100,然后移动至位置——y坐标:500,z坐标:50。大家可随意更换其纹理:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0e36bd55-8be5-4fa7-84b3-b6947c17e27d.png" width="670" style="">
现在就得到这样一个盒子:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b7ee2ebf-7467-4eae-9f74-f5293c2e487f.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第3步 - 添加汽车
在主角色中,添加一个汽车模型,高度为60。大家可随意更改其颜色。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c03ad5a7-dafe-44c7-b417-aed6112617ef.png" width="900" style="">
汽车被放在场景中央,面对前方的盒子:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9fbbc31e-956b-40ce-89ad-b98852f5e24a.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第4步 - 向前开车
接下来,我们为车设置“前进速度”100。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/47a5ad97-b2cf-42d1-bce4-7cf490b48d69.png" width="400" style="">
汽车就会按照预期向前行驶,并且会穿过盒子。https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a14ddce4-41ce-4de8-b8fa-6634f8d2406e.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第5步 - 添加跟踪摄像机
当汽车到处行驶,很快就走出摄像机的视野。我们可以添加一个“追踪摄像头”,当汽车行驶和转向时就会跟踪汽车。
注意:你需要把方向设置为“目标”,以便于摄像机总是与汽车面向同一方向。另外,“透视度”设为60%,当障碍物遮挡住汽车时,障碍物就变成半透明。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7dee58fa-ab60-4655-9774-39b9109bde23.png" width="1000" style="">
现在应该就能使摄像机跟踪汽车,如下所示:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a103f8a1-159b-4d60-bc1f-3d42aca22a1d.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第6步 - 打开前方距离感应器
接下来,我们要为汽车添加前方距离感应器,以便于让汽车能发现前方的障碍物有多远。
如下图所示,我们只需开启“前”感应器。现在为了调试用途,将其设为可见。由于汽车高度为60,我们把“z位移”设为30。此外,现在我们只用一个感应器。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b1e713d9-5639-45e1-a1ae-5ff485fb0356.png" width="1100" style="">
现在就能看到前方的距离感应器光束了:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/107c95e0-f78f-4b1d-842d-2e16301752b3.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第7步 - 当汽车离障碍物太近就停下
现在我们测试一下距离感应器是否管用。我们可以用一个“重复执行”循环不断检查与前方障碍物的距离,当距离小于250时就停下来。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e9efabad-96cd-4193-b3f1-cab9e2e60430.png" width="600" style="">
果然,汽车向前行驶一段距离后停了下来:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/62e7b72a-2a4c-48ee-be83-6259001b0cd7.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第8步 - 向左转躲开障碍物
我们不让汽车停止,而是让汽车左转躲开障碍物。
做法为,设置“前进速度”为0的积木删除,换成积木“左转3度”。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/399d67c2-0c11-4123-95af-b4bc059b1aca.png" width="600" style="">
如下图所示,汽车距离盒子小于250时就开始左转,会一直转到感应器检测不到盒子:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b568e648-bb35-4c23-86ff-de48e7621055.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第9步 - 换成5个距离感应器
我们的方案仍然有个问题。汽车会斜着撞到盒子上,原因就是,尽管中间的感应器光束碰不到盒子,汽车右半部分仍然会撞到盒子。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/515bd71e-5bcf-4838-9583-a2155124e789.png" width="400" style="border-radius: 5px; border: 1px solid #29622d;">
为解决这个问题,我们把1个距离感应器换成5个。这样的话,汽车右前角的感应器光束会碰到盒子,告诉我们仍然有障碍物。https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0bb94062-2390-4684-ba72-1999128dbcd3.png" width="1100" style="">
现在汽车会一直左转到5根光束都碰不到障碍物:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c738a3ef-0167-4f34-9c25-9b15b7f3b858.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第10步 - 第二个障碍物
现在我们的代码应该能适用于任意数量的障碍物。测试方法为,进入“障碍物”角色,再添加一个障碍物。提示,大家只需要复制第一个盒子,比重新添加要快得多。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5d10fa5b-cecc-4192-a4dd-ce047764f45f.png" width="700" style="">
如下所示,汽车可以躲开这两个盒子:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/59ddcc48-45d4-4f81-9b96-8d63bc9c1627.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
第11步 - 更多一些障碍物
现在我们尝试添加更多障碍物,利用积木“矩阵复制”。
在“障碍物”角色,我们可以将盒子沿x轴、y轴方向矩阵复制20行、20列。盒子在x轴、y轴方向的间隔距离均为500。为复制的盒子设置一定程度的随机位置、随机大小和随机方向。
我们还要把初始的盒子移到场景的左下角(x坐标、y坐标均为负值),这样才能让汽车从障碍物中间开始出发。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b493847f-85cf-40cd-82f0-b1e300658351.png" width="1100" style="">
从上方看去如下图所示,初始的盒子在左下角,汽车在最中央。
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a828c892-9b71-4509-83ba-a1487e473db0.png" width="800" style="border-radius: 5px; border: 1px solid #29622d;">
当我们运行程序,汽车能躲开所有障碍物:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/86eea00e-027f-4cc8-8303-f17496f869bd.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">
创意扩展
大家可以把这个项目扩展成多种形式。以下是一些创意想法:
-
不同形状的障碍物:大家可以尝试用球形、圆柱形,甚至是模型。距离感应器适用于各种障碍物。
-
左右转向:目前汽车总是左转来躲开障碍物。大家能不能试试左右随机转向?
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/39baf084-02e4-44e4-a11e-4fe0461d12a7.gif" width="470" style="margin-left: 40px; border-radius: 5px; border: 1px solid #29622d;">
-
先减速再转向:目前汽车如果躲闪不及,仍然会撞到障碍物。我们可以改为检测到障碍物时先减速,躲过障碍物后再恢复正常速度。
-
三维迷宫:大家还可以把汽车改为飞机,让飞机在空中飞过障碍物迷宫。飞机要能够上升、下降来躲避障碍物。
-
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