Main Content

Visualize Aircraft Takeoff via Virtual Reality Animation Object

This example shows how to visualize aircraft takeoff and chase helicopter with the virtual reality animation object. In this example, you can use the Aero.VirtualRealityAnimation object to set up a virtual reality animation based on the asttkoff.wrl file. The scene simulates an aircraft takeoff. The example adds a chase vehicle to the simulation and a chase viewpoint associated with the new vehicle.

Note: This example is not supported in MATLAB Online.

Create the Animation Object

This code creates an instance of the Aero.VirtualRealityAnimation object.

h = Aero.VirtualRealityAnimation;

Set the Animation Object Properties

This code sets the number of frames per second and the seconds of animation data per second time scaling. 'FramesPerSecond' controls the rate at which frames are displayed in the figure window. 'TimeScaling' is the seconds of animation data per second time scaling.

The 'TimeScaling' and 'FramesPerSecond' properties determine the time step of the simulation. The settings in this example result in a time step of approximately 0.5s. The equation is:

(1/FramesPerSecond)*TimeScaling + extra terms to handle for sub-second precision.

h.FramesPerSecond = 10;
h.TimeScaling = 5;

This code sets the .wrl file to be used in the virtual reality animation.

h.VRWorldFilename = "asttkoff.wrl";

Initialize the Virtual Reality Animation Object

The initialize method loads the animation world described in the 'VRWorldFilename' field of the animation object. When parsing the world, node objects are created for existing nodes with DEF names. The initialize method also opens the Simulink 3D Animation viewer.

h.initialize();

Set Additional Node Information

This code sets simulation timeseries data. takeoffData.mat contains logged simulated data. takeoffData is set up as a 'StructureWithTime', which is one of the default data formats.

load takeoffData
[~, idxPlane] = find(strcmp('Plane', h.nodeInfo));
h.Nodes{idxPlane}.TimeseriesSource = takeoffData;
h.Nodes{idxPlane}.TimeseriesSourceType = 'StructureWithTime';

Set Coordinate Transform Function

The virtual reality animation object expects positions and rotations in aerospace body coordinates. If the input data is different, you must create a coordinate transformation function in order to correctly line up the position and rotation data with the surrounding objects in the virtual world. This code sets the coordinate transformation function for the virtual reality animation.

In this particular case, if the input translation coordinates are [x1,y1,z1], they must be adjusted as follows: [X,Y,Z] = -[y1,x1,z1].

You can see the custom transformation function in the file vranimCustomTransform.m.

h.Nodes{idxPlane}.CoordTransformFcn = @vranimCustomTransform;

Add a Chase Helicopter

This code shows how to add a chase helicopter to the animation object.

You can view all the nodes currently in the virtual reality animation object by using the nodeInfo method. When called with no output argument, this method prints the node information to the command window. With an output argument, the method sets node information to that argument.

h.nodeInfo;
Node Information
1	Camera1
2	Plane
3	_V2
4	Block
5	Terminal
6	_v3
7	Lighthouse
8	_v1

This code moves the camera angle of the virtual reality figure to view the aircraft.

set(h.VRFigure,'CameraDirection',[0.45 0 -1]);

Use the addNode method to add another node to the object. By default, each time you add or remove a node or route, or when you call the saveas method, Aerospace Toolbox displays a message about the current .wrl file location. To disable this message, set the 'ShowSaveWarning' property in the VirtualRealityAnimation object.

h.ShowSaveWarning = 0;
h.addNode('Lynx',fullfile(pwd,"chaseHelicopter.wrl"));

Another call to nodeInfo shows the newly added Node objects.

h.nodeInfo
Node Information
1	Camera1
2	Plane
3	_V2
4	Block
5	Terminal
6	_v3
7	Lighthouse
8	_v1
9	Lynx
10	Lynx_Inline

Adjust newly added helicopter to sit on runway.

[~, idxLynx] = find(strcmp('Lynx',h.nodeInfo));
h.Nodes{idxLynx}.VRNode.translation = [0 1.5 0];

This code sets data properties for the chase helicopter. The 'TimeseriesSourceType' is the default 'Array6DoF', so no additional property changes are needed. The same coordinate transform function (vranimCustomTransform) is used for this node as the preceding node. The previous call to nodeInfo returned the node index (2).

load chaseData
h.Nodes{idxLynx}.TimeseriesSource = chaseData;
h.Nodes{idxLynx}.CoordTransformFcn = @vranimCustomTransform;

Create New Viewpoint

This code uses the addViewpoint method to create a new viewpoint named 'chaseView'. The new viewpoint will appear in the viewpoint pulldown menu in the virtual reality window as "View From Helicopter". Another call to nodeInfo shows the newly added node objects. The node is created as a child of the chase helicopter.

h.addViewpoint(h.Nodes{idxLynx}.VRNode,'children','chaseView','View From Helicopter');

Play Animation

The play method runs the simulation for the specified timeseries data.

h.play();

Wait

Wait for the animation to stop playing before the modifying the object.

h.wait();

Play Animation from Helicopter

This code sets the orientation of the viewpoint via the vrnode object associated with the node object for the viewpoint. In this case, it will change the viewpoint to look out the left side of the helicopter at the plane.

[~, idxChaseView] = find(strcmp('chaseView',h.nodeInfo));
h.Nodes{idxChaseView}.VRNode.orientation = [0 1 0 convang(200,'deg','rad')];  
set(h.VRFigure,'Viewpoint','View From Helicopter');

Add ROUTE

This code calls the addRoute method to add a ROUTE command to connect the plane position to the Camera1 node. This will allow for the "Ride on the Plane" viewpoint to function as intended.

h.addRoute('Plane','translation','Camera1','translation');

This code plays the animation.

h.play();
h.wait();

Add Another Body

This code adds another helicopter to the scene. It also changes to another viewpoint to view all three bodies in the scene at once.

set(h.VRFigure,'Viewpoint','See Whole Trajectory');
h.addNode('Lynx1',"chaseHelicopter.wrl");

h.nodeInfo
Node Information
1	Camera1
2	Plane
3	_V2
4	Block
5	Terminal
6	_v3
7	Lighthouse
8	_v1
9	Lynx
10	Lynx_Inline
11	chaseView
12	Lynx1
13	Lynx1_Inline

Adjust newly added helicopter to sit above runway.

[~, idxLynx1] = find(strcmp('Lynx1',h.nodeInfo));
h.Nodes{idxLynx1}.VRNode.translation = [0 1.3 0];

Remove Body

This code uses the removeNode method to remove the second helicopter. removeNode takes either the node name or node index (as obtained from nodeInfo). The associated inline node is removed as well.

h.removeNode('Lynx1');

h.nodeInfo
Node Information
1	Camera1
2	Plane
3	_V2
4	Block
5	Terminal
6	_v3
7	Lighthouse
8	_v1
9	Lynx
10	Lynx_Inline
11	chaseView

Revert to Original World

The original filename is stored in the 'VRWorldOldFilename' property of the animation object. To bring up the original world, set 'VRWorldFilename' to the original name and reinitializing it.

h.VRWorldFilename = h.VRWorldOldFilename{1};  
h.initialize();

Close and Delete World

To close and delete

h.delete();

See Also

| |

Related Topics