Products & Services Solutions Academia Support User Community Company

Learn more about Simulink 3D Animation   

Using the MATLAB Interface

Creating a vrworld Object

To connect MATLAB to a virtual world and to interact with that virtual world through the MATLAB command-line interface, you need to create vrworld and vrnode objects. A virtual world is defined by a VRML file with the extension .wrl. For a complete list of virtual world methods, see vrworld Object Methods, vrnode Object Methods, and vrfigure Object Methods.

After you create a virtual world, you can create a vrworld object. This procedure uses the virtual world vrmount.wrl as an example.

  1. Open MATLAB. In the MATLAB Command Window, type

    myworld = vrworld('vrmount.wrl')
    

    The MATLAB Command Window displays output like

    myworld = 
    	vrworld object: 1-by-1
    
    VR Car in the Mountains 
    (matlabroot/toolbox/sl3d/vrdemos/vrmount.wrl)
    
  2. Type

    vrwhos
    

    The MATLAB Command Window displays the messages

    		Closed, associated with  
    		'C:matlabroot\toolbox\sl3d\sl3ddemos\vrmount.wrl'.
    		Visible for local viewers.
    		No clients are logged on.
    

The vrworld object myworld is associated with the virtual world vrmount.wrl. You can think of the variable myworld as a handle to the vrworld object stored in the MATLAB workspace.

Your next step is to open a virtual world using the vrworld object. See Opening a Virtual World.

Opening a Virtual World

Opening a virtual world lets you view the virtual world in a VRML viewer, scan its structure, and change virtual world properties from the MATLAB Command Window.

After you create a vrworld object, you can open the virtual world by using the vrworld object associated with that virtual world. This procedure uses the vrworld object myworld associated with the virtual world vrmount.wrl as an example:

  1. In the MATLAB Command Window, type

    open(myworld);
    

    The MATLAB Command Window opens the virtual world vrmount.wrl.

  2. Type

    set(myworld, 'Description', 'My first virtual world');
    

    The Description property is changed to My first virtual world. This is the description that is displayed in all Simulink® 3D Animation object listings, in the title bar of the Simulink 3D Animation viewer, and in the list of virtual worlds on the Simulink 3D Animation HTML page.

  3. Display the virtual world vrmount.wrl. Type

    view(myworld)
    

    The viewer that is set as the default viewer displays the virtual scene. This is typically the Simulink 3D Animation viewer unless you have a different viewer set.

Alternatively, you can display the virtual world in a VRML-enabled Web browser.

  1. Repeat steps 1 and 2 of the preceding procedure.

  2. Open a Web browser. In the Address box, type

    http://localhost:8123
    

    The browser displays the Simulink 3D Animation HTML page with a link to My first virtual world. The number 8123 is the default Simulink 3D Animation port number. If you set a different port number on your system, enter that number in place of 8123. For more information on the Simulink 3D Animation HTML page, see Viewing a Virtual World with a Web Browser on the Host Computer.

  3. If the Web browser has the VRML plug-in installed, in the browser window, click My first virtual world.

  4. Your default VRML-enabled Web browser displays the virtual world vrmount.wrl.

      Note   If your Web browser is not VRML-enabled, clicking on a virtual world link such as My first virtual world results in a broken link message. The browser cannot display the virtual world. If you get such a message, ensure that the Web browser is properly enabled for VRML with the blaxxun Contact plug-in. For details, see Installation.

For more information on changing your default viewer, see Setting the Default Viewer of Virtual Scenes.

Interacting with a Virtual World

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 Object Methods and vrnode Object Methods.

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:

  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.

Closing and Deleting a vrworld Object

After you are finished with a session, you must close all open virtual worlds and remove them from memory:

  1. In the MATLAB Command Window, type

    close(myworld);
    delete(myworld);
    

    The virtual world representation of the vrworld object myworld is removed from memory. All possible connections to the viewer and browser are closed and the virtual world name is removed from the list of available worlds.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS