Overview
Show how fire a projectile (a Tank!) using the F key and create an explosion
Expected Result
Projectile over ground Result
Projectile over water Result
Example details
This example shows how to fire a projectile from the observer view point. It will also detect if the impact is on ground or water and display the appropriate explosion (smoke or water splash). Currently the projectile type is amplified by using a tank! The commented code is also present to use a more realistic projectile.
Building the Example
VR-Vantage includes pre-built versions of the example plug-in. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.
Running the Example
This example is a plug-in. You can run it by running ./bin64/exampleProjectile_stealth.bat (on Windows) or ./bin64/exampleProjectile_stealth (on Linux). Press 'f' to fire the projectile. For more information about running examples, please see Running Applications and Examples.
Example Source Files
DtExampleProjectile.cxx
{
:
makVrv::DtDriver( am,
"DtExampleProjectile" )
, myObserverShooter( 0 )
{
}
DtExampleProjectile::~DtExampleProjectile()
{
DtExampleProjectile::onStop();
}
const std::string& DtExampleProjectile::className() const
{
static std::string name = "DtExampleProjectile";
return name;
}
bool DtExampleProjectile::onStart()
{
DtScene& sceneDriver = myAgentManager.de().scene();
std::string groundDbPath = myAgentManager.de().dePathConfiguration().sharedDataPath();
groundDbPath += "/TerrainData/TerrainConfiguration/Ground_DB II.mtf";
if ( sceneDriver.terrainFileName() != groundDbPath )
{
sceneDriver.loadTerrain( groundDbPath );
}
myObserverShooter = new DtObserverShooter( myAgentManager.de() );
myObserverShooter->setModelDefinitionName( "TrackedM1Abrams" );
myObserverShooter->setProjectileSpeed( 100. );
myObserverShooter->setIntersectionCallback(
boost::bind( &DtExampleProjectile::processIntersection, this, boost::placeholders::_1, boost::placeholders::_2 ) );
return true;
}
bool DtExampleProjectile::onStop()
{
if ( myObserverShooter )
{
delete myObserverShooter;
myObserverShooter = 0;
}
DetonationList::const_iterator detIter = myDetonationObjects.begin();
DetonationList::const_iterator endDetIter = myDetonationObjects.end();
for( ; detIter != endDetIter; ++detIter )
{
if ( ! myAgentManager.de().destructing() )
{
DetonationData detonation = *detIter;
detonation.detonationObject->removeModel( detonation.detonationModel );
delete detonation.detonationModel;
delete detonation.detonationObject;
}
}
myDetonationObjects.clear();
SplashList::const_iterator splashIter = mySplashObjects.begin();
SplashList::const_iterator endSplashIter = mySplashObjects.end();
for( ; splashIter != endSplashIter; ++splashIter )
{
if ( ! myAgentManager.de().destructing() )
{
SplashData splash = *splashIter;
splash.splashObject->removeModel( splash.splashModel );
delete splash.splashModel;
delete splash.splashObject;
}
}
mySplashObjects.clear();
return true;
}
bool DtExampleProjectile::onTick()
{
if ( myObserverShooter )
{
myObserverShooter->tick( myAgentManager.de().simulationTime() );
}
return true;
}
void DtExampleProjectile::slot_displayEngineAdded( const DtDeRecord& igRecord )
{
stop();
start();
}
void DtExampleProjectile::processIntersection( const DtIntersectorResult& isectResult, double speed )
{
std::cout << "Hit: "<< isectResult.x << " " << isectResult.y << " " << isectResult.z;
if ( isectResult.dynamicOcean )
{
createSplash( DtVector( isectResult.x, isectResult.y, isectResult.z ), speed );
std::cout << " Type: " << "Ocean" << std::endl;
}
else
{
DtElementEntry* entry = myAgentManager.de().dataBank().elementData().findElement( isectResult.id );
if ( entry )
{
switch( entry->type() )
{
case DtElementEntry::Entity:
createDetonation( DtVector( isectResult.x, isectResult.y, isectResult.z ) );
std::cout << " Type: " << "Entity" << std::endl;
break;
case DtElementEntry::Prop:
createDetonation( DtVector( isectResult.x, isectResult.y, isectResult.z ) );
std::cout << " Type: " << "Prop" << std::endl;
break;
case DtElementEntry::TerrainPatch:
createDetonation( DtVector( isectResult.x, isectResult.y, isectResult.z ) );
std::cout << " Type: " << "Terrain" << std::endl;
break;
default:
std::cout << " Type: " << entry->type() << std::endl;
break;
}
}
else
{
std::cout << " Type: Unregistered Object" << std::endl;
}
}
}
void DtExampleProjectile::createDetonation( const DtVector& position )
{
DtSceneObjectAgent* sceneObj = DtSceneObjectAgent::create( myAgentManager );
sceneObj->setModelSet( DtModelSetId::Models3D );
sceneObj->setPosition( 0, DtVector( position ) );
DtParticleSystemModelAgent* detonation = DtParticleSystemModelAgent::create( myAgentManager );
sceneObj->addModel( detonation, 0 );
detonation->setModelDefinition( "DetonationBombBlastLarge" );
DetonationData data;
data.detonationObject = sceneObj;
data.detonationModel = detonation;
data.timeOfOccurence = myAgentManager.de().simulationTime();
myDetonationObjects.push_back( data );
}
void DtExampleProjectile::createSplash( const DtVector& position, double speed )
{
DtSceneObjectAgent* sceneObj = DtSceneObjectAgent::create( myAgentManager );
sceneObj->setModelSet( DtModelSetId::Models3D );
DtWaterImpactModelAgent* splash = DtWaterImpactModelAgent::create( myAgentManager );
sceneObj->addModel( splash, 0 );
splash->setModelDefinition( "DetonationWaterImpactExtraLarge" );
splash->setImpactLocation( position );
splash->setMunitionSpeed( speed );
SplashData data;
data.splashObject = sceneObj;
data.splashModel = splash;
data.timeOfOccurence = myAgentManager.de().simulationTime();
mySplashObjects.push_back( data );
}
}
Contains makVrv:DtAgentManager class.
Contains makVrv::DtDataBank class.
Contains makVrv::DtDe class.
Contains makVrv::DtDeSharedState class.
Contains makVrv:DtMasterScene class.
Contains the DtExampleProjectile class declaration.
Contains makVrv:DtIntersector class.
Contains makVrv::DtObserver class.
Contains vrvCore::DtObserverMode class.
Contains the DtObserverModeManager class declaration.
Contains makVrv::DtObserverSettingsManager class.
Contains the DtProjectile class definition.
Contains makVrv::DtScene class.
Contains makVrv::DtSceneObject class.
DtExampleProjectile(makVrv::DtAgentManager &am)
CTOR.
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
DtExampleProjectile.h
#pragma once
#include <matrix/vlVector.h>
#include <string>
#include <list>
{
class DtObserverShooter;
class DtIntersectorResult;
class DtSceneObjectAgent;
class DtParticleSystemModelAgent;
class DtWaterImpactModelAgent;
{
public:
virtual ~DtExampleProjectile();
virtual const std::string& className() const;
protected:
virtual bool onStart();
virtual bool onStop();
virtual bool onTick();
virtual void processIntersection( const DtIntersectorResult& isect, double speed );
virtual void createDetonation( const DtVector& position );
virtual void createSplash( const DtVector& position, double speed );
protected:
DtObserverShooter* myObserverShooter;
struct DetonationData
{
DtSceneObjectAgent* detonationObject;
DtParticleSystemModelAgent* detonationModel;
double timeOfOccurence;
};
struct SplashData
{
DtSceneObjectAgent* splashObject;
DtWaterImpactModelAgent* splashModel;
double timeOfOccurence;
};
typedef std::list<DetonationData> DetonationList;
typedef std::list<SplashData> SplashList;
DetonationList myDetonationObjects;
SplashList mySplashObjects;
};
}
Contains makVrv:DtDriver class.
The DtAgentManager manages the distributed scene object framework. This includes the objects and upda...
Definition DtAgentManager.h:39
A Record of an IG instance.
Definition DtDeRecord.h:25
The DtDriver is an abstract class. Developers derive from this to create logic that "directs" things ...
Definition DtDriver.h:32
#define DT_DLL_EXAMPLEPROJECTILE
Definition exampleProjectile.h:20
DtObserverShooter.cxx
#include <osg/Matrixd>
{
: myDe( de )
, myModelDefinitionName( "" )
, myProjectileSpeed( 100. )
, myExpiredProjectiles()
, myIntersectionCallback( (DtProjectile::IntersectionCallback) 0 )
{
DtInputDriver& inputDriver = de.driverManager().inputDriver();
DtKeyMapManager& keyMapManager = DtKeyMapManager::instance(de);
keyMapManager.addKeyFunction( "Fire Projectile", boost::bind(
&DtObserverShooter::fireProjectile, this), DtKeyMapManager::Miscellaneous);
DtKeyMap* obsFrame = keyMapManager.findKeyMap( "Observer Frame" );
if ( obsFrame )
{
inputDriver.addKeyBinding( obsFrame, DtKeyState::KEY_F, "Fire Projectile" ,
DtKeyState::NO_MODIFIER, true);
}
}
DtObserverShooter::~DtObserverShooter()
{
ProjectileMap::iterator curIter = myProjectiles.begin();
ProjectileMap::iterator endIter = myProjectiles.end();
for( ; curIter != endIter; ++curIter )
{
delete curIter->second;
}
myProjectiles.clear();
DtKeyMapManager& keyMapManager = DtKeyMapManager::instance( myDe );
keyMapManager.removeKeyFunction( "Fire Projectile" );
}
void DtObserverShooter::fireProjectile()
{
DtObserver* obs = myDe.driverManager().inputDriver().findObserverByName( "Observer 1" );
if ( obs )
{
DtVector start = obs->location();
double yaw, pitch, roll;
obs->getOrientation( yaw, pitch, roll );
DtTaitBryan orientation( -yaw, pitch, roll );
DtVector direction = obs->observerCameraFront();
DtProjectile* proj = new DtProjectile( myDe, start, orientation, myProjectileSpeed, direction,
myModelDefinitionName, myIntersectionCallback );
myProjectiles[proj->id()] = proj;
}
}
void DtObserverShooter::setModelDefinitionName( const std::string& modelDefinitionName )
{
myModelDefinitionName = modelDefinitionName;
}
void DtObserverShooter::setProjectileSpeed( double speed )
{
myProjectileSpeed = speed;
}
void DtObserverShooter::setIntersectionCallback( DtProjectile::IntersectionCallback callback )
{
myIntersectionCallback = callback;
}
void DtObserverShooter::removeExpiredProjectiles()
{
std::list<ProjectileMap::iterator> removeList;
ProjectileIDList::iterator curIter = myExpiredProjectiles.begin();
ProjectileIDList::iterator endIter = myExpiredProjectiles.end();
for( ; curIter != endIter; ++curIter )
{
DtUniqueID projectile = *curIter;
ProjectileMap::iterator findIter = myProjectiles.find( projectile );
if ( findIter != myProjectiles.end() )
{
delete findIter->second;
removeList.push_back( findIter );
}
}
std::list<ProjectileMap::iterator>::iterator delIter = removeList.begin();
std::list<ProjectileMap::iterator>::iterator endDelIter = removeList.end();
for ( ; delIter != endDelIter; ++delIter )
{
myProjectiles.erase( *delIter );
}
}
void DtObserverShooter::tick( double simTime )
{
removeExpiredProjectiles();
ProjectileMap::iterator curIter = myProjectiles.begin();
ProjectileMap::iterator endIter = myProjectiles.end();
for( ; curIter != endIter; ++curIter )
{
if ( ! curIter->second->tick( simTime ) )
{
myExpiredProjectiles.push_back( curIter->first );
}
}
}
}
DtObserverShooter(DtDe &de)
CTOR.
DtObserverShooter.h
#pragma once
#include <matrix/vlVector.h>
#include <matrix/vlTaitBryan.h>
#include <unordered_map>
#include <boost/function.hpp>
#include <list>
#include <string>
{
class DtDe;
{
public:
DtObserverShooter( DtDe& de );
virtual ~DtObserverShooter();
void fireProjectile();
void setModelDefinitionName( const std::string& modelDefinitionName );
void setProjectileSpeed( double speed );
void tick( double simTime );
void removeExpiredProjectiles();
void setIntersectionCallback( DtProjectile::IntersectionCallback callback );
protected:
typedef std::unordered_map<DtUniqueID, DtProjectile*> ProjectileMap;
typedef std::list<DtUniqueID> ProjectileIDList;
protected:
DtDe& myDe;
ProjectileMap myProjectiles;
ProjectileIDList myExpiredProjectiles;
std::string myModelDefinitionName;
double myProjectileSpeed;
DtProjectile::IntersectionCallback myIntersectionCallback;
};
}
Contains the DtProjectile class definition.
Contains makVrv::DtUniqueID type.
DtProjectile.cxx
#include <osg/Matrixd>
#include <boost/bind/bind.hpp>
{
const DtVector& direction, const std::string& modelDefinitionName, IntersectionCallback callback )
: myDe( de )
, myFiringPosition( start )
, myFiringStartTime( -1. )
, myIntersectionCallback( callback )
{
DtUniqueID elementId = myDe.agentManager().createNewUniqueId();
myFacade = new DtEntity3dFacade( myDe.agentManager(),
DtSharedSettingsManager::instance( myDe ), elementId, DtModelSetId::Models3D, true );
myFacade->setUpdateTypes( false, false );
myFacade->setArticulatedModelDefinition( modelDefinitionName );
myFacade->setPosition( start );
myFacade->setOrientation( orientation );
myFacade->setVisible( true );
myCoordinateSystem = myDe.sharedState().coordinateSystem().clone();
if (myCoordinateSystem == nullptr)
{
DtTHROW_NEW(DtCorruptedState, "myDe.sharedState().coordinateSystem().clone() failed.");
}
myConnections += DtDeSharedStateSignaler::instance(myDe).signal_coordinateSystemChanged.connect(
boost::bind(&DtProjectile::slot_coordinateSystemChanged, this));
slot_coordinateSystemChanged();
DtVecScale( direction, speed, myVelocity0 );
myCoordinateSystem->converter()->setupTopoFrame( start );
DtVecScale( myCoordinateSystem->converter()->up( start ), -9.81, myAcceleration );
myIntersector = DtIntersectorCreator::instance(myDe).create(myDe, DtModelSetId::Models3D);
}
DtProjectile::~DtProjectile()
{
delete myCoordinateSystem;
delete myIntersector;
delete myFacade;
myFacade = 0;
}
void DtProjectile::slot_coordinateSystemChanged()
{
myCoordinateSystem->set(myDe.sharedState().coordinateSystem().type(), myDe.sharedState().coordinateSystem().parameters());
}
bool DtProjectile::tick( double simTime )
{
if ( myFiringStartTime < 0. )
{
myFiringStartTime = simTime;
return true;
}
double total_time = simTime - myFiringStartTime;
DtVector newPos;
DtVector vComponent;
DtVecScale( myVelocity0, total_time, vComponent );
DtVector aComponent;
double oneHalf_tSquared = ( total_time * total_time) / 2. ;
DtVecScale( myAcceleration, oneHalf_tSquared, aComponent );
newPos = myFiringPosition + vComponent + aComponent;
DtIntersectorResult isectResult;
std::vector<DtElementID> ignoreList;
ignoreList.push_back( id() );
DtVector pos = myFacade->position();
bool hit = myIntersector->findFirstIntersectWithLineSegment( pos.x(), pos.y(), pos.z(),
newPos.x(), newPos.y(), newPos.z(), isectResult, DtIntersector::PickableNodes, &ignoreList );
if ( hit )
{
newPos = DtVector( isectResult.x, isectResult.y, isectResult.z );
if ( myIntersectionCallback )
{
DtVecScale( myAcceleration, total_time, aComponent );
DtVector velocity = myVelocity0 + aComponent;
myIntersectionCallback( isectResult, sqrt( velocity.magnitudeSquared() ) );
}
myFacade->setOrientation( orientationFromOldAndNewPositions( myFacade->position(), newPos ) );
myFacade->setPosition( newPos );
return false;
}
myFacade->setOrientation( orientationFromOldAndNewPositions( myFacade->position(), newPos ) );
myFacade->setPosition( newPos );
return true;
}
DtUniqueID DtProjectile::id() const
{
if ( myFacade )
{
return myFacade->elementId();
}
return (DtUniqueID)0;
}
DtTaitBryan DtProjectile::orientationFromOldAndNewPositions(const DtVector& oldPos, const DtVector& newPos )
{
DtVector geoStart, geoEnd;
myCoordinateSystem->localToNetPos( oldPos, geoStart );
myCoordinateSystem->localToNetPos( newPos, geoEnd );
DtQuat qOri =
DtOrientToVector( *(myCoordinateSystem->converter()), geoStart, geoEnd );
double yaw, pitch, roll;
return DtTaitBryan( yaw, pitch, roll );
}
}
Contains makVrv::DtDeSharedStateSignaler class.
Contains makVrv::DtEntity3dFacade class.
Contains various enum classes relating to model sets.
Contains makVrv::DtSharedSettingsManager class.
DtProjectile(DtDe &de, const DtVector &start, const DtTaitBryan &orientation, double speed, const DtVector &direction, const std::string &modelDefinitionName, IntersectionCallback callback=(IntersectionCallback) 0)
CTOR.
Contains additional coordinate system related utilities.
DT_DLL_VRVUTIL DtQuat DtOrientToVector(DtCoordinateConverter &converter, const DtVector &start, const DtVector &end)
This form defines the vector as end - start.
void DT_DLL_VRVUTIL DtQuaternionToEulerRad(const double x, const double y, const double z, const double w, double &yawOut, double &pitchOut, double &rollOut)
Converts Quaternions to Eulers. Eulers are in radians.
DtProjectile.cxx
#include <osg/Matrixd>
#include <boost/bind/bind.hpp>
{
const DtVector& direction, const std::string& modelDefinitionName, IntersectionCallback callback )
: myDe( de )
, myFiringPosition( start )
, myFiringStartTime( -1. )
, myIntersectionCallback( callback )
{
DtUniqueID elementId = myDe.agentManager().createNewUniqueId();
myFacade = new DtEntity3dFacade( myDe.agentManager(),
DtSharedSettingsManager::instance( myDe ), elementId, DtModelSetId::Models3D, true );
myFacade->setUpdateTypes( false, false );
myFacade->setArticulatedModelDefinition( modelDefinitionName );
myFacade->setPosition( start );
myFacade->setOrientation( orientation );
myFacade->setVisible( true );
myCoordinateSystem = myDe.sharedState().coordinateSystem().clone();
if (myCoordinateSystem == nullptr)
{
DtTHROW_NEW(DtCorruptedState, "myDe.sharedState().coordinateSystem().clone() failed.");
}
myConnections += DtDeSharedStateSignaler::instance(myDe).signal_coordinateSystemChanged.connect(
boost::bind(&DtProjectile::slot_coordinateSystemChanged, this));
slot_coordinateSystemChanged();
DtVecScale( direction, speed, myVelocity0 );
myCoordinateSystem->converter()->setupTopoFrame( start );
DtVecScale( myCoordinateSystem->converter()->up( start ), -9.81, myAcceleration );
myIntersector = DtIntersectorCreator::instance(myDe).create(myDe, DtModelSetId::Models3D);
}
DtProjectile::~DtProjectile()
{
delete myCoordinateSystem;
delete myIntersector;
delete myFacade;
myFacade = 0;
}
void DtProjectile::slot_coordinateSystemChanged()
{
myCoordinateSystem->set(myDe.sharedState().coordinateSystem().type(), myDe.sharedState().coordinateSystem().parameters());
}
bool DtProjectile::tick( double simTime )
{
if ( myFiringStartTime < 0. )
{
myFiringStartTime = simTime;
return true;
}
double total_time = simTime - myFiringStartTime;
DtVector newPos;
DtVector vComponent;
DtVecScale( myVelocity0, total_time, vComponent );
DtVector aComponent;
double oneHalf_tSquared = ( total_time * total_time) / 2. ;
DtVecScale( myAcceleration, oneHalf_tSquared, aComponent );
newPos = myFiringPosition + vComponent + aComponent;
DtIntersectorResult isectResult;
std::vector<DtElementID> ignoreList;
ignoreList.push_back( id() );
DtVector pos = myFacade->position();
bool hit = myIntersector->findFirstIntersectWithLineSegment( pos.x(), pos.y(), pos.z(),
newPos.x(), newPos.y(), newPos.z(), isectResult, DtIntersector::PickableNodes, &ignoreList );
if ( hit )
{
newPos = DtVector( isectResult.x, isectResult.y, isectResult.z );
if ( myIntersectionCallback )
{
DtVecScale( myAcceleration, total_time, aComponent );
DtVector velocity = myVelocity0 + aComponent;
myIntersectionCallback( isectResult, sqrt( velocity.magnitudeSquared() ) );
}
myFacade->setOrientation( orientationFromOldAndNewPositions( myFacade->position(), newPos ) );
myFacade->setPosition( newPos );
return false;
}
myFacade->setOrientation( orientationFromOldAndNewPositions( myFacade->position(), newPos ) );
myFacade->setPosition( newPos );
return true;
}
DtUniqueID DtProjectile::id() const
{
if ( myFacade )
{
return myFacade->elementId();
}
return (DtUniqueID)0;
}
DtTaitBryan DtProjectile::orientationFromOldAndNewPositions(const DtVector& oldPos, const DtVector& newPos )
{
DtVector geoStart, geoEnd;
myCoordinateSystem->localToNetPos( oldPos, geoStart );
myCoordinateSystem->localToNetPos( newPos, geoEnd );
DtQuat qOri =
DtOrientToVector( *(myCoordinateSystem->converter()), geoStart, geoEnd );
double yaw, pitch, roll;
return DtTaitBryan( yaw, pitch, roll );
}
}
exampleProjectile.cxx
static makVrv::vrvSignalsLib::connection postInitializeConnection;
{
}
{
}
{
{
}
}
{
return true;
}
#define DT_DE_INIT_ONCE(de)
This macro is called by modules in their init() function. This macro ensures that the module is only ...
Definition DtDe.h:30
Contains makVrv::DtDeMainWindow class.
Contains makArchives::DtObserverStateRecord class.
Contains makVrv::DtQtDeMainWindow class.
Contains makVrv::DtStealthCommandLineProcessor class.
Contains makVrv::DtStealthConfiguration class.
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
DtAgentManager & agentManager()
Get the scene manager.
virtual bool isInMasterMode() const
Returns true if the DtDe is in master mode.
virtual DtDriverManager & driverManager()
The DtMasterScene accessor. The function will throw before the initialize function is called.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
The initialization configuration for the makVrv::DtDe.
Definition DtDeInitializer.h:40
The abstract main window class.
Definition DtDeMainWindow.h:25
DtDeMainWindow & igMainWindow()
Use this accessor to lazily create the singleton instance of the DtDeMainWindow;.
static DtDeMainWindowProvider & instance(DtDe &de)
Get the main window provider instance for a DtDe.
void addDriver(DtDriver *driver)
Add a host driver to the master scene. The host is now owned by the Master Scene.
void startDriver(DtDriver *driver)
Start a driver driver modifying the scene.
Definition DtExampleProjectile.h:30
The Qt main window. There will be only one of these per DtDe instance. Note, this window will be dest...
Definition DtQtDeMainWindow.h:46
virtual void hideStartupDialog()
Hide startup dialog.
This owns a list of display engine configurations.
Definition DtVrvApplicationConfiguration.h:33
The Root IG Application object. To run the IG simply create one, initialize it and call run.
Definition DtVrvApplication.h:58
static DtVrvApplication * findFromDe(DtDe &)
virtual void installPostPluginTransform(const ApplicationTransform &transform, const std::string &id)
Add a transformer that runs after plug-ins are loaded.
boost::function< void(DtDeInitializer &, DtDe &, DtVrvApplicationConfiguration &)> ApplicationTransform
Definition DtVrvApplication.h:147
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
DT_DLL_EXAMPLEPROJECTILE void installDriver(makVrv::DtDe &de)
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)
DT_DLL_VRVCOREQT void init(DtDe &de)
Contains DLL export macros.