Advanced Ballistics - Performance improvement (#4708)

* Up to 16% faster frame times with 200 simultaneously flying projectiles
This commit is contained in:
ulteq 2016-11-25 19:25:03 +01:00 committed by GitHub
parent ed0fd5f6f6
commit 8f16702e64
2 changed files with 14 additions and 33 deletions

Binary file not shown.

View File

@ -14,6 +14,7 @@
#define ABSOLUTE_ZERO_IN_CELSIUS -273.15f
#define KELVIN(t) (t - ABSOLUTE_ZERO_IN_CELSIUS)
#define CELSIUS(t) (t + ABSOLUTE_ZERO_IN_CELSIUS)
#define EARTH_ANGULAR_SPEED 0.00007292f
#define UNIVERSAL_GAS_CONSTANT 8.314f
#define WATER_VAPOR_MOLAR_MASS 0.018016f
#define DRY_AIR_MOLAR_MASS 0.028964f
@ -43,11 +44,7 @@ struct Bullet {
double humidity;
double overcast;
double startTime;
double speed;
double frames;
double lastFrame;
double hDeflection;
double spinDrift;
double bcDegradation;
unsigned randSeed;
std::default_random_engine randGenerator;
@ -480,11 +477,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].overcast = overcast;
bulletDatabase[index].startTime = tickTime;
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;
strncpy_s(output, outputSize, "", _TRUNCATE);
@ -548,8 +541,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double TOF = 0.0;
double deltaT = 0.0;
double bulletSpeed;
double bulletDir;
double bulletSpeedAvg = 0.0;
double trueVelocity[3] = { 0.0, 0.0, 0.0 };
double trueSpeed = 0.0;
double temperature = 0.0;
@ -557,7 +548,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double windSpeed = 0.0;
double windAttenuation = 1.0;
double velocityOffset[3] = { 0.0, 0.0, 0.0 };
double positionOffset[3] = { 0.0, 0.0, 0.0 };
TOF = tickTime - bulletDatabase[index].startTime;
@ -565,11 +555,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].lastFrame = tickTime;
bulletSpeed = sqrt(pow(velocity[0], 2) + pow(velocity[1], 2) + pow(velocity[2], 2));
bulletDir = atan2(velocity[0], velocity[1]);
bulletDatabase[index].speed += bulletSpeed;
bulletDatabase[index].frames += 1;
bulletSpeedAvg = (bulletDatabase[index].speed / bulletDatabase[index].frames);
windSpeed = sqrt(pow(wind[0], 2) + pow(wind[1], 2) + pow(wind[2], 2));
if (windSpeed > 0.1) {
@ -694,27 +679,23 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
}
}
if (bulletSpeedAvg > 0) {
double distanceSqr = pow(bulletDatabase[index].origin[0] - position[0], 2) + pow(bulletDatabase[index].origin[1] - position[1], 2) + pow(bulletDatabase[index].origin[2] - position[2], 2);
double horizontalDeflection = 0.0000729 * distanceSqr * sin(bulletDatabase[index].latitude) / bulletSpeedAvg;
double horizontalDeflectionPartial = horizontalDeflection - bulletDatabase[index].hDeflection;
bulletDatabase[index].hDeflection = horizontalDeflection;
positionOffset[0] += sin(bulletDir + M_PI / 2) * horizontalDeflectionPartial;
positionOffset[1] += cos(bulletDir + M_PI / 2) * horizontalDeflectionPartial;
if (TOF > 0) {
double bulletDir = atan2(velocity[0], velocity[1]);
double spinAccel = bulletDatabase[index].twistDirection * (0.0482251 * (bulletDatabase[index].stabilityFactor + 1.2)) / pow(TOF, 0.17);
velocityOffset[0] += sin(bulletDir + M_PI / 2) * spinAccel * deltaT;
velocityOffset[1] += cos(bulletDir + M_PI / 2) * spinAccel * deltaT;
}
double centripetalAccel = 2 * 0.0000729 * (bulletDatabase[index].muzzleVelocity) * cos(bulletDatabase[index].latitude) * sin(bulletDir);
velocityOffset[2] += centripetalAccel * deltaT;
double lat = bulletDatabase[index].latitude;
accel[0] = 2 * EARTH_ANGULAR_SPEED * +(velocity[1] * sin(lat) - velocity[2] * cos(lat));
accel[1] = 2 * EARTH_ANGULAR_SPEED * -(velocity[0] * sin(lat));
accel[2] = 2 * EARTH_ANGULAR_SPEED * +(velocity[0] * cos(lat));
double spinDrift = bulletDatabase[index].twistDirection * 0.0254 * 1.25 * (bulletDatabase[index].stabilityFactor + 1.2) * pow(TOF, 1.83);
double spinDriftPartial = spinDrift - bulletDatabase[index].spinDrift;
bulletDatabase[index].spinDrift = spinDrift;
velocityOffset[0] += accel[0] * deltaT;
velocityOffset[1] += accel[1] * deltaT;
velocityOffset[2] += accel[2] * deltaT;
positionOffset[0] += sin(bulletDir + M_PI / 2) * spinDriftPartial;
positionOffset[1] += cos(bulletDir + M_PI / 2) * spinDriftPartial;
outputStr << "_bullet setVelocity (_bulletVelocity vectorAdd [" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]); _bullet setPosASL (_bulletPosition vectorAdd [" << positionOffset[0] << "," << positionOffset[1] << "," << positionOffset[2] << "]);";
outputStr << "_bullet setVelocity (_bulletVelocity vectorAdd [" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]);";
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
EXTENSION_RETURN();
} else if (!strcmp(mode, "set")) {