Main Content

Animate Actor with Gravity Property

This example shows how to animate an actor by enabling physics and gravity properties using either Simulink® or MATLAB®. You build a ball to bounce. First, you create a world scene and build an actor. Then, you create a moveable actor and set a view in the scene. Finally, you view the animation in the Simulation 3D Viewer window.

Unreal Engine® defaults to using the PhysX® engine to control actor motion and perform real-time physics calculations when the physics property of an actor is enabled.

Animate Actor with Gravity Property Using Simulink

You can use Simulation 3D Actor block and Simulation 3D Scene Configuration block to create and animate a ball actor in the virtual world using physics and gravity properties.

Open Model

Open the Simulink model.

open_system("AnimateBallWithGravity");

Simulink model with two Simulation 3D Actor blocks named ball and ground and a Simulation 3D Scene Configuration block.

Explore Model Components

The model includes two Simulation 3D Actor blocks 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 actors named Ball and Ground to the virtual world. Double-click the Simulation 3D Actor block named Ball 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 sphere shape for the actor using the createShape function. Specify the size and use the Properties of the actor object to set color of the actor. Set the properties Mobility, Gravity, and Physics to animate the actor.

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

Double-click the Simulation 3D Actor block named Ground to open the Block Parameters dialog box. Build a plane shape for the actor using the createShape function in the Initialization script text box. Specify the size and set the PreciseContacts property of the actor for accurate collision of the ball actor with the plane actor.

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

Simulate Model

Simulate the model and view the animation of a ball in the Simulation 3D Viewer.

sim("AnimateBallWithGravity");

Ball actor and plane actor in virtual world scene.

Close Model

Close the Simulink model.

close_system("AnimateBallWithGravity");

Animate Actor with Gravity Property Using MATLAB

You can use the sim3d.World and sim3d.Actor objects to create and animate a ball actor in a virtual world using physics and gravity properties.

Create a World Scene

Create a world scene.

world = sim3d.World();

Build Plane and Movable Ball Actor

Build a ball actor using the createShape function and set the Properties of the actor object. Set the Mobility, Gravity, and Physics properties. Then, build a plane actor. Set the PreciseContacts property of the actor for accurate collision of the ball actor with the plane actor. Add the actors to the world.

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

ball.Mobility = sim3d.utils.MobilityTypes.Movable;
ball.Gravity = true;
ball.Physics = true;
ball.Translation = [0, 0, 5];
ball.Color = [.77 0 .255];
ball.Shadows = false;

plane = sim3d.Actor();
plane.createShape('plane', [5 5 0.1]);
plane.PreciseContacts = true;
world.add(plane);

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);

Run Animation

Run an animation set for 10 seconds with a sample time of 0.01 seconds.

run(world,0.01,10)

Ball actor and plane actor in the virtual world

Delete World

Delete the world object.

delete(world)

See Also

| | | | | |

Related Topics