Documentation

Connect Virtual Worlds and Models

Visualize Model Dynamics in Virtual Worlds

After you create a virtual world and a Simulink® model, to have the virtual world interact with a dynamic system simulation, connect the model and the virtual world using Simulink 3D Animation™ blocks.

Simulating a Simulink model generates signal data for a dynamic system. To output data from the model to control and animate a virtual world, use a VR Sink block .

Output Simulation Data to a Virtual World

To output data from the model to control and animate a virtual world, use a VR Sink block.

This example shows how to use simulation data from a model to display a dynamic visualization of the simulation. The example simulates a plane taking off and lets you view it in a virtual world. This example assumes that you are using the Simulink 3D Animation Viewer.

Tip

For other examples of how to use the VR Sink block, see Magnetic Levitation Model and Geometry Morphing.

  1. In the MATLAB® Command Window, type

    vrtut2
    

    A Simulink model opens without a Simulink 3D Animation block that connects the model to a virtual world.

  2. In the Simulink Editor, select Simulation > Mode > Normal, then click Simulation > Run.

    Observe the results of the simulation in the scope windows.

  3. Double-click to the right of the model and, in the search box, type VR Sink, Select the block.

  4. Now you are ready to select a virtual world for the visualization of your simulation. A simple virtual world with a runway and a plane is in the virtual world 3D file vrtkoff.wrl, located in the vrdemos folder. Select the VR Sink. To open the Block Parameters dialog box if the VR Sink block is still selected, double-click the block. Otherwise, in the Simulink 3D Animation Viewer, select SimulationBlock parameters.

  5. Associate a virtual world with the model. At the Source File text box, click the Browse button. The Select World dialog box opens. Find the folder matlabroot\toolbox\sl3d\sl3ddemos. Select the file vrtkoff.wrl and click OK.

  6. In the Description text box, examine the brief description of the model. This description appears on the list of available worlds served by the Simulink 3D Animation server.

  7. Select the Open Viewer automatically parameter and click Apply. The VR Sink dialog box displays the virtual scene node tree, show the structure of the associate virtual world.

  8. Expand the Plane (Transform) node.

    Now you can see what characteristics of the plane can be driven from the Simulink interface. This model computes the position and the pitch of the plane.

  9. In the Plane (Transform) tree, select the translation and rotation fields, which represent the position (translation) and the pitch (rotation) of the plane. Click OK.

    In the Simulink diagram, the VR Sink block is updated with two inputs.

    The first input is Plane rotation. Define the rotation with a four-element vector. The first three numbers define the axis of rotation. In this example, it is [1 0 0] for the x-axis (see the Pitch Axis of Rotation block in the model). The pitch of the plane is expressed by the rotation about the x-axis. The last number is the rotation angle around the x-axis, in radians.

  10. In the Simulink model, connect the line going to the Scope block labeled Display Pitch to the Plane rotation input.

    The second input is Plane translation. This input describes the position of the plane in the virtual world. This position consists of three coordinates, x, y, z. The connected vector must have three values. In this example, the runway is in the x-z plane (using the VR Signal Expander block). The y-axis defines the altitude of the plane.

  11. In the Simulink model, connect the line going to the Scope block labeled Display Position to the Plane translation input.

    Remove the Scope blocks. Your model looks similar to the figure shown.

  12. Double-click the VR Sink block. A viewer window containing the virtual world of the plane opens.

    Tip

    When you next open the model, the associated virtual scene opens automatically. This behavior occurs even if the Simulink 3D Animation block associated with the virtual scene is in a subsystem of the model.

  13. Run the simulation. In the Simulink 3D Animation Viewer, from the Simulation menu, click Run.

    A plane, moving right to left, takes off .

Input Virtual World Data to a Model

Use the VR Source block to provide interactivity between a user navigating the virtual world and the simulation of a Simulink model. The VR Source block registers user interactions with the virtual world and passes that data to the model to affect the simulation of the model. The VR Source block reads values from virtual world fields specified in the block dialog box and inputs their values to a model. Using the block in this way, you can:

  • Use sensor data from a virtual world to control a simulation. For details, see Add Sensors to Virtual Worlds and Detect Object Collisions.

  • Provide interactivity between user navigation and interaction in a virtual world and the simulation of the model.

  • Have a simulation react to virtual world events, such as time ticks or outputs from scripts.

  • Use static information from the virtual world, such as the size of a box, to control a simulation.

For example, you can define setpoints in the virtual world, so that user can specify the location of a virtual world object interactively. The simulation then responds to the changed location of the object. The VR Source block can read into the model events from the virtual world, such as time ticks or outputs from scripts. The VR Source block can also read into the model static information about the virtual world (for example, the size of a box defined in the virtual world 3D file). For examples of models that use the VR Source block, see Magnetic Levitation Model and Virtual Control Panel.

Change the Associated Virtual World

You can associate a different virtual world with a Simulink model or connect different signals.

After you associate a virtual world with a Simulink model, you can select another virtual world or change signals connected to the virtual world. This example assumes that you have connected the vrtut2 Simulink model with a virtual world. See Input Virtual World Data to a Model.

  1. Double-click the VR Sink block in the model. The viewer opens.

  2. Open the Block Parameters dialog box of the VR Sink block by selecting Simulation > Block Parameters.

  3. At the Source File text box, click the Browse button. Find the folder matlabroot\toolbox\sl3d\sl3ddemos. Select the file vrtkoff2.wrl, and click OK. In the VR Sink dialog box, click Apply.

    A virtual scene tree appears on the right side, associating a different virtual world with the model.

  4. Expand the Plane (Transform) node.

    The Plane Transform tree expands. Now you can see what characteristics of the plane you can drive from the Simulink interface. This model computes the position.

  5. In the Plane Transform tree, select the translation field check box. Clear the rotation field check box. Click OK.

    The VR Sink block is updated and changes to just one input, the Plane translation. The VR Sink block is ready to use with the new parameters defined.

  6. Verify that the correct output is connected to your VR Sink block. The output from the VR Signal Expander is connected to the single input.

  7. In the Simulink 3D Animation Viewer, from the Simulation menu, run the simulation again and observe the simulation.

See Also

Functions

Blocks

Related Topics

Was this topic helpful?