Documentation Center

  • Trial Software
  • Product Updates

Interact with a Virtual World with MATLAB

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 VRML 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 like the following table.

    		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 VRML 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, and you can observe that the car is repositioned in the VRML 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.

Was this topic helpful?