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 virtual world object name and the virtual world virtual world.x3d as an example.

  1. In the MATLAB Command Window, type

    nodes(virtual world object name);
    

    The MATLAB Command Window displays a list of the vrnode objects and their fields that are accessible from the Simulink® 3D Animation™ software.

  2. Type

    mynodes = get(virtual world object name, 'Nodes')
    

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

  3. Type

    whos
    

    The MATLAB Command Window displays the messages.

    Now you can get node characteristics and set new values for certain node properties.

  4. Type

    view(virtual world object name)
    

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.

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('virtual world object name');
n = get(w,'Nodes');
n = w.Grip_Reference;
n.translation;
n.translation_abs;

See Also

Functions

Related Topics