This example demonstrates the VR-Vantage ballistics system in isolation. It creates a plug-in driver, DtExampleBallisticsDriver, that fires bursts of GPU tracer rounds in response to key presses, removes them again, and previews the predicted trajectory of each shot. The example shows no platform or vehicle: the only things drawn are the GPU tracers and the trajectory-preview arcs.
When you press one of the fire keys, a burst of tracer rounds is fired from a fixed muzzle position and travels along a ballistic trajectory under the influence of gravity and wind. Each fire key produces a differently coloured tracer burst. As a tracer passes near the observer, the "Bullet Flyby" proximity sound plays.
When you press Shift together with a fire key, nothing is fired. Instead, the driver draws the predicted trajectory of that same shot as three coloured arcs: green for wind plus gravity, yellow for gravity only, and red for no forces. Comparing the arcs shows how much wind and gravity bend the path. The arcs remain visible for 10 seconds, and if you change the wind while they are displayed the green wind-and-gravity arc re-bends immediately to match.
The example is built around the DtBallisticManager singleton, which owns the GPU ballistics simulation. The driver installs a DtEventProcessorInterface to watch for key presses. On a fire key it fills in a DtBallisticManager::BallisticData record and calls DtBallisticManager::createProjectiles(). On a Shift+key it runs the CPU-side DtTrajectoryPredictor instead and renders the result with DtDebugShapes.
Both the fire path and the preview path read the same per-key parameters from a single shot table, so the previewed arc always matches the burst that would be fired. Each entry describes a burst: the muzzle position and velocity, maximum range, the number of rounds, the rate of fire, how often a round is a visible tracer (the tracer interval), and the tracer appearance (DtTracerDefinition). The proximity sound group is always "Bullet Flyby".
This example is a plug-in. You can run it by running vrvStealth.exe –plugin ../examples/plugins/release/exampleBallistics.dll (on Windows) or vrvStealth.exe –plugin ../examples/plugins/release/libexampleBallistics.so (on Linux) from the command prompt. Once running, press 'B', 'N', or 'M' to fire a tracer burst, hold Shift with 'B', 'N', or 'M' to preview a trajectory, and press 'R' to remove everything. For more information about running examples, please see Running Applications and Examples.
#include <algorithm>
#include <vector>
using namespace makVrv;
#define SHOW_GRAVITY_ONLY
#define SHOW_NO_FORCES
struct DtBallisticShotDefinition
{
DtVector myPosition;
DtVector myVelocity;
double myMaxDistance;
int myCount;
double myRateOfFire;
int myTracerInterval;
std::string myArcLabel;
};
namespace
{
DtVector DtScaledMuzzleVelocity(const DtVector& rawVelocity)
{
const double velocityScale = 0.25;
DtVector scaled;
return scaled;
}
const std::vector<DtBallisticShotDefinition>& DtBallisticShotDefinitions()
{
static const std::vector<DtBallisticShotDefinition> theDefinitions = []()
{
std::vector<DtBallisticShotDefinition> definitions;
#ifdef USE_GEOCENTRIC_TERRAIN
definitions.push_back({
DtVector(-5506817.128206, -2240932.121720, 2301773.052875),
DtScaledMuzzleVelocity(DtVector(-365.649048, 761.991699, -115.231880)),
258.1192626953, 15, 750.0 / 60.0, 5,
definitions.push_back({
DtVector(-5506976.325637, -2240559.782043, 2301862.716279),
DtScaledMuzzleVelocity(DtVector(218.335922, -614.219727, -550.156860)),
198.2325134277, 3, 550.0 / 60.0, 1,
definitions.push_back({
DtVector(-5506878.107655, -2240862.760257, 2301961.459429),
DtScaledMuzzleVelocity(DtVector(-132.207169, 511.155518, -669.962891)),
272.3383483887, 3, 550.0 / 60.0, 2,
#else
definitions.push_back({
DtVector(-521.924, -845.68, 61.8551),
DtScaledMuzzleVelocity(DtVector(789.674, 302.878, -110.854)),
318.4730529785, 15, 750.0 / 60.0, 5,
definitions.push_back({
DtVector(-164.228, -268.048, 36.5075),
DtScaledMuzzleVelocity(DtVector(-128.745, -938.665, -32.1992)),
470.5762329102, 3, 550.0 / 60.0, 1,
definitions.push_back({
DtVector(-153.321, -270.162, 36.5229),
DtScaledMuzzleVelocity(DtVector(-135.452, -841.676, -29.0304)),
469.6385498047, 3, 550.0 / 60.0, 2,
#endif
return definitions;
}();
return theDefinitions;
}
}
{
public:
DtBallisticFireEventProcessor(bool& fireRequestedB, bool& fireRequestedN, bool& fireRequestedM,
bool& previewRequestedB, bool& previewRequestedN, bool& previewRequestedM,
bool& removeRequested)
: myFireRequestedB(fireRequestedB)
, myFireRequestedN(fireRequestedN)
, myFireRequestedM(fireRequestedM)
, myPreviewRequestedB(previewRequestedB)
, myPreviewRequestedN(previewRequestedN)
, myPreviewRequestedM(previewRequestedM)
, myRemoveRequested(removeRequested)
, myClassName("DtBallisticFireEventProcessor")
{
}
virtual ~DtBallisticFireEventProcessor() {}
{
{
return false;
}
{
{
myFireRequestedB = true;
return true;
}
{
myFireRequestedN = true;
return true;
}
{
myFireRequestedM = true;
return true;
}
{
myRemoveRequested = true;
return true;
}
}
{
{
myPreviewRequestedB = true;
return true;
}
{
myPreviewRequestedN = true;
return true;
}
{
myPreviewRequestedM = true;
return true;
}
}
return false;
}
virtual const std::string& className() { return myClassName; }
protected:
bool& myFireRequestedB;
bool& myFireRequestedN;
bool& myFireRequestedM;
bool& myPreviewRequestedB;
bool& myPreviewRequestedN;
bool& myPreviewRequestedM;
bool& myRemoveRequested;
std::string myClassName;
};
: makVrv::
DtDriver( am,
"DtExampleBallisticsDriver" )
, myCoordinateSystem( nullptr )
, myWindSpeed( 0.0f )
, myWindDirection( 0.0f )
, myFireRequestedB( false )
, myFireRequestedN( false )
, myFireRequestedM( false )
, myPreviewRequestedB( false )
, myPreviewRequestedN( false )
, myPreviewRequestedM( false )
, myRemoveRequested( false )
, myEventProcessor( nullptr )
{
if (myCoordinateSystem == nullptr)
{
DtTHROW_NEW(DtCorruptedState, "myDe.sharedState().coordinateSystem().clone() failed.");
}
myConnections += DtDeSharedStateSignaler::instance( am.
de() ).signal_coordinateSystemChanged.connect(
slot_coordinateSystemChanged();
}
{
}
{
static std::string name = "DtExampleBallisticsDriver";
return name;
}
{
#ifdef USE_GEOCENTRIC_TERRAIN
groundDbPath += "/TerrainData/TerrainConfiguration/Ala Moana.mtf";
#else
groundDbPath += "/TerrainData/TerrainConfiguration/Ground_DB II.mtf";
#endif
this, boost::placeholders::_1)));
this, boost::placeholders::_1)));
return true;
}
{
{
}
return true;
}
{
const std::vector<DtBallisticShotDefinition>& definitions = DtBallisticShotDefinitions();
{
if (definitions.size() > 0)
{
}
}
{
if (definitions.size() > 1)
{
}
}
{
if (definitions.size() > 2)
{
}
}
{
if (definitions.size() > 0)
{
}
}
{
if (definitions.size() > 1)
{
}
}
{
if (definitions.size() > 2)
{
}
}
{
{
{
}
myFiredIdentifiers.clear();
}
}
return true;
}
{
}
{
}
DtVector position, DtVector& east, DtVector& north, DtVector& up)
{
{
return;
}
if (!converter)
{
return;
}
}
{
}
{
double muzzleVelocity = std::sqrt(def.myVelocity.magnitudeSquared());
if (muzzleVelocity <= 0.0)
{
return;
}
double lifeTime = (def.myMaxDistance / muzzleVelocity) * 2.0;
double timeStep = lifeTime / 100.0;
DtVector east(1.0, 0.0, 0.0);
DtVector north(0.0, 1.0, 0.0);
DtVector up(0.0, 0.0, 1.0);
dvec3(east.x(), east.y(), east.z()),
dvec3(north.x(), north.y(), north.z()),
dvec3(up.x(), up.y(), up.z()));
dvec3 origin =
dvec3(def.myPosition.x(), def.myPosition.y(), def.myPosition.z());
dvec3 velocity =
dvec3(def.myVelocity.x(), def.myVelocity.y(), def.myVelocity.z());
velocity.normalize();
const float previewAliveTime = 10.0f;
auto drawArc = [&](const std::string& label, double red, double green, double blue,
{
std::vector<DtTrajectoryPoint> trajectory =
trajectoryPredictor.
predictTrajectory(origin, velocity, bulletDef, environment, lifeTime, timeStep);
if (trajectory.empty())
{
return;
}
if (!multiLine)
{
return;
}
DtVector pointA(0.0, 0.0, 0.0);
DtVector pointB(0.0, 0.0, 0.0);
int index = -1;
{
pointB = pointA;
pointA = DtVector(point.position().x, point.position().y, point.position().z);
if (index == -1)
{
index = 0;
continue;
}
multiLine->
setLine(pointA, pointB, index);
++index;
}
};
{
0.0);
dvec3 windVelocityInFrame =
frame.right() * windVelocityEnu.
x +
frame.forward() * windVelocityEnu.y +
frame.up() * windVelocityEnu.z;
drawArc(def.myArcLabel + " Wind+Gravity", 0.0, 1.0, 0.0, environment);
}
#ifdef SHOW_GRAVITY_ONLY
{
drawArc(def.myArcLabel + " Gravity", 1.0, 1.0, 0.0, environment);
}
#endif
#ifdef SHOW_NO_FORCES
{
drawArc(def.myArcLabel + " No Forces", 1.0, 0.0, 0.0, environment);
}
#endif
{
}
}
{
ballisticData.
myCount = def.myCount;
}
{
}
{
for (const DtBallisticShotDefinition* def : active)
{
}
}
{
for (const DtBallisticShotDefinition& def : DtBallisticShotDefinitions())
{
}
}