remove
Description
Examples
Delete Actor During Simulation Using MATLAB
This example shows how to delete an actor during a simulation using MATLAB®. You build two ball actors and choose one to destroy. To delete an actor during a simulation using Simulink®, see Delete Actor During Simulation Using Simulink.
You can use the sim3d.World
class and functions to create a world object, view a 3D environment, and delete an actor from the 3D environment during simulation. You can use the sim3d.Actor
class and functions to build actor objects in the 3D environment.
You can also modify actors at run time using the sim3d.World
class.
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'); createShape(ball1,'sphere',[0.5 0.5 0.5]); ball1.Color = [0 .149 .179]; ball1.Translation = [0 0 4.5]; add(world,ball1); ball2 = sim3d.Actor(ActorName='Ball2'); createShape(ball2,'sphere',[0.5 0.5 0.5]); ball2.Color = [.255 .510 .850]; ball2.Translation = [0 0 3]; add(world,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 the default view is set 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.
viewport = createViewport(world);
Run Animation
Run a simulation set for 10
seconds with a sample time of 0.01
seconds.
sampletime = 0.01; stoptime = 10; run(world,sampletime,stoptime);
The Ball1
actor disappears from the 3D environment during simulation.
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}; remove(world,actorToDelete); end end
Input Arguments
world
— World object
sim3d.World
object
World object from which the actor object is being removed, specified as a
sim3d.World
object.
actor
— Actor object
sim3d.Actor
object
Actor object being removed from the 3D world, specified as a
sim3d.Actor
object.
Version History
Introduced in R2023a
See Also
sim3d.World
| sim3d.Actor
| createViewport
| add
| run
| pause
| resume
| close
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)