Main Content

Delete Actor During Simulation

This example shows how to delete an actor during a simulation using either Simulink® or MATLAB®. You build two ball actors and choose one to destroy.

Delete Actor During Simulation Using Simulink

You can use the Simulation 3D Actor block and Simulation 3D Scene Configuration block to create an actor in a virtual world. You can then delete the actor during simulation.

Open Model

Open the Simulink model.

open_system("DeleteActor");

Simulink model with two Simulation 3D Actor blocks named Ball1 and Ball2 and a Simulation 3D Scene Configuration block.

Explore Model Components

The model includes a Simulation 3D Scene Configuration block and two Simulation 3D Actor blocks. 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.

Block parameter dialog box of Simulation 3D Scene Configuration block.

The Simulation 3D Actor block adds an actor to the virtual world. This example uses two Simulation 3D Actor blocks to create actors named Ball1 and Ball2. Double-click the Ball1 block to open the Block Parameters dialog box. In this example, to create an actor dynamically during simulation, on the Main tab, set Operation to Create at step. This setting creates the input port Instance. The block generates actors during simulation based on the value at the Instance port. When the Instance value is positive, the block creates an actor with 'Actor name + Instance value'. For more details, see Operating Modes.

In this example, each actor block creates an actor during simulation. The Initialization script of each actor block builds a sphere shape using the createShape function. You can use the actor object Properties to specify the size, color, and translation of each actor in the Initialization script text box.

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

To create the Ball1 actor connect the Instance port to a Constant block with value 1. To create the Ball2 actor, connect a Step block with value 2 to the Instance port. A positive instance value creates an actor, and a negative value deletes an actor. During simulation, the Step block changes from positive 2 to negative 2 value after Step time. This change deletes the Ball2 actor during simulation.

The block parameter dialog box of step block shows step time, initial and final values.

Simulate Model

Simulate the model and view the two balls in the Simulation 3D Viewer.

sim("DeleteActor");

Two ball actors in the virtual world.

The Ball2 actor disappears from the virtual world during simulation.

One ball actor in the virtual world.

Close Model

Close the Simulink model.

close_system("DeleteActor");

Delete Actor During Simulation Using MATLAB

You can use the sim3d.World object and functions to create, view a virtual world, and delete any actor from the virtual world during simulation. You can use the sim3d.Actor object and functions to build actors in the virtual world.

You can modify actors at run time using the sim3d.World object.

Create World

Create a world scene and set up communication with the Unreal Engine® using the update function. The Unreal Engine executes at each time step and sends data to MATLAB in the update function.

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

Build Ball Actors

Instantiate two actors named Ball1 and Ball2. Build actor appearances from a sphere using the createShape function. Add actors to the world.

ball1 = sim3d.Actor('ActorName', 'Ball1');
ball1.createShape('sphere', [0.5 0.5 0.5]);
ball1.Color = [0 .149 .179];
ball1.Translation = [0, 0, 4.5];
ball2 = sim3d.Actor('ActorName', 'Ball2');
ball2.createShape('sphere', [0.5 0.5 0.5]);
ball2.Color = [.255 .510 .850];
ball2.Translation = [0, 0, 3];
world.add(ball1);
world.add(ball2);

Use the UserData property in the sim3d.World object to create a user data structure with a field named Step to store the simulation step during simulation. Initialize the user data structure to 0. You will use this structure to insert a delay before removing an actor from the world in the update function.

world.UserData.Step = 0;

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);

Run Animation

Run a simulation set for 10 seconds with a sample time of 0.01 seconds.

run(world,0.01,10)

Two ball actors in the virtual world.

The Ball1 actor disappears from the virtual world during simulation.

One ball actor in the virtual world.

Delete world

Delete the world object.

delete(world);

Set Up Update Function

Use an update function to read data at each simulation step. The updateImpl function reads image data from MainCamera1 in the Unreal Engine and removes a ball actor from the scene.

function updateImpl(world)
world.UserData.Step=world.UserData.Step + 1;
actorFields = fields(world.Actors);
actorPresent = strcmp(actorFields, 'Ball1');
if any(actorPresent) && (world.UserData.Step == 500)
    actorIndex=(find(actorPresent));
    actorToDelete = actorFields{actorIndex};
    world.remove(actorToDelete);
end
end

See Also

| | | | | |

Related Topics