Skip to content

Commit

Permalink
Greatly improves the plausibility of the transonic flight simulation (#…
Browse files Browse the repository at this point in the history
…4652)

* Transonic flight now induces yawing and pitching of the nose of the bullet instead of modifying the bullet velocity directly
* The ballistic coefficient now starts to decreases once the bullet goes transonic
  • Loading branch information
ulteq authored Nov 22, 2016
1 parent dcd4137 commit e70a23d
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions extensions/advanced_ballistics/AdvancedBallistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct Bullet {
double lastFrame;
double hDeflection;
double spinDrift;
double bcDegradation;
unsigned randSeed;
std::default_random_engine randGenerator;
};
Expand All @@ -68,7 +69,6 @@ Map* map = &mapDatabase[""];
double calculateRoughnessLength(double posX, double posY) {
// Source: http://es.ucsc.edu/~jnoble/wind/extrap/index.html
double roughness_lengths[10] = {0.0002, 0.0005, 0.0024, 0.03, 0.055, 0.1, 0.2, 0.4, 0.8, 1.6};
double roughnessLength = 0.0024;

int gridX = (int)floor(posX / 50);
int gridY = (int)floor(posY / 50);
Expand Down Expand Up @@ -482,6 +482,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].lastFrame = tickTime;
bulletDatabase[index].hDeflection = 0.0;
bulletDatabase[index].spinDrift = 0.0;
bulletDatabase[index].bcDegradation = 1.0;
bulletDatabase[index].speed = 0.0;
bulletDatabase[index].frames = 0.0;
bulletDatabase[index].randSeed = 0;
Expand Down Expand Up @@ -617,6 +618,24 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
trueVelocity[2] = velocity[2] - wind[2];
trueSpeed = sqrt(pow(trueVelocity[0], 2) + pow(trueVelocity[1], 2) + pow(trueVelocity[2], 2));

double speedOfSound = 331.3 + (0.6 * temperature);
double transonicSpeed = 394 + (0.6 * temperature);
if (bulletDatabase[index].transonicStabilityCoef < 1.0f && bulletSpeed < transonicSpeed && bulletSpeed > speedOfSound) {
std::uniform_real_distribution<double> distribution(-10.0, 10.0);
double coef = 1.0f - bulletDatabase[index].transonicStabilityCoef;

trueVelocity[0] += distribution(bulletDatabase[index].randGenerator) * coef;
trueVelocity[1] += distribution(bulletDatabase[index].randGenerator) * coef;
trueVelocity[2] += distribution(bulletDatabase[index].randGenerator) * coef;
double speed = sqrt(pow(trueVelocity[0], 2) + pow(trueVelocity[1], 2) + pow(trueVelocity[2], 2));

trueVelocity[0] *= trueSpeed / speed;
trueVelocity[1] *= trueSpeed / speed;
trueVelocity[2] *= trueSpeed / speed;

bulletDatabase[index].bcDegradation *= pow(0.993, coef);
};

temperature = bulletDatabase[index].temperature - 0.0065 * position[2];
pressure = (1013.25 - 10 * bulletDatabase[index].overcast) * pow(1 - (0.0065 * (bulletDatabase[index].altitude + position[2])) / (273.15 + temperature + 0.0065 * bulletDatabase[index].altitude), 5.255754495);

Expand All @@ -640,6 +659,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
}

ballisticCoefficient = calculateAtmosphericCorrection(ballisticCoefficient, temperature, pressure, bulletDatabase[index].humidity, bulletDatabase[index].atmosphereModel);
ballisticCoefficient *= bulletDatabase[index].bcDegradation;
drag = deltaT * calculateRetard(bulletDatabase[index].dragModel, ballisticCoefficient, trueSpeed);
accel[0] = (trueVelocity[0] / trueSpeed) * drag;
accel[1] = (trueVelocity[1] / trueSpeed) * drag;
Expand Down Expand Up @@ -694,16 +714,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
positionOffset[0] += sin(bulletDir + M_PI / 2) * spinDriftPartial;
positionOffset[1] += cos(bulletDir + M_PI / 2) * spinDriftPartial;

double speedOfSound = 331.3 + (0.6 * temperature);
if (bulletSpeed < (speedOfSound + 5) && bulletSpeedAvg > speedOfSound && bulletSpeed > (speedOfSound - 5)) {
std::uniform_real_distribution<double> distribution(0.0, 1.0);
double coef = 1.0f - bulletDatabase[index].transonicStabilityCoef;

velocityOffset[0] += (distribution(bulletDatabase[index].randGenerator) * 0.8 - 0.4) * coef;
velocityOffset[1] += (distribution(bulletDatabase[index].randGenerator) * 0.8 - 0.4) * coef;
velocityOffset[2] += (distribution(bulletDatabase[index].randGenerator) * 0.8 - 0.4) * coef;
};

outputStr << "_bullet setVelocity (_bulletVelocity vectorAdd [" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]); _bullet setPosASL (_bulletPosition vectorAdd [" << positionOffset[0] << "," << positionOffset[1] << "," << positionOffset[2] << "]);";
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
Expand All @@ -725,7 +735,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
} else if (!strcmp(mode, "init")) {
int mapSize = 0;
int mapGrids = 0;
int gridCells = 0;
unsigned int gridCells = 0;

worldName = strtok_s(NULL, ":", &next_token);
mapSize = strtol(strtok_s(NULL, ":", &next_token), NULL, 10);
Expand Down

0 comments on commit e70a23d

Please sign in to comment.