主要内容

Car in the Mountains

此示例说明了Simulink®3DAnimation™Matlab®接口的使用。万博1manbetx在分步教程中,它显示了查询和操纵虚拟世界对象的命令。您将了解虚拟世界结构,然后沿着山区的道路浏览虚拟汽车。

在此示例中,我们将向您展示如何使用面向MATLAB对象的接口在虚拟世界中控制对象。

创建世界对象

我们首先创建代表虚拟世界的类​​VRWorld的对象。构成世界的VRML文件以前是使用Simulink 3D动画产品中包含的3D世界编辑器制成的。万博1manbetx该文件的名称为vrmount.x3d。

world = vrworld('vrmount.x3d');

开放并查看世界

The world must be opened before it can be used. This is accomplished using the OPEN command.

开放(世界);

可以在VRML查看器中查看虚拟世界。万博1manbetxSimulink 3D动画提供了两种查看虚拟世界的方法 - 内部查看器(默认方法)和外部查看器(与Web浏览器集成)。我们将使用视图功能在内部查看器中查看虚拟世界。观众可能需要一些时间,所以请耐心等待。

fig = view(world,'-内部的');vrdrawnow;

Examine the Virtual World Properties

您可以使用GET命令检查虚拟世界的属性。请注意,“文件名”和“描述”属性包含从VRML文件的“标题”属性中获取的文件名和描述。所有属性的详细描述都超出了本示例的范围,但是可以在Simulink 3D动画文档中轻松找到。万博1manbetx

get(world)
Canvases = vr.canvas object: 0-by-0 Clients = 1 ClientUpdates = 'on' Comment = '' Description = 'VR Car in the Mountains' Figures = vrfigure object: 1-by-1 FileName = char array: 1-by-76 Nodes = vrnode object: 13-by-1 Open = 'on' Record3D = 'off' Record3DFileName = '%f_anim_%n.%e' Recording = 'off' RecordMode = 'manual' RecordInterval = [0 0] RemoteView = 'off' Time = 0 TimeSource = 'external' View = 'on' Viewpoints = vrnode object: 2-by-1

寻找世界的节点

全部elements in a virtual world are represented by VRML nodes. The behavior of any element can be controlled by changing the fields of the appropriate node(s). The NODES command prints out a list of nodes available in the world.

节点(世界)
View1 (Viewpoint) [VR Car in the Mountains] Camera_car (Transform) [VR Car in the Mountains] VPfollow (Viewpoint) [VR Car in the Mountains] Automobile (Transform) [VR Car in the Mountains] Wheel (Shape) [VR Car in the Mountains] Tree1 (Group) [VR Car in the Mountains] Wood (Group) [VR Car in the Mountains] Canal (Shape) [VR Car in the Mountains] ElevApp (Appearance) [VR Car in the Mountains] River (Shape) [VR Car in the Mountains] Bridge (Shape) [VR Car in the Mountains] Road (Shape) [VR Car in the Mountains] Tunnel (Transform) [VR Car in the Mountains]

访问VRML节点

访问VRML节点,一个适当的VRNODE object must be created. The node is identified by its name and the world it belongs to.

我们将创建一个与VRML节点“汽车”相关联的VRNode对象,该对象代表道路上的汽车模型。如果您在场景中看不到它,请不要担心。它隐藏在左侧的隧道中。

车= vrnode(world,'Automobile')
car = vrnode对象:1 by-1汽车(变换)[山上的VR汽车]

Viewing Fields of Nodes

可以使用字段命令查询给定节点的VRML字段。您会看到节点列表中有名为“翻译”和“旋转”的字段。您可以通过更改这些字段的值来移动汽车。

fields(car)
Field Access Type Sync ----------------------------------------------------------- removeChildren eventIn MFNode off addChildren eventIn MFNode off metadata exposedField SFNode off children exposedField MFNode off center exposedField SFVec3f off rotation exposedField SFRotation off scale exposedField SFVec3f off scaleOrientation exposedField SFRotation off translation exposedField SFVec3f off bboxSize field SFVec3f off bboxCenter field SFVec3f off

Moving the Car Node

现在,我们准备确定汽车运动的坐标向量。通过将它们设置在循环中,我们将创建一个动画场景。汽车运动的三个阶段有三组数据。

Z1 = 0:12;x1 = 3 +零(size(z1));y1 = 0.25 +零(size(z1));Z2 = 12:26;x2 = 3:1.4285:23;y2 = 0.25 +零(size(z2));x3 = 23:43;z3 = 26 +零(size(x3));y3 = 0.25 +零(size(z3));

Now let's move the car along the first part of its trajectory. The car is moved by setting the 'translation' field of the 'Automobile' node.

fori = 1:长度(x1)car.translation = [x1(i)y1(i)z1(i)];vrdrawnow;暂停(0.1);end

We'll rotate the car a little to get to the second part of the road. This is done by setting the 'rotation' property of the 'Automobile' node.

车.rotation = [0, 1, 0, -0.7]; vrdrawnow;

现在,我们将通过第二条道路部分。

fori=1:length(x2) car.translation = [x2(i) y2(i) z2(i)]; vrdrawnow; pause(0.1);end

最后,我们再次向左转动汽车...

car.Rotation = [0,1,0,0];vrdrawnow;

... and let it move through the third part of the road.

fori=1:length(x3) car.translation = [x3(i) y3(i) z3(i)]; vrdrawnow; pause(0.1);end

If you want to reset the scene to its original state defined in the VRML file, just reload the world.

reload(world); vrdrawnow;

保留MATLAB®工作区中的虚拟世界对象

使用VRWorld对象完成后,必须关闭并删除它。这是使用关闭和删除命令来完成的。

close(world); delete(world);

However, we will not do it here. Instead, let's leave the world open so that you can play with it further. You can try moving the car around using commands similar to those above, or you can try to access other nodes and their fields. We will clear only the used global variables.

清除Ansix1x2x3y1y2y3z1z2z3