Skip to content

Commit

Permalink
Advanced Ballistics - Fixes bug in the vanilla drag removal routine
Browse files Browse the repository at this point in the history
* More accurate bullet drop at extreme long ranges
* Fixes Client FPS affecting the trajectory
  • Loading branch information
ulteq committed Oct 30, 2017
1 parent 32e3b64 commit 4bfc956
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
Binary file modified ace_advanced_ballistics.dll
Binary file not shown.
Binary file modified ace_advanced_ballistics_x64.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion addons/advanced_ballistics/functions/fnc_handleFired.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ if (_caliber > 0 && _bulletLength > 0 && _bulletMass > 0 && _barrelTwist > 0) th

GVAR(currentbulletID) = (GVAR(currentbulletID) + 1) % 10000;

"ace_advanced_ballistics" callExtension format["new:%1:%2:%3:%4:%5:%6:%7:%8:%9:%10:%11:%12:%13:%14:%15:%16:%17", GVAR(currentbulletID), _airFriction, _ballisticCoefficients, _velocityBoundaries, _atmosphereModel, _dragModel, _stabilityFactor, _twistDirection, _muzzleVelocity, _transonicStabilityCoef, getPosASL _projectile, EGVAR(common,mapLatitude), EGVAR(weather,currentTemperature), EGVAR(common,mapAltitude), EGVAR(weather,currentHumidity), EGVAR(weather,currentOvercast), CBA_missionTime toFixed 6];
"ace_advanced_ballistics" callExtension format["new:%1:%2:%3:%4:%5:%6:%7:%8:%9:%10:%11:%12:%13:%14:%15:%16:%17", GVAR(currentbulletID), _airFriction, _ballisticCoefficients, _velocityBoundaries, _atmosphereModel, _dragModel, _stabilityFactor, _twistDirection, _transonicStabilityCoef, getPosASL _projectile, _bulletVelocity, EGVAR(common,mapLatitude), EGVAR(weather,currentTemperature), EGVAR(common,mapAltitude), EGVAR(weather,currentHumidity), EGVAR(weather,currentOvercast), CBA_missionTime toFixed 6];

GVAR(allBullets) pushBack [_projectile, _caliber, _bulletTraceVisible, GVAR(currentbulletID)];

Expand Down
57 changes: 32 additions & 25 deletions extensions/advanced_ballistics/AdvancedBallistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ struct Bullet {
double stabilityFactor;
double twistDirection;
double transonicStabilityCoef;
double muzzleVelocity;
double bulletSpeed;
std::vector<double> bulletVelocity;
std::vector<double> origin;
double latitude;
double temperature;
Expand Down Expand Up @@ -335,7 +334,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double stabilityFactor = 1.5;
int twistDirection = 1;
double transonicStabilityCoef = 1;
double muzzleVelocity = 850;
char* bulletVelocityArray;
char* bulletVelocityEntry;
std::vector<double> bulletVelocity;
char* originArray;
char* originEntry;
std::vector<double> origin;
Expand Down Expand Up @@ -368,7 +369,6 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
dragModel = strtol(strtok_s(NULL, ":", &next_token), NULL, 10);
stabilityFactor = strtod(strtok_s(NULL, ":", &next_token), NULL);
twistDirection = strtol(strtok_s(NULL, ":", &next_token), NULL, 10);
muzzleVelocity = strtod(strtok_s(NULL, ":", &next_token), NULL);
transonicStabilityCoef = strtod(strtok_s(NULL, ":", &next_token), NULL);
originArray = strtok_s(NULL, ":", &next_token);
originArray++;
Expand All @@ -378,6 +378,14 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
origin.push_back(strtod(originEntry, NULL));
originEntry = strtok_s(NULL, ",", &token);
}
bulletVelocityArray = strtok_s(NULL, ":", &next_token);
bulletVelocityArray++;
bulletVelocityArray[strlen(bulletVelocityArray) - 1] = 0;
bulletVelocityEntry = strtok_s(bulletVelocityArray, ",", &token);
while (bulletVelocityEntry != NULL) {
bulletVelocity.push_back(strtod(bulletVelocityEntry, NULL));
bulletVelocityEntry = strtok_s(NULL, ",", &token);
}
latitude = strtod(strtok_s(NULL, ":", &next_token), NULL);
temperature = strtod(strtok_s(NULL, ":", &next_token), NULL);
altitude = strtod(strtok_s(NULL, ":", &next_token), NULL);
Expand All @@ -398,8 +406,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
bulletDatabase[index].stabilityFactor = stabilityFactor;
bulletDatabase[index].twistDirection = twistDirection;
bulletDatabase[index].transonicStabilityCoef = transonicStabilityCoef;
bulletDatabase[index].muzzleVelocity = muzzleVelocity;
bulletDatabase[index].bulletSpeed = muzzleVelocity;
bulletDatabase[index].bulletVelocity = bulletVelocity;
bulletDatabase[index].origin = origin;
bulletDatabase[index].latitude = latitude / 180 * M_PI;
bulletDatabase[index].temperature = temperature;
Expand Down Expand Up @@ -477,6 +484,7 @@ 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 bulletSpeed = sqrt(pow(bulletDatabase[index].bulletVelocity[0], 2) + pow(bulletDatabase[index].bulletVelocity[1], 2) + pow(bulletDatabase[index].bulletVelocity[2], 2));

bulletDatabase[index].lastFrame = tickTime;

Expand Down Expand Up @@ -522,9 +530,9 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
windSpeed = sqrt(pow(wind[0], 2) + pow(wind[1], 2) + pow(wind[2], 2));
}

trueVelocity[0] = velocity[0] - wind[0];
trueVelocity[1] = velocity[1] - wind[1];
trueVelocity[2] = velocity[2] - wind[2];
trueVelocity[0] = bulletDatabase[index].bulletVelocity[0] - wind[0];
trueVelocity[1] = bulletDatabase[index].bulletVelocity[1] - wind[1];
trueVelocity[2] = bulletDatabase[index].bulletVelocity[2] - wind[2];
trueSpeed = sqrt(pow(trueVelocity[0], 2) + pow(trueVelocity[1], 2) + pow(trueVelocity[2], 2));

if (bulletDatabase[index].transonicStabilityCoef < 1.0f && trueSpeed - 60 < SPEED_OF_SOUND(temperature) && trueSpeed > SPEED_OF_SOUND(temperature)) {
Expand All @@ -544,11 +552,11 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
};

if (bulletDatabase[index].ballisticCoefficients.size() == bulletDatabase[index].velocityBoundaries.size() + 1) {
dragRef = deltaT * bulletDatabase[index].airFriction * bulletDatabase[index].bulletSpeed * bulletDatabase[index].bulletSpeed;
dragRef = deltaT * bulletDatabase[index].airFriction * bulletSpeed * bulletSpeed;

accelRef[0] = (velocity[0] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[1] = (velocity[1] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[2] = (velocity[2] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[0] = (bulletDatabase[index].bulletVelocity[0] / bulletSpeed) * dragRef;
accelRef[1] = (bulletDatabase[index].bulletVelocity[1] / bulletSpeed) * dragRef;
accelRef[2] = (bulletDatabase[index].bulletVelocity[2] / bulletSpeed) * dragRef;

velocityOffset[0] -= accelRef[0];
velocityOffset[1] -= accelRef[1];
Expand Down Expand Up @@ -577,11 +585,11 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
double airFriction = bulletDatabase[index].airFriction * airDensity / STD_AIR_DENSITY_ICAO;

if (airFriction != bulletDatabase[index].airFriction || windSpeed > 0) {
dragRef = deltaT * bulletDatabase[index].airFriction * bulletDatabase[index].bulletSpeed * bulletDatabase[index].bulletSpeed;
dragRef = deltaT * bulletDatabase[index].airFriction * bulletSpeed * bulletSpeed;

accelRef[0] = (velocity[0] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[1] = (velocity[1] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[2] = (velocity[2] / bulletDatabase[index].bulletSpeed) * dragRef;
accelRef[0] = (bulletDatabase[index].bulletVelocity[0] / bulletSpeed) * dragRef;
accelRef[1] = (bulletDatabase[index].bulletVelocity[1] / bulletSpeed) * dragRef;
accelRef[2] = (bulletDatabase[index].bulletVelocity[2] / bulletSpeed) * dragRef;

velocityOffset[0] -= accelRef[0];
velocityOffset[1] -= accelRef[1];
Expand All @@ -599,7 +607,7 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
}

if (TOF > 0) {
double bulletDir = atan2(velocity[0], velocity[1]);
double bulletDir = atan2(bulletDatabase[index].bulletVelocity[0], bulletDatabase[index].bulletVelocity[1]);
double driftAccel = bulletDatabase[index].twistDirection * (0.0482251 * (bulletDatabase[index].stabilityFactor + 1.2)) / pow(TOF, 0.17);
double driftVelocity = 0.0581025 *(bulletDatabase[index].stabilityFactor + 1.2) * pow(TOF, 0.83);
double dragCorrection = (driftVelocity / trueSpeed) * drag;
Expand All @@ -608,18 +616,17 @@ void __stdcall RVExtension(char *output, int outputSize, const char *function)
}

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));
accel[0] = 2 * EARTH_ANGULAR_SPEED * +(bulletDatabase[index].bulletVelocity[1] * sin(lat) - bulletDatabase[index].bulletVelocity[2] * cos(lat));
accel[1] = 2 * EARTH_ANGULAR_SPEED * -(bulletDatabase[index].bulletVelocity[0] * sin(lat));
accel[2] = 2 * EARTH_ANGULAR_SPEED * +(bulletDatabase[index].bulletVelocity[0] * cos(lat));

velocityOffset[0] += accel[0] * deltaT;
velocityOffset[1] += accel[1] * deltaT;
velocityOffset[2] += accel[2] * deltaT;

velocity[0] += velocityOffset[0];
velocity[1] += velocityOffset[1];
velocity[2] += velocityOffset[2];
bulletDatabase[index].bulletSpeed = sqrt(pow(velocity[0], 2) + pow(velocity[1], 2) + pow(velocity[2], 2));
bulletDatabase[index].bulletVelocity[0] = velocity[0] + velocityOffset[0];
bulletDatabase[index].bulletVelocity[1] = velocity[1] + velocityOffset[1];
bulletDatabase[index].bulletVelocity[2] = velocity[2] + velocityOffset[2];

outputStr << "[" << velocityOffset[0] << "," << velocityOffset[1] << "," << velocityOffset[2] << "]";
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE);
Expand Down

0 comments on commit 4bfc956

Please sign in to comment.