Main Content

Build Actor from 3D Graphic Primitives

This example shows how to build an actor from a 3D graphic primitive using either Simulink® or MATLAB®. You can use the createShape function to build an appearance for the actor using predefined 3D graphic primitives, including box, cylinder, plane, and sphere.

Build Actor from 3D Graphic Primitive Using Simulink

You can use Simulation 3D Actor block and Simulation 3D Scene Configuration block to create a virtual world with an actor. You can build the appearance of the actor from a 3D graphic primitive and set a view in the scene to view the actor in the Simulation 3D Viewer window.

Open Model

Open the Simulink model.

open_system("CreateActorFrom3DGraphic");

Simulink model with Simulation 3D Actor block named cylinder and Simulation 3D Scene Configuration block

Explore Model Components

The model includes a Simulation 3D Actor block and a Simulation 3D Scene Configuration block. 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. Double-click the Simulation 3D Actor block to open the Block Parameters dialog box. To create an actor before simulation starts, on the Main tab, set the Operation parameter to Create at setup. The block first creates an empty actor with the name specified in the Actor name parameter. You can use any name for the actor. Then, the block loads the source file, if any is present, and runs the Initialization script. For more details, see Operating Modes. The Initialization script builds a cylinder shape for the actor using the createShape function. Set the size and use the Properties of the actor object to set the color of the cylinder in the Initialization script text box. On the Transform tab, you can also set the Initial position, Initial rotation and Actor scale of the actor.

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

Simulate Model

Simulate the model and view the virtual world with cylinder shape actor in the Simulation 3D Viewer.

sim("CreateActorFrom3DGraphic");

Cylinder actor in the virtual world.

Close Model

Close the Simulink model.

close_system("CreateActorFrom3DGraphic");

Build Actor from 3D Graphic Primitive Using MATLAB

You can use the sim3d.World and sim3d.Actor objects to create a virtual world with an actor. First, you create a virtual world and an actor object. Next, you build the actor from a 3D graphic using the createShape function. Then, you add the actor to the world, transform the actor, and set a view in the scene. Finally, you view the actor in the Simulation 3D Viewer window.

Create a world scene.

world = sim3d.World();

Instantiate an actor object named Cylinder. You can use any name for the actor.

ActObj = sim3d.Actor('ActorName','Cylinder');

Build a cylinder shape for the actor object and specify its size. Specify a color. Add the actor object to the world.

createShape(ActObj,'cylinder', [0.5, 0.5, .75]);
ActObj.Color = [1, 0, 1];
add(world,ActObj);

Set Actor Transformation

Use the actor object translation, rotation, and scale properties to orient the actor relative to the world origin.

ActObj.Translation = [0 0 0];
ActObj.Rotation = [0, 0, 0];
ActObj.Scale = [1, 1, 1];

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 = [-4.5, 0, 1];

Run Animation

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

run(world,0.02,10)

Cylinder actor in the virtual world.

Delete World

Delete the world object.

delete(world)

See Also

| | | | | |

Related Topics