SimMechanics | ![]() ![]() |
Interfacing SimMechanics with Virtual Worlds
To animate a body, you need to measure its motion in your SimMechanics simulation and export that information to the virtual world. This requires connecting Body Sensor blocks to the Bodies you want to animate in your model, then creating an interface that animates the virtual bodies with the body sensor motion signals.
Adding and Configuring Body Sensors
Refer to Sensing Body Motions for general information on how to use Body Sensors. Connect the Body Sensors to Body coordinate systems (CSs) on the bodies whose motions you want to animate. The Body block reference discusses how to create and configure Body CSs. You need to take these special steps to export the signals of a body sensor to your virtual world:
Absolute (World)
or Local (Body CS)
. This coordinate system should be the same as the coordinate system used to define the body's position and orientation in the VRML files.
A Simulink output port >
appears on the block for each of the motion signals. The translational signal is a 3-vector of spatial coordinates: (x, y, z). The rotational signal is a 9-vector, column-wise representation of the 3-by-3 orthogonal rotation matrix R: (R11, R21, R31, R12, ... ).
Animating the Virtual World Bodies
Animating the virtual bodies requires interfacing the body sensor signals in the SimMechanics model with the VRML translation and/or rotation fields in the .wrl
files. You accomplish this with the VR Sink block, which you can find in the Virtual Reality Toolbox block library. Enter
at the command line. Drag a copy of the VR Sink block into your model.
Open the VR Sink dialog box. In the Source file field in the World properties area, enter the name of the VRML file that represents your model's virtual world. This is the file that refers to the other .wrl
files representing the component bodies of your machine. If the virtual world VRML file is not the same directory as your model, enter the file's path relative to the model. Click Apply.
In the VRML tree window, the node list of the virtual world .wrl
file appears. Expand the tree of each component body in the list to view that body's check box list. Select the rotation and/or translation check boxes as needed for each body. Simulink input ports >
appear on the block icon for each of these selected check boxes. The ports are labeled node
.
field
. The node
is the name for the body. The field
is named either rotation
or translation
.
Converting Body Sensor Signals into VRML Format
You are now ready to connect the Body Sensor output signals to the VR Sink block. But you might need to modify those signals for valid use in VRML.
node
.translation
input port on the VR Sink. The VRML node tree directly accepts translation motion as a 3-vector signal of rectangular coordinates (x,y,z).
node
.rotation
input port on VR Sink for that body. This block converts the 3-by-3 R matrix signal into the 4-vector VRML form.
Close the VR Sink block dialog. Your SimMechanics model now animates the virtual world.
Example: Interfacing the Conveyor Loader Model and Virtual World
In the mech_conveyor_vr
demo model, open the Body Sensor1 block. The block measures the translational and rotational motion of Link3 in the conveyor:
The Body Sensor1 block has two Simulink output signals.
The other Body Sensor blocks are similar, except for Body Sensor2, which measures only the translational motion of the pusher. All the Body Sensors measure body motions with respect to World, the frame in which the conveyor base is at rest. Each motion signal represents the body's displacement relative to its initial position.
The mech_conveyor_vr
model contains a Virtual Reality Toolbox interface to the model's custom VRML files.
Conveyor Loader Model with Custom Virtual Reality Interface
convmech.wrl
, the master file for this virtual world. The VRML tree on the right reproduces the node tree visible in the VRML editor for convmech.wrl
.
node
.
field
. The Base of the conveyor does not move, so its node has no motion input fields.
mech_conveyor
demo, starting the model opens the Reference Position slider bar that you can move from side to side. As you do so, watch the pusher in the viewer move in parallel.
![]() | Creating Virtual Worlds for SimMechanics Models | SimMechanics Block Reference | ![]() |