Main Content

Build and Connect a Virtual World


This example 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.

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.

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.

Open Tutorial Model Three

This example opens the tutorial model three, vrtut3, showing how to connect a Simulink® model to a virtual world.


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 tutorial model three, vrtut3.


    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.

  2. From the MATLAB® Command Window, type


    The Simulink 3D Animation library opens.

  3. 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.

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.x3d 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.x3d.

  1. Open the VR Sink Block Parameters dialog box. In the Simulink Editor, double-click the VR Sink block.

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

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

  4. In the Output pane, select Open Viewer automatically. This check box specifies that a viewer for the virtual world starts when you run the model.

  5. For the Description parameter, type vrtut3.

  6. In the VR Sink dialog box, click Apply.

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

    The VR Sink block appears with corresponding inputs.

  8. Delete the three Scope blocks and their associated input signal lines.

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

  10. Double-click the VR Sink block.

    The viewer appears.

  11. In the viewer, select the Simulation > 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 VR Sink Block Parameters dialog box, click the View button.

  13. In the Simulink Editor, select Simulation > 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.

See Also



Related Topics