Simulate Actor with Gravity Property Using MATLAB

This example shows how to simulate an actor by enabling physics and gravity properties in the Unreal Engine® simulation environment using MATLAB®. You build a ball to bounce from a plane. First, you create a 3D environment with world object using `sim3d.World` class and build an actor object using `sim3d.Actor` class. Then, you create a moveable actor and set a view in the scene. Finally, you view the animation in the Simulation 3D Viewer window. To simulate an actor by enabling physics and gravity properties in the Unreal Engine® simulation environment using Simulink®, see Simulate Actor with Gravity Property Using Simulink.

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

Create 3D Environment

Create a world object.

`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(); createShape(ball,'sphere',[0.5 0.5 0.5]); 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; add(world,ball); plane = sim3d.Actor(); createShape(plane,'plane', [5 5 0.1]); plane.PreciseContacts = true; add(world,plane);```

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

Run Animation

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

```sampletime = 0.01; stoptime = 10; run(world,sampletime,stoptime)```

Delete World

Delete the world object.

`delete(world)`