%% Record Virtual Reality Animation Object Simulation
% This example shows how to 
% 
% * Record the simulation of a virtual reality animation object
% * Simulate and record flight data
% * Create an animation object

h = Aero.VirtualRealityAnimation;

% Control the frame display rate.

h.FramesPerSecond = 10;

% Configure the animation object to set the seconds of animation data per 
% second time scaling (TimeScaling) property.

h.TimeScaling = 5;

% The combination of FramesPerSecond and TimeScaling property determine the 
% time step of the simulation. These settings result in a time step of 
% approximately 0.5 s.
% This code sets the .wrl file to use in the virtual reality animation.

h.VRWorldFilename = [matlabroot,'/toolbox/aero/astdemos/asttkoff.wrl'];

% Copy the .wrl file to a temporary directory and set the world file name 
% to the copied .wrl file.

copyfile(h.VRWorldFilename,[tempdir,'asttkoff.wrl'],'f');
h.VRWorldFilename = [tempdir,'asttkoff.wrl'];

% Load the animation world described in the 'VRWorldFilename' field of the 
% animation object. 

h.initialize();

% Set 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';

% Use the example custom function vranimCustomTransform 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.

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

% Set up recording properties.

h.VideoRecord = 'on';
h.VideoQuality = 50;
h.VideoCompression = 'Motion JPEG AVI'
h.VideoFilename = 'astMotion_JPEG_VR';

% Play the animation.

h.play();

% Verify that a file named astMotion_JPEG_VR.avi was created in the current folder.
% Disable recording to preserve the file.

h.VideoRecord = 'off';

%% 
% Copyright 2012 The MathWorks, Inc.