Main Content

Simulate Actor with Kinematics Properties Using MATLAB

This example shows how to simulate an actor in Unreal Engine® simulation environment by setting kinematics properties using MATLAB®. You build sphere and box actors to animate. To simulate an actor in Unreal Engine® simulation environment by setting kinematics properties using Simulink®, see Simulate Actor with Kinematics Properties Using Simulink.

First, you create a 3D environment using sim3d.World and build the actors using sim3d.Actor classes. 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.

Create 3D Environment

Create a world object.

world = sim3d.World();

Build Actors

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.

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

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

Set Movable Actor

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

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

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

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

Run Animation

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

sampletime = 0.02;
stoptime = 10;
run(world,sampletime,stoptime);

Sphere actor and box actor in the virtual world.

Delete World

Delete the world object.

delete(world)

See Also

| | | |

Related Topics