Interact with a Virtual World withMATLAB
Set Values for Nodes
In the life cycle of avrworld
object you can set new values for all the available virtual world nodes and their fields usingvrnode
object methods. This way, you can change and control the degrees of freedom for the virtual world from within the MATLAB®environment.
An object of typevrworld
contains nodes named in the virtual world 3D file using the DEF statement. These nodes are of typevrnode
. For more information, seevrworld
andvrnode
functions.
After you open avrworld
object, you can get a list of available nodes in the virtual world. This procedure uses thevrworld
objectmyworld
and the virtual worldvrmount.wrl
as an example. To create themyworld
, seeCreate vrworld Object for a Virtual World.
In the MATLAB Command Window, type
nodes(myworld);
The MATLAB Command Window displays a list of the
vrnode
objects and their fields that are accessible from the金宝app®3D Animation™software.Tunnel (Transform) [My first virtual world] Road (Shape) [My first virtual world] Bridge (Shape) [My first virtual world] River (Shape) [My first virtual world] ElevApp (Appearance) [My first virtual world] Canal (Shape) [My first virtual world] Wood (Group) [My first virtual world] Tree1 (Group) [My first virtual world] Wheel (Shape) [My first virtual world] Automobile (Transform) [My first virtual world] VPfollow (Viewpoint) [My first virtual world] Camera_car (Transform) [My first virtual world] View1 (Viewpoint) [My first virtual world]
Type
mynodes = get(myworld, 'Nodes')
The MATLAB software creates an array of
vrnode
objects corresponding to the virtual world nodes and displaysmynodes = vrnode对象:13-by-1隧道(变换) [My first virtual world] Road (Shape) [My first virtual world] Bridge (Shape) [My first virtual world] River (Shape) [My first virtual world] ElevApp (Appearance) [My first virtual world] Canal (Shape) [My first virtual world] Wood (Group) [My first virtual world] Tree1 (Group) [My first virtual world] Wheel (Shape) [My first virtual world] Automobile (Transform) [My first virtual world] VPfollow (Viewpoint) [My first virtual world] Camera_car (Transform) [My first virtual world] View1 (Viewpoint) [My first virtual world]
Type
whos
The MATLAB Command Window displays the messages
Name Size Bytes Class ans 1x1 132 vrfigure object mynodes 13x1 3564 vrnode object myworld 1x1 132 vrworld object
Now you can get node characteristics and set new values for certain node properties. For example, you can change the position of the automobile by using
Automobile
,which is the fourth node in the virtual world.Access the fields of the
Automobile
node by typingfields(myworld.Automobile)
or
fields(mynodes(10));
The MATLAB Command Window displays information about the
Automobile
node.Field Access Type Sync ----------------------------------------------------------- addChildren eventIn MFNode off removeChildren eventIn MFNode 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 bboxCenter field SFVec3f off bboxSize field SFVec3f off
The
Automobile
node is of typeTransform
. This node allows you to change its position by changing itstranslation
field values. From the list, you can see thattranslation
requires three values, representing the [x y z] coordinates of the object.Type
view(myworld)
Your default viewer opens and displays the virtual world
vrmount.wrl
.Move the MATLAB window and the browser window side by side so you can view both at the same time. In the MATLAB Command Window, type
myworld.Automobile.translation = [15 0.25 20];
The MATLAB sets a new position for the
Automobile
node. You can observe that the car is repositioned in the virtual world browser window.
You can change the node fields listed by using the functionvrnode/setfield
.
Note
The dot notation is the preferred method for accessing nodes.
Read Sensor Values Using MATLAB
To read a value of a readable field (eitherexposedField
oreventOut
), first synchronize that field with thevrnode/sync
method. After synchronization, each time the field changes in the scene, the field value updates on the host. You can then read the value of the field with thevrnode/getfield
method or directly access the field value using dot notation.
The virtual scene for theMagnetic Levitation Modelexample,maglev.wrl
, contains a PlaneSensor (with theDEF
name'Grab_Sensor'
). The PlaneSensor is attached to the ball geometry to register your attempts to move the ball up or down when grabbing it using the mouse. The example uses the sensor fieldsminPosition
andmaxPosition
to restrict movement in other directions. You can use the output of the sensor translation field as the new setpoint for the ball position controller. You can read the sensor output value into a MATLAB variable setpoint.
Create the vrworld object and open the world.
wh = vrworld('maglev.wrl'); open(wh);
Get the node handle.
nh = vrnode(wh,'Grab_Sensor');
同步翻译领域。
sync(nh,'translation','on');
Read the synchronized field value, using one of these three alternatives:
setpoint = getfield(nh,'translation'); setpoint = nh.translation; setpoint = wh.Grab_Sensor.translation;
Global Coordinates for Rotation and Translation
Rotation and translation values for aTransform
object are specified in local coordinates, relative to the parent object of the object.Simulink 3D Animationprovides two extensions for converting rotation and translation values into global coordinates:rotation_abs
andtranslation_abs
. To access these global coordinates, use dot notation with the translation or rotation field, adding_abs
to the field name. This example shows the difference between the local and global coordinates for translation:
w = vrview('vrmanipul.wrl'); n = get(w,'Nodes'); n = w.Grip_Reference; n.translation n.translation_abs
ans = 0 -0.1000 0 ans = -3.0406 -3.0000 2.3334