remove
Description
Examples
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 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 Simulation
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 object that defines the 3D environment, specified as a sim3d.World
object.
Example: world = sim3d.World()
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
| wait
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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)