Main Content

Animate Actor Using Kinematics Properties

This example shows how to animate an actor by setting kinematics properties using either Simulink® or MATLAB®. You build sphere and box actors to animate.

First, you create a world scene and build the actors. Then, you set actor properties to enable mobility and set a view in the scene. Finally, you view the animation in the Simulation 3D Viewer window.

Animate Actor with Kinematics Properties Using Simulink

You can use the Simulation 3D Actor block and Simulation 3D Scene Configuration block to create and animate actors in a virtual world using kinematics properties.

Open Model

Open the Simulink model.

open_system("AnimateActorWithKinematics");

Simulink model with two Simulation 3D Actor blocks named sphere and box and a 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. When building Simulink models with Simulation 3D Actor blocks, you must include the Simulation 3D Scene Configuration block.

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 Operation 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 block parameter dialog box of the Simulation 3D Actor block named box shows parameters and initialization script

The Initialization script for the Sphere block builds a sphere shape and the Initialization script for the Box block builds a box shape for the actors. Each script sets a specific size using the createShape function. You can use the Properties of an actor object to set the Color and Mobility to make the actors movable. Specify the LinearVelocity value for the Sphere actor and AngularVelocity value for the Box actor to animate each actor at the specified value. On the Transform tab, you can also set the Initial position, Initial rotation and Actor scale for each actor.

Simulate Model

Simulate the model and view the animation of the sphere and box in the Simulation 3D Viewer.

sim("AnimateActorWithKinematics");

Sphere actor and box actor in the virtual world.

Close Model

Close the Simulink model.

close_system("AnimateActorWithKinematics");

Animate Actor with Kinematics Properties Using MATLAB

You can use the sim3d.World and sim3d.Actor objects to create and animate actors in a virtual world using kinematics properties.

Build Actor in World

Create a world scene.

world = sim3d.World();

Instantiate the actor objects named sphere and box. You can use any name for the actors. Use the createShape function to build sphere and box shapes for the actor objects and specify the size. Add the actor objects to the world.

ActObj1 = sim3d.Actor('ActorName','sphere');
createShape(ActObj1,'sphere', [0.5 0.5 0.5]);
add(world,ActObj1);

ActObj2 = sim3d.Actor('ActorName','box');
createShape(ActObj2,'box', [0.5 0.5 0.5]);
add(world,ActObj2);

Set Movable Actor

Use the sim3d.Actor object properties to create a movable actor. Use the kinematics properties LinearVelocity and AngularVelocity to move and rotate the actors ActObj1 and ActObj2, respectively.

ActObj1.Translation = [1, -2, 1];
ActObj1.Color = [0, 1, 1];
ActObj1.Mobility = sim3d.utils.MobilityTypes.Movable;
ActObj1.LinearVelocity = [0, 0.5, 0];

ActObj2.Translation = [1, 0, -1];
ActObj2.Color = [1, 0, 1];
ActObj2.Mobility = sim3d.utils.MobilityTypes.Movable;
ActObj2.AngularVelocity = [0, 0, pi/4];

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.

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

Run Animation

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

run(world,0.02,10)

Sphere actor and box actor in the virtual world.

Delete World

Delete the world object.

delete(world)

See Also

| | | | | |

Related Topics