Build and Connect a Virtual World


The example in this section shows you how to create a simple virtual world using the 3D World Editor. The example does not show everything that you can do with the editor. However, the example does show you how to perform some basic tasks to get started.

This example assumes that you have set your default editor to be the 3D World Editor. For details, see Set the Default Editor.

This example describes the steps to build a simplified version of the virtual world that you see if you enter this command in the MATLAB® command window:


Define the Problem

Suppose that you want to simulate and visualize in virtual reality the deformation of a sphere. In your virtual world, you want to have two boxes representing rigid plates (B1, B2) and an elastic sphere (S) between them. All three of the objects are center-aligned along the x-axis. The boxes B1 and B2 move toward S with identical velocities, but they move in opposite directions. As they reach the sphere S, they start to deform it by reducing its x dimension and stretching both its y and z dimensions.

Here is how this virtual world looks:

The following table lists the positions and dimensions of the objects that you create for this example.

ObjectCenter PositionDimensions


[3 0 0]

[0.3 1 1]


[-3 0 0]

[0.3 1 1]


[0 0 0]

r = 0.9

The Simulink® 3D Animation™ product includes the tutorial model vrtut3. This simplified model simulates the deformation of an elastic sphere. After collision with the rigid blocks, the sphere's x dimension decreases by a factor from 1 to 0.4. Also, the y and z dimensions expand to keep the volume of the deformed sphere-ellipsoid constant. Additional blocks in the model supply the correctly sized vectors to the Simulink 3D Animation block. The simulation stops when the sphere is deformed to 0.4 times its original size in the x direction.

Your first task is to open a Simulink model and add a Simulink 3D Animation block to your model.

Add a Simulink 3D Animation Block

This procedure uses the Simulink model vrtut3 to show how to add a Simulink 3D Animation block to your model. The model generates the values for the position of B1, the position of B2, and the dimensions of S (as described in Define the Problem).

  1. Open the Tutorial #3. example.

    1. At the top of the page that opens, select Open this Example.

    2. Save thevrtut3 file to your MATLAB working folder.

  2. In MATLAB, change the current folder to your MATLAB working folder.

  3. In the MATLAB Command Window, type:


    A Simulink window opens with a model that contains Simulink 3D Animation VR Signal Expander blocks, but no VR Sink block to write data from the model to Simulink 3D Animation. Instead, this model uses Scope blocks to monitor temporarily the relevant signals.

  4. From the MATLAB Command Window, type


    The Simulink 3D Animation library opens.

  5. From the Library window, drag and drop the VR Sink block to the Simulink diagram. You can then close the Library Browser window.

Your next task is to create a virtual world that you will associate with the VR Sink block. See Open a New Virtual World.

Open a New Virtual World

You must create a virtual world to connect to a Simulink model for visualizing signals.

This procedure opens a new virtual world, in which you add nodes for visualizing the signals of the model vrtut3. The connection between the virtual world and the Simulink model requires that the model includes a VR Sink block, as described in Add a Simulink 3D Animation Block.

  1. Start the 3D World Editor with an empty virtual world. From the MATLAB Toolstrip, in the Apps tab, in the Simulation Graphics and Reporting section, click 3D World Editor.

    The 3D World Editor displays:

    • In the left pane, a virtual scene tree with only a ROOT node

    • In the right pane, an empty virtual world

    • In the bottom pane, an empty pane for editing objects

  2. You can save the virtual world at any point. Save the virtual world as vrtut3.wrl in the same working folder where your vrtut3 file resides. Do not close the 3D World Editor.

Your next two tasks create a virtual world to use with the vrtut3 model:

Add Nodes

Create Boxes

Defining virtual world objects involves defining a hierarchy of nodes. This example shows how to define Transform nodes under the ROOT node, with each Transform node including a hierarchy of children, Shape, Appearance, Geometry, and specific shape (in this case, a Box) nodes.

  1. In the tree in the left pane, click ROOT (the topmost item).

  2. Add a Transform node, using the following sequence of menu selections.

  3. This Transform node is for the B1 box. To name the Transform node:

    1. Right-click the Transform node.

    2. Select the Edit Name menu item.

    3. In the edit box to the left of the Transform node, type B1.

  4. Add a Shape node:

    1. Expand the B1 Transform node.

    2. Select the children node.

    3. Add a Shape node, using the following sequence of menu selections:

  5. Add an Appearance node for the Shape node:

    1. Under the Shape node, select the appearance (SFNode) node.

    2. Add an Appearance node, using the following sequence of menu selections.

  6. Add a Material node to the Appearance node:

    1. Expand the (Appearance) node and select the material(SFNode) node.

    2. Add a Material node, using the following sequence of menu selections.

  7. Add a Box node to the geometry node:

    1. Select the geometry(SFNode) node of the (Shape) node.

    2. Add a Box node, using the following sequence of menu selections.

    With all the nodes expanded, the 3D World Editor now displays a box in the virtual world display pane.

  8. Make the box smaller by editing its size property:

    1. Select the size property of the Box node.

    2. In the object properties edit pane at the bottom of the 3D World Editor, enter 0.3 in the first column, and 1 in the second and third columns.

    3. Click Apply.

    The box becomes smaller.

  9. Move the box to the right by changing the translation(SFVec3f) property of the B1(Transform) node. In the object properties edit pane, set the first column to 3 and leave the second and third columns set to 0.

  10. Add a second box that is similar to the first box.

    1. Under the ROOT node, add a Transform node (see step 2) and name it B2 (see step 3).

    2. Copy the Shape node. Under the B1 Transform node, right-click the Shape node in the B1 Transform node and select the Copy menu item.

    3. Paste the copied Shape node into the B2 Transform node. Under the B2 Transform node), right-click the children node and select the Paste Node > Paste menu item.

      With the B1 node collapsed and the B2 node expanded, the 3D World Editor looks like the following graphic.

  11. Move the box that you created to the left by changing the translation property of the B2(Transform) node. In the object properties edit pane, set the first column to -3 and leave the second and third columns set to 0.

Create a Sphere

Your next task is to add a sphere between the two boxes. This section assumes that you have completed the tasks described in Add Nodes.

  1. To make it easier to focus the tree structure pane on the nodes that you want to add, collapse the B1(Transform) and B2(Transform) nodes.

  2. In the tree in the left pane, click ROOT node.

  3. Add a Sphere node. The 3D World Editor includes a library of objects for building a virtual world, including a Sphere object.

    Add a Sphere library object using the following sequence of menu selections.

    From the list of Component Library folders, select the Shapes folder, and then select the Sphere.wrl file.

  4. Select the Transform node and name it S.

    With the S Transform node fully expanded and the other Transform nodes collapsed, the 3D World Editor looks like the following graphic.

  5. To make the sphere blue, under the Material node, select the diffuseColor property. In the object properties edit pane, change the first column value to 0.2, the second column to 1, and the third column to 1.

  6. Save the virtual world file.

Your next task is to connect the model outputs to the Simulink 3D Animation block in your Simulink model. See Link to a Simulink Model.

Link to a Simulink Model

After you create a virtual world and a Simulink model with a VR Sink block, define the associations between the model signals and the virtual world. This procedure uses the model vrtut3 as an example. It assumes that you have opened the model and that you have added a VR Sink block, and that you have created a virtual world called vrtut3.wrl. See the tutorial starting with Add a Simulink 3D Animation Block.

  1. In the Simulink model window, double-click the VR Sink block.

    The Parameters: VR Sink dialog box opens.

  2. Next to the Source file edit box, click Browse.

    The Open dialog box opens.

  3. Select vrtut3.wrl, and then click Open.

  4. In the Output pane, select the Open Viewer automatically check box.

    This check box specifies that a viewer for the virtual world starts when you run the model.

  5. In the Description field, type vrtut3.

  6. Click Apply in the Parameters: VR Sink dialog box.

  7. In the tree structure pane, select the B1 translation, B2 translation, and S scale check boxes as the nodes you want to connect to your model signals. Click OK to close the dialog box.

    The VR Sink block appears with corresponding inputs.

  8. Delete the three Scope blocks (B1 Translation, B2 Translation, and S scale) and their associated input signal lines.

  9. Connect the input lines from the VR Signal Expander, VR Signal Expander1, and S Scaling in XYZ blocks to the appropriate ports in the VR Sink block.

  10. Double-click the VR Sink block.

    The viewer appears.

  11. Select the Simulation menu Block Parameters option. Your default viewer opens and displays the virtual world. For more information on changing your default viewer, see Set the Default Viewer.

  12. In the Block Parameters dialog box of the VR Sink block, click the View button.

  13. In the Simulink window, from the Simulation menu, click Run.

    In your default viewer, you see a 3-D animation of the scene. Using the viewer controls, you can observe the action from various points.

    When the width of the sphere is reduced to 0.4 of its original size, the simulation stops running.

This example shows you how to create and use a simple virtual reality model. Using the same method, you can create more complex models for solving the particular problems that you face.

See Also



Related Topics

Was this topic helpful?