Delete Actor During Simulation Using Simulink
This example shows how to delete an actor during a simulation using Simulink®. You build two ball actors and choose one to destroy. To delete an actor during a simulation using MATLAB®, see Delete Actor During Simulation Using MATLAB.
You can use the Simulation 3D Actor block and Simulation 3D Scene Configuration block to create an actor in a 3D environment. You can then delete the actor during simulation.
Open Model
Open the Simulink model.
open_system("DeleteActor");
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.
The Simulation 3D Actor block adds an actor to the 3D environment. 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 and color of each actor in the Initialization script. Set the initial translation of each actor in the Transform tab.
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.
Simulate Model
Simulate the model and view the two balls in the Simulation 3D Viewer.
sim("DeleteActor");
The Ball2
actor disappears from the 3D environment during simulation.
Close Model
Close the Simulink model.
close_system("DeleteActor");
See Also
Simulation 3D Actor | Simulation 3D Scene Configuration