Overview
This example creates a new driver. On start, this driver will load the Makland terrain database if it is not already loaded; create and initialize an example sim engine; and attach the main observer to the entity created by the sim engine. On tick, the driver will tick the sim engine. On stop, it will destroy the sim engine.
Expected Result
Example Driver Result
Example details
The example driver's onStart() method loads the database first.
The Driver
DtScene& sceneDriver = myAgentManager.de().scene();
std::string maklandPath = myAgentManager.de().dePathConfiguration().userPath();
maklandPath += "/terrains/Makland.mtf";
if ( sceneDriver.terrainFileName() != maklandPath )
{
sceneDriver.loadTerrain( maklandPath );
#ifdef SIM_IS_SHIP
myAgentManager.de().scene().terrain().addOceanLayer("Ocean");
DtOceanLayer* layer = myAgentManager.de().scene().terrain().findOceanLayer( "Ocean" );
layer->setSeaLevel( 10. );
DtEnvironment& env = myAgentManager.de().scene().environment();
env.setWindDrivenSeaState(false);
env.setSeaState( (int) DtDouglasSeaState::Slight );
env.setSeaStateDirection( 1.57 );
env.setSeaChoppiness( 0.8 );
#endif
}
It then creates the example sim engine and initializes it.
if ( ! myExampleSimEngine )
{
myExampleSimEngine->
init();
}
Finally, it attaches the current observer to the entity that the sim engine will create by calling attachObserverToEntity.
attachObserverToEntity( "Entity From Facade" );
attachObserverToEntity fills out a DtObserverStateRecord by getting the current state of the current observer, changing the attached state, and then calling DtObserver::setFromRecord on the same observer. setFromRecord has the special property that if an attachment is specified and the entity indicated is not yet available to attach to, the observer listen for signals that elements have been added, until the desired element is created.
DtObserver* currentObserver = myAgentManager.de().
driverManager().inputDriver().currentObserver();
currentObserver->getRecord( observerStart );
std::set<std::string> displayNames;
displayNames.insert( entityName );
currentObserver->setFromRecord( observerStart );
The example driver's onTick() method calls the sim engine's tick function, providing the current simulation time from the display engine.
myExampleSimEngine->tick( myAgentManager.de().simulationTime() );
The example driver's onStop() method call's the sim engine's uninit method and then deletes the sim engine if the sim engine exists (it may not, since onStop() may be called multiple times).
if ( myExampleSimEngine )
{
myExampleSimEngine->uninit();
delete myExampleSimEngine;
myExampleSimEngine = 0;
}
The Sim Engine
The example sim engine creates and updates an entity that includes an entity indicator and a height-above-terrain (HAT) line. The sim engine is constructed with a reference to the display engine (DtDe) which provides access to the agent manager for creating distributed objects; a coordinate system object for converting locations and orientations to the current coordinate system is use; the databank, so that the engine can report element creation and attribute changes; and the main event queue, to listen for mouse over signals for the entity indicator.
The init() method is responsible for creating and initializing the entity, entity indicator, entity information widget, and HAT line.
First, the sim engine creates a new entity facade and tells it to use the F16 Visual Definition.
myEntityDisplayName = "Entity From Facade";
DtElementID elementId = mySimulationConnection.sceneInterface().createNewUniqueId();
myEntityFacade = new DtEntity3dFacade( mySimulationConnection.sceneInterface(),
mySimulationConnection.settingsManager(), elementId, true );
myEntityFacade->setArticulatedModelDefinition("FixedWingRQ-1Predator");
It creates the entity indicator, and adds the new model agent to the existing entity facade's scene object (so that it will be positioned in the same place).
myEntityIndicator =
new DtEntityIndicatorWidget( mySimulationConnection.sceneInterface(), myEntityFacade->modelSet() );
myEntityIndicator->setEventsEnabled( true );
myEntityIndicator->setColor( 0, 0, 1, 1 );
setHoverSignalsActive( true );
myEntityFacade->sceneObjectAgent().addModel( myEntityIndicator->model(),
It creates a HAT line using the DtEntityHATLineFacade, which will create the necessary models and attach to the entity's scene object.
myEntityHATLineFacade = new DtEntityHATLineFacade( mySimulationConnection.sceneInterface(), myEntityFacade->modelSet(),
myEntityFacade->sceneObjectAgent().uniqueId() );
init() then connects to the selection manager's signal_entityInfoToggled.
As labels are pinned or unpinned, this will cause slot_toggleLabelPinnedState to be called. This method updates the visibility of both the indicator and the info label, and also tells the sim to listen or stop listening to the mouse hover signals (detailed below).
The example driver updates the sim engine by calling its tick() method. The sim engine's tick() method updates the F16's position using updatePosition(), and updates the text displayed on the F16's entity info label.
The driver's generateInfoString() method essentially performs some formatting and unit conversion on the information it has about the aircraft's position and orientation in the scene. The value returned from this method is then simply set on the entity info widget (if there is one present - if the mouse isn't hovering, there won't be an info widget).
The driver's updatePosition() method first determines how long it's been since the last update, and updates the aircraft's clock angle based on its angular velocity. The clock angle is the bearing to the aircraft from the point at the center of the aircraft's orbit (0 for north, and increasing in the clockwise direction). It then determines the aircraft's offset from the center of its orbit.
The aircraft's orbit is circular, so computing the heading is as easy as adding 90 degrees (Pi/2) to the clock angle. Once this has been done, the heading and roll can be used to determine the net orientation of the F16.
Now that the F16's position and orientation have been computed, the aircraft can be repositioned in the scene.
Finally, the current time is set as the last update time in preparation for the next update.
Positioning the aircraft takes place in the repositionAircraft(..) method. This method first determines the type of coordinate system using the DtCoordinateConverter, then converts the position and orientation to local database (CIG) coordinates. Finally, the CIG coordinates are set on the entity facade, performing the actual reposition.
DtCoordinateConverter* coordConverter;
coordConverter = mySimulationConnection.coordinateSystem().converter();
if ( coordConverter->type() == DtCoordinateConverter::Geocentric )
{
DtGeodeticCoord geod( DtDeg2Rad( 10.0 ), DtDeg2Rad(60.0), 0.0 );
coordConverter->setupTopoFrame( geod.geocentric() );
}
else
{
coordConverter->setupTopoFrame( position );
}
coordConverter->enuToCig_coordTrans( position, myPosition );
myOrientation = coordConverter->nedToCig_EulerTrans( ori );
myEntityFacade->setOrientation( myOrientation );
myEntityFacade->setPosition( myPosition );
The sim engine is unitialized by calling its uninit() method. This cleans up the entity facade, indicator, info widget, and HAT line allocated on the heap. It also tells the DataBank that the element has been destroyed.
if ( myEntityFacade )
{
if ( myEntityInfoWidget )
{
myEntityFacade->sceneObjectAgent().removeModel(
myEntityInfoWidget->model() );
delete myEntityInfoWidget;
myEntityInfoWidget = 0;
}
if ( myEntityIndicator )
{
setHoverSignalsActive( false );
myEntityFacade->sceneObjectAgent().removeModel(
myEntityIndicator->model() );
delete myEntityIndicator;
myEntityIndicator = 0;
}
mySimulationConnection.reportElementDestroyed(
myEntityFacade->elementId() );
delete myEntityFacade;
myEntityFacade = 0;
}
return true;
The example driver is created, added to the driver manager, and started in the main program.
Building the Example
VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
Running the Example
This example is an application. You can run it by running ./bin64/exampleDistributedObject.exe (on Windows) or ./bin64/exampleDistributedObject (on Linux). Press the "s" key to get away from the entity and see the HAT line. For more information about running examples, please see Running Applications and Examples.
Example Source Files
exampleDriver.cxx
#ifdef SIM_IS_SHIP
#endif
using namespace makVrv;
: makVrv::
DtDriver(am,
"DtExampleDriver")
, myExampleSimEngine(0)
{
}
{
}
{
static std::string name = "DtExampleDriver";
return name;
}
{
maklandPath += "/terrains/Makland.mtf";
{
#ifdef SIM_IS_SHIP
#endif
}
{
}
#ifdef SIM_IS_SHIP
#else
#endif
return true;
}
{
{
}
return true;
}
{
{
}
return true;
}
{
driverManager().inputDriver().currentObserver();
std::set<std::string> displayNames;
displayNames.insert(entityName);
}
{
}
DtExampleDriver.h
#pragma once
#include <string>
{
public:
virtual const std::string&
className()
const;
protected:
protected:
};
DtExampleDriver.cxx
#include "DtExampleSimEngine.h"
#ifdef SIM_IS_SHIP
#endif
using namespace makVrv;
: makVrv::
DtDriver( am,
"DtExampleDriver" )
, myExampleSimEngine( 0 )
{
}
{
}
{
static std::string name = "DtExampleDriver";
return name;
}
{
maklandPath += "/terrains/Makland.mtf";
{
#ifdef SIM_IS_SHIP
#endif
}
{
}
return true;
}
{
{
}
return true;
}
{
{
}
return true;
}
{
driverManager().inputDriver().currentObserver();
std::set<std::string> displayNames;
displayNames.insert( entityName );
}
{
}
[<< Examples] [Home] [Top of Page]
Overview
This example creates a new driver. This driver will load the Makland terrain database and create an F-16 entity on start, attach the main observer to it, and then move the F16 a small amount each time that it's ticked.
Expected Result
Threaded Driver Result
Example details
The example driver's onStart() method loads the database first.
DtScene& sceneDriver = myAgentManager.de().scene();
It then creates a new entity facade and tells it to use the F16 Visual Definition.
Using the coordinate system information provided by the loaded terrain database, the driver provides an initial position and orientation in the coordinate system of the database.
As labels are pinned or unpinned, the togglePinnedLabel slot is called. This slot updates the visibility of both the indicator and the info label, and also tells the driver to listen or stop listening to the mouse hover signals (detailed below).
The driver manager updates the example driver by calling its onTick() method. The driver's onTick() method updates the F16's position using updatePosition(), and updates the text displayed on the F16's entity info label.
The driver's generateInfoString() method essentially performs some formatting and unit conversion on the information it has about the aircraft's position and orientation in the scene. The value returned from this method is then simply set on the entity info widget (if there is one present - if the mouse isn't hovering, there won't be an info widget).
The driver's updatePosition() method first determines how long it's been since the last update, and updates the aircraft's clock angle based on its angular velocity. The clock angle is the bearing to the aircraft from the point at the center of the aircraft's orbit (0 for north, and increasing in the clockwise direction). It then determines the aircraft's offset from the center of its orbit.
The aircraft's orbit is circular, so computing the heading is as easy as adding 90 degrees (Pi/2) to the clock angle. Once this has been done, the heading and roll can be used to determine the net orientation of the F16.
Now that the F16's position and orientation have been computed, the aircraft can be repositioned in the scene. Finally, the current time is set as the last update time in preparation for the next update.
Positioning the aircraft takes place in the repositionAircraft(..) method. This method first determines the type of coordinate system using the DtCoordinateConverter, then converts the position and orientation to local database (CIG) coordinates. Finally, the CIG coordinates are set on the entity facade, performing the actual reposition.
The driver is stopped by the driver manager callings its onStop() method. This cleans up the entity facade allocated on the heap.
The example driver is created, added to the driver manager, and started in the main program.
Building the Example
VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
Running the Example
This example is an application. You can run it by running ./bin64/exampleThreadedDriver.exe (on Windows) or ./bin64/exampleThreadedDriver (on Linux). For more information about running examples, please see Running Applications and Examples.
Example Source Files
DtExampleSimEngine.cxx
#include <DtExampleSimEngine.h>
#include <vrvCore/DtSceneObjectAgent.hpp>
using namespace makVrv;
: mySimulationConnection( connection )
, myEntityFacade( 0 )
, myEntityIndicator( 0 )
, myEntityInfoWidget( 0 )
, myEntityHATLineFacade( 0 )
, myEntityDisplayName( "" )
, myEntityInfoWidgetPinnedFlag( false )
, myPosition()
, myOrientation()
, myOrbitCenter( 2000.0, 1400.0, 350.0 )
, myAngularVelocity( 0.2 )
, myLastCommLineTime( 0.0 )
, myLastUpdateTime( 0.0 )
, myClockAngle( 0.0 )
, myHeading( 0.0 )
, myRadius( 1000.0 )
, myRoll( 0.5 )
{
}
{
DtStateVisualizer::EntityVisualizerType );
return true;
}
{
{
{
}
{
}
}
return true;
}
{
{
}
return true;
}
{
widgetSignaler );
if ( active )
{
}
else
{
}
}
{
{
DtStateVisualizer::EntityVisualizerType );
}
{
}
}
{
DtReferenceEllipsoid refEllip( DtWGS84 );
DtGeodeticCoord geod( &refEllip );
DtVector localp;
DtTaitBryan hpr;
const char* entityFormatString =
"Marking :%s\n"
"Heading :%.2f Deg\n"
"Lat|Lon :%.2fN %.fE\n"
"Alt. :%.1f m above sea level";
std::string buffer;
buffer.resize( 1024, '0' );
{
int charsWritten = sprintf(
const_cast<char*>(buffer.c_str()), entityFormatString,
DtRad2Deg(geod.lat()),
DtRad2Deg(geod.lon()),
);
buffer.resize(charsWritten,'0');
}
return DtUnicode::fromUtf8(buffer);
}
{
double now = simTime;
myLastUpdateTime = now;
}
{
if ( coordConverter->
type() == DtCoordinateConverter::Geocentric )
{
DtGeodeticCoord geod( DtDeg2Rad( 10.0 ), DtDeg2Rad(60.0), 0.0 );
}
else
{
}
}
{
DtSelectionManager::IdSet::const_iterator i = sel.begin();
DtSelectionManager::IdSet::const_iterator e = sel.end();
{
{
}
}
}
DtExampleSimEngine.h
#pragma once
#include <matrix/vlTaitBryan.h>
#include <matrix/vlVector.h>
#include <string>
namespace makVrv
{
class DtEntity3dFacade;
class DtEntityHATLineFacade;
}
{
public:
bool tick(
double simTime );
protected:
protected:
};
DtExampleSimulationConnection.cxx
#include "DtExampleSimEngine.h"
using namespace makVrv;
, myCoordinateSystem(coordSystem.clone())
, myExampleSimEngine( 0 )
, mySimTime( 0.0 )
{
}
{
}
{
}
{
}
{
}
{
{
}
}
{
}
{
}
{
}
DtExampleSimulationConnection.h
#pragma once
namespace makVrv
{
class DtCoordinateSystem;
class DtAgentManagerInterface;
class DtSharedSettingsManager;
}
{
public:
protected:
};
DtExampleThreadedDriver.cxx
#include <vlutil/vlRunnable.h>
#include <vlutil/vlProcessControl.h>
#include <vlutil/vlPerformanceStats.h>
using namespace makVrv;
:
DtDriver( am,
"DtExampleThreadedDriver" )
, myClassName("DtExampleThreadedDriver")
, myUseThreadFlag(true)
, myThread(0)
, myConnectionThreadFunctions()
, mySimulationConnection(0)
, myAsynchronousEventQueue(0)
, mySimulationConnectionState(false)
{
if ( myUseThreadFlag )
{
myAsynchronousInterface =
myAgentManager.createAsynchronousManager(instanceName());
}
}
{
}
{
}
{
}
{
maklandPath += "/terrains/Makland.mtf";
{
}
{
return false;
}
{
}
else
{
}
return true;
}
{
{
}
else
{
}
{
}
return true;
}
{
{
}
else
{
{
}
}
}
{
}
{
myDynamicSignals.disconnectSignals();
}
{
queue.getForReading(functions);
if(functions->size() > 0)
{
FunctionCallList::iterator curIter = functions->begin();
FunctionCallList::iterator endIter = functions->end();
for(;curIter != endIter;++curIter)
{
(*curIter)();
}
}
}
{
{
return;
}
{
}
myStart = DtThreadStartSP(
new DtThreadStart(
this ));
if (
myThread->start() != DtThreadSuccess )
{
DtTHROW_NEW( DtCorruptedState, "Unable to start Connection thread." );
}
}
{
if(!connected)
{
return;
}
DtTimedSection ts;
double desiredFrameTime = .01667;
while(!timeToStop())
{
ts.start();
try
{
}
catch(...)
{
break;
}
{
}
ts.stop();
if(ts.duration() < desiredFrameTime )
{
DtSleep(desiredFrameTime - ts.duration());
}
}
{
}
}
{
{
DtYield();
{
DtYield();
}
}
}
{
{
}
*simSceneInterface,
DtSharedSettingsManager::instance(de));
return connection;
}
{
}
{
}
{
}
{
}
{
driverManager().inputDriver().currentObserver();
std::set<std::string> displayNames;
displayNames.insert( entityName );
}
{
{
DtDriver::stop();
DtDriver::start();
}
}
{
}
DtExampleThreadedDriver.h
#pragma once
#include <vlutil/vlRunnable.h>
#include <boost/function.hpp>
#include <boost/signal.hpp>
#include <vlutil/vlThread.h>
#include <string>
namespace makVrv
{
class DtAsynchronousAgentManager;
class DtAsynchronousEventQueue;
}
class DtThread;
class DtThreadStart;
{
public:
virtual const std::string&
className()
const;
protected:
protected:
};
exampleThreadedDriver.cxx
int main( int argc, char* argv[] )
{
application.installCommandLineProcessor( &stealthParser );
application.initialize( argc, argv );
application.de().driverManager().addDriver( driver );
application.de().driverManager().startDriver( driver );
return 0;
}
[<< Examples] [Home] [Top of Page]