2015-01-16 23:09:19 +00:00
|
|
|
/*
|
|
|
|
* ace_fcs.cpp
|
|
|
|
*
|
|
|
|
* Calculates firing solution.
|
|
|
|
*
|
|
|
|
* Takes:
|
|
|
|
* initSpeed,airFriction,angle,distance as string
|
|
|
|
* Example: "900,-0.0004,2,1050"
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* Correction to angle
|
|
|
|
*/
|
|
|
|
|
2015-05-13 15:34:14 +00:00
|
|
|
#include "shared.hpp"
|
2015-04-12 03:42:58 +00:00
|
|
|
|
2015-01-16 23:09:19 +00:00
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
2016-02-17 22:19:23 +00:00
|
|
|
#include <stdlib.h>
|
2015-01-16 23:09:19 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#define MAXELEVATION 20
|
2015-04-08 19:09:11 +00:00
|
|
|
#define MAXITERATIONS 600
|
2015-01-16 23:09:19 +00:00
|
|
|
#define PRECISION 0.1
|
|
|
|
#define RADIANS(X) (X / (180 / M_PI))
|
|
|
|
|
|
|
|
extern "C" {
|
2015-05-17 17:10:02 +00:00
|
|
|
EXPORT void __stdcall RVExtension(char *output, int outputSize, const char *function);
|
2015-09-08 19:53:29 +00:00
|
|
|
}
|
2015-01-16 23:09:19 +00:00
|
|
|
|
|
|
|
std::vector<std::string> splitString(std::string input) {
|
|
|
|
std::istringstream ss(input);
|
|
|
|
std::string token;
|
|
|
|
|
|
|
|
std::vector<std::string> output;
|
|
|
|
while (std::getline(ss, token, ',')) {
|
|
|
|
output.push_back(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
double traceBullet(double initSpeed, double airFriction, double angle, double angleTarget, double distance) {
|
2015-02-17 09:24:03 +00:00
|
|
|
double velX, velY, posX, posY, lastPosX, lastPosY, posTargetX, posTargetY, velMag;
|
2015-01-16 23:09:19 +00:00
|
|
|
velX = cos(RADIANS(angle)) * initSpeed;
|
|
|
|
velY = sin(RADIANS(angle)) * initSpeed;
|
|
|
|
posX = 0;
|
|
|
|
posY = 0;
|
|
|
|
posTargetX = cos(RADIANS(angleTarget)) * distance;
|
|
|
|
posTargetY = sin(RADIANS(angleTarget)) * distance;
|
|
|
|
|
2015-02-17 09:24:03 +00:00
|
|
|
double simulationStep = 0.05;
|
|
|
|
|
2015-01-16 23:09:19 +00:00
|
|
|
int i = 0;
|
|
|
|
while (i < MAXITERATIONS) {
|
2015-02-17 09:24:03 +00:00
|
|
|
lastPosX = posX;
|
|
|
|
lastPosY = posY;
|
2015-01-16 23:09:19 +00:00
|
|
|
velMag = sqrt(pow(velX, 2) + pow(velY, 2));
|
2015-04-06 21:43:32 +00:00
|
|
|
posX += velX * simulationStep * 0.5;
|
|
|
|
posY += velY * simulationStep * 0.5;
|
2015-02-17 09:24:03 +00:00
|
|
|
velX += simulationStep * (velX * velMag * airFriction);
|
2017-10-28 08:34:54 +00:00
|
|
|
velY += simulationStep * (velY * velMag * airFriction - 9.8066);
|
2015-04-06 21:43:32 +00:00
|
|
|
posX += velX * simulationStep * 0.5;
|
|
|
|
posY += velY * simulationStep * 0.5;
|
2015-01-16 23:09:19 +00:00
|
|
|
if (posX >= posTargetX) { break; }
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2015-02-17 09:24:03 +00:00
|
|
|
double coef = (posTargetX - lastPosX) / (posX - lastPosX);
|
|
|
|
return (lastPosY + (posY - lastPosY) * coef) - posTargetY;
|
2015-01-16 23:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double getSolution(double initSpeed, double airFriction, double angleTarget, double distance) {
|
|
|
|
if (traceBullet(initSpeed, airFriction, MAXELEVATION, angleTarget, distance) < 0) {
|
|
|
|
return MAXELEVATION - angleTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
double a1 = angleTarget;
|
|
|
|
double a2 = MAXELEVATION;
|
|
|
|
double f1, f2, tmp;
|
|
|
|
f1 = traceBullet(initSpeed, airFriction, a1, angleTarget, distance);
|
|
|
|
if (fabs(f1) <= PRECISION) { return 0; }
|
|
|
|
while (fabs(f1) > PRECISION) {
|
|
|
|
f2 = traceBullet(initSpeed, airFriction, a2, angleTarget, distance);
|
|
|
|
tmp = a2 - f2 * (a2 - a1) / (f2 - f1);
|
|
|
|
a1 = a2;
|
|
|
|
a2 = tmp;
|
|
|
|
f1 = f2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a2 - angleTarget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
|
2015-05-13 15:34:14 +00:00
|
|
|
ZERO_OUTPUT();
|
2015-01-16 23:09:19 +00:00
|
|
|
if (!strcmp(function, "version")) {
|
2020-04-18 17:41:37 +00:00
|
|
|
strncpy(output, ACE_FULL_VERSION_STR, outputSize - 1);
|
2015-01-16 23:09:19 +00:00
|
|
|
} else {
|
|
|
|
std::vector<std::string> argStrings = splitString(function);
|
|
|
|
double initSpeed = std::stod(argStrings[0]);
|
|
|
|
double airFriction = std::stod(argStrings[1]);
|
|
|
|
double angleTarget = std::stod(argStrings[2]);
|
|
|
|
double distance = std::stod(argStrings[3]);
|
|
|
|
|
|
|
|
double result = getSolution(initSpeed, airFriction, angleTarget, distance);
|
|
|
|
|
2015-04-12 03:42:58 +00:00
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << result;
|
|
|
|
|
2020-04-18 17:41:37 +00:00
|
|
|
strncpy(output, sstream.str().c_str(), outputSize - 1);
|
2015-01-16 23:09:19 +00:00
|
|
|
}
|
2015-05-13 15:34:14 +00:00
|
|
|
EXTENSION_RETURN();
|
2015-01-16 23:09:19 +00:00
|
|
|
}
|