Main Content

Animate Actor using Simulink Real-Time Instrument

R2026b

This example shows how to use Simulink® 3D Animation with Simulink® Real-Time™ Instrument objects and real-time simulation. For more information about animation techniques, see Simulate Actor Movement Using MATLAB.

Build World in 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;

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.

run(world)

Open and Build Simulink Model

tg = slrealtime;
connect(tg);
model = 'slrt_ex_animateactor';
open_system(model);
modelSTF = getSTFName(tg);
set_param(model,"SystemTargetFile",modelSTF);
set_param(model,"StopTime","10.0");
set_param('slrt_ex_animateactor/Sine Wave','Amplitude','[1 1 1]');
set_param('slrt_ex_animateactor/Sine Wave','Bias','[0 0 0]');
set_param('slrt_ex_animateactor/Sine Wave','Frequency','[1 1 1]');
set_param('slrt_ex_animateactor/Sine Wave','Phase','[0 pi/2 pi]');
evalc('slbuild(model)');

Add Simulink Real-Time Instrument and connect Callback code to Update World

hInst = slrealtime.Instrument(model);
hInst.addSignal("slrt_ex_animateactor/Sine Wave",1);
hInst.connectCallback(@(src,evnt)updateWorld(src,evnt,world));

Add Instrument to Target and Start Execution

addInstrument(tg,hInst);
load(tg,model);
start(tg);

Change Sine Wave Parameters and Visualize Changes

setparam(tg,[model,'/Sine Wave'],'Amplitude',[1 2 1]);
setparam(tg,[model,'/Sine Wave'],'Bias',[1 0 3]);
setparam(tg,[model,'/Sine Wave'],'Frequency',[1 5 1]);
setparam(tg,[model,'/Sine Wave'],'Phase',[0 pi/2 pi/2]);

Stop Model on Target

stop(tg);
removeAllInstruments(tg);

Delete World

Delete the world object.

world.delete();