Main Content

Animate Actor Using Simulink and MATLAB Inputs

This example shows how to set up a cosimulation framework with Unreal Engine®. You can control the actor movement in the Unreal Engine environment either using Simulink® or MATLAB® and view the animation in the Simulation 3D Viewer. You can also capture the actor image from any viewpoint using a virtual camera.

Animate Actor Using Simulink Input

You can use the Simulation 3D Actor block and Simulation 3D Scene Configuration block to set up a cosimulation framework between the Unreal Engine and Simulink. In this example, you build an actor and control the actor movement using Simulink blocks.

Open Model

Open the Simulink model.

open_system("CosimulateWithUE");

Simulink model with Simulation 3D Actor block named cube1, delay blocks, constant blocks, sum blocks and a simulation 3D scene configuration block.

Explore Model Components

The main model components include a Simulation 3D Scene Configuration block, a Simulation 3D Actor block, and a Simulation 3D Camera Get block. The Simulation 3D Scene Configuration block implements a 3D simulation environment. Double-click the Simulation 3D Scene Configuration block to open the Block Parameters dialog box. Set a view in the scene with the Scene view parameter. You can also set a custom viewpoint with this parameter. You must include the configuration block when building Simulink models with Simulation 3D Actor blocks.

The block parameter dialog box of simulation 3D scene configuration block.

The Simulation 3D Actor block adds an actor to the virtual world. Double-click the Simulation 3D Actor block to open the Block Parameters dialog box. To create an actor before simulation starts, on the Main tab, set Operation to Create at setup. The block first creates an empty actor with the name specified in the Actor name parameter. You can use any name for the actor. Then, the block loads the source file, if any is present, and runs the Initialization script. For more details, see Operating Modes. The Initialization script builds a box shape for the actor Cube1 using the createShape function. Specify the size and color of the actor in the Initialization script text box.

The block parameter dialog box of the Simulation 3D Actor block named cube1 shows parameters and the initialization script

You can use input and output ports to control the actor during simulation. On the Inputs and Outputs tabs, click Browse to add input and output ports to the actor block, respectively.

Input tab of cube1 block

Output tab of cube1 block

The output ports feed back to the input ports with delay units and Sum blocks to vary the translation, rotation, scale, and color of the actor during each simulation step. The changing values of the properties animates the actor in the Unreal Engine.

The Simulation 3D Camera Get block outputs the images captured during the simulation. The block outputs the camera display using a To Video Display (Computer Vision Toolbox) block.

Simulate Model

Simulate the model and view the animation of the cube in the Simulation 3D Viewer.

sim("CosimulateWithUE");

Cube actor in the virtual world.

You can also view the Camera Display behind the Simulation 3D Viewer window.

Cube actor in the virtual world.

Close Model

Close the Simulink model.

close_system("CosimulateWithUE");

Animate Actor Using MATLAB

You can use the sim3d.World object to set up a cosimulation framework between the Unreal Engine and MATLAB. The sim3d.World object can send and receive data about a sim3d.Actor object to the Unreal Engine at each simulation step using output and update functions. Before the Unreal Engine simulates, MATLAB calls the output function. Then, the Unreal Engine executes at each time step and sends data back to MATLAB in the update function.

Create Actor in Virtual World

Create a virtual world using functions that set up communication with the Unreal Engine.

world = sim3d.World('Output', @outputImpl, 'Update', @updateImpl);

Instantiate a box actor object named Box1. You can use any name for the actor. Specify the size and color of the actor object and add the object to the virtual world.

box1 = sim3d.Actor('ActorName', 'Box1', 'Mobility', sim3d.utils.MobilityTypes.Movable);
box1.createShape('box', [0.25 0.25 0.25]);
box1.Color = [1, 0, 1];
world.add(box1);

Add Camera to World Scene

Use the sim3d.sensors.IdealCamera object to capture an image in the Unreal Engine and return the image to MATLAB. To return the image, use the read function in the update function updateImpl.

camera1 = sim3d.sensors.IdealCamera("ActorName","Camera1", "ImageSize", [768, 1024],...
    "HorizontalFieldOfView", 60);
camera1.Translation = [-3, 0, 2];
world.add(camera1);

Set Viewer Window Point of View

If you do not create a viewport, then the point of view is set to 0, 0, 0, and you can use the keyboard shortcuts and mouse controls to navigate in the Simulation 3D Viewer window.

For this example, use the createViewport function to create a viewport with a single field, Main, that contains a sim3d.sensors.MainCamera object.

viewport = createViewport(world);
viewport.Translation = [-5, 0, 1];

Run Animation

Run the animation set for 2 seconds with a sample time of 0.01 seconds. The image from Camera1 displays in MATLAB.

run(world,0.01,2)

Cube actor in the virtual world

Delete World

Delete the world object.

world.delete();

Set Up Output Function

Use an output function to send data at each simulation step. The outputImpl function sends data about the Box1 actor object to the Unreal Engine. This function controls the actor by varying the actor properties during each simulation step. The changing values of the properties animates the actor in the Unreal Engine.

function outputImpl(world)
% Sets the actor outputs (e.g. actor position to follow a path)
world.Actors.Box1.Translation(3) = world.Actors.Box1.Translation(3) + 0.01;
world.Actors.Box1.Rotation(3) = world.Actors.Box1.Rotation(3) + 0.01;
world.Actors.Box1.Scale = world.Actors.Box1.Scale + [0.01, 0.01, 0.01];
world.Actors.Box1.Color = world.Actors.Box1.Color + [0.01, 0, 0];
end

Set Up Update Function

Use an update function to read data at each simulation step. The updateImpl function reads image data from Camera1 in the Unreal Engine using the read function of the sim3d.sensors.IdealCamera object.

function updateImpl(world)
% Implements a control algorithm and updates the actor states (e.g. read a
% sensor and calculate new actor position)
sceneImage = world.Actors.Camera1.read();
image(sceneImage);
drawnow;
end

See Also

| | | | | | |

Related Topics