Main Content

Build Actor from 3D Graphic Primitives Using MATLAB

This example shows how to build an actor from a 3D graphic primitive using MATLAB®.

You can use the sim3d.World and sim3d.Actor objects to create a 3D environment with an actor. First, you create a 3D environment and an actor object. Next, you build the actor from a 3D graphic primitives, including box, cylinder, plane, and sphere 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.

To build an actor from a 3D graphic primitive using Simulink®, see Build Actor from 3D Graphic Primitives Using Simulink.

Create 3D Environment

Create a world object.

world = sim3d.World();

Create Actor

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