Main Content

Interact with a Virtual World with MATLAB

Set Values for Nodes

In the life cycle of a vrworld object you can set new values for all the available virtual world nodes and their fields using vrnode 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 type vrworld contains nodes named in the virtual world 3D file using the DEF statement. These nodes are of type vrnode. For more information, see vrworld and vrnode functions.

After you open a vrworld object, you can get a list of available nodes in the virtual world. This procedure uses the vrworld object myworld and the virtual world vrmount.wrl as an example. To create the myworld, see Create vrworld Object for a Virtual World.

  1. 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 Simulink® 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]
    
  2. Type

    mynodes = get(myworld, 'Nodes')
    

    The MATLAB software creates an array of vrnode objects corresponding to the virtual world nodes and displays

    mynodes = 
    
    	vrnode object: 13-by-1
    
    	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]
    
  3. 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.

  4. Access the fields of the Automobile node by typing

    fields(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 type Transform. This node allows you to change its position by changing its translation field values. From the list, you can see that translation requires three values, representing the [x y z] coordinates of the object.

  5. Type

    view(myworld)
    

    Your default viewer opens and displays the virtual world vrmount.wrl.

  6. 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 function vrnode/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 (either exposedField or eventOut), first synchronize that field with the vrnode/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 the vrnode/getfield method or directly access the field value using dot notation.

The virtual scene for the Magnetic Levitation Model example, maglev.wrl, contains a PlaneSensor (with the DEF 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 fields minPosition and maxPosition 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.

  1. Create the vrworld object and open the world.

    wh = vrworld('maglev.wrl');
    open(wh);
  2. Get the node handle.

    nh = vrnode(wh, 'Grab_Sensor');
  3. Synchronize the translation field.

    sync(nh, 'translation', 'on');
  4. 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 a Transform object are specified in local coordinates, relative to the parent object of the object. Simulink 3D Animation provides two extensions for converting rotation and translation values into global coordinates: rotation_abs and translation_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

See Also

Functions

Related Topics