ACE3/addons/fcs/functions/fnc_getAngle.sqf

86 lines
2.6 KiB
Plaintext
Raw Normal View History

#include "..\script_component.hpp"
/*
* Author: KoffeinFlummi
* Calculates the angle offset necessary to hit the current target.
*
* Arguments:
2015-12-10 15:00:14 +00:00
* 0: distance to target in meters <NUMBER>
* 1: current angle of the turret <NUMBER>
* 2: maximum elevation of the turret <NUMBER>
* 3: initSpeed of the projectile <NUMBER>
* 4: airFriction of the projectile <NUMBER>
* 5: maximum timeToLive of the projectile <NUMBER>
* 6: simulationStep of the projectile <NUMBER>
*
* Return Value:
2015-12-10 15:00:14 +00:00
* offset from the current angle necessary to hit the target <NUMBER>
*
* Example:
* [1, 2, 3, 4, 5, 6, 7] call ace_fcs_fnc_getAngle
*
2015-12-10 15:00:14 +00:00
* Public: No
*/
2015-12-10 15:00:14 +00:00
#define PRECISION 0.1
2015-12-10 15:00:14 +00:00
params ["_distance", "_angleTarget", "_maxElev", "_initSpeed", "_airFriction", "_timeToLive", "_simulationStep"];
if (_simulationStep == 0) exitWith {_angleTarget};
2015-12-10 15:00:14 +00:00
private _fnc_traceBullet = {
params ["_distance", "_angleTarget", "_maxElev", "_initSpeed", "_airFriction", "_timeToLive", "_simulationStep", "_angle"];
2015-01-14 20:07:41 +00:00
_angle = _angle - _angleTarget;
_angleTarget = 0;
2015-12-10 15:00:14 +00:00
private _posTargetX = (cos _angleTarget) * _distance;
private _posTargetY = (sin _angleTarget) * _distance;
2015-01-14 20:07:41 +00:00
2015-12-10 15:00:14 +00:00
private _posX = 0;
private _posY = 0;
2015-01-14 20:07:41 +00:00
2015-12-10 15:00:14 +00:00
private _velocityX = (cos _angle) * _initSpeed;
private _velocityY = (sin _angle) * _initSpeed;
2015-01-14 20:07:41 +00:00
// trace the path of the bullet
for "_i" from 1 to ((floor (_timeToLive / _simulationStep)) + 1) do {
2015-12-10 15:00:14 +00:00
private _velocityMagnitude = sqrt (_velocityX^2 + _velocityY^2);
2015-01-14 20:07:41 +00:00
_velocityX = _velocityX + _simulationStep * (_velocityX * _velocityMagnitude * _airFriction);
_velocityY = _velocityY + _simulationStep * (_velocityY * _velocityMagnitude * _airFriction - 9.81);
2015-12-10 15:00:14 +00:00
2015-01-14 20:07:41 +00:00
_posX = _posX + _velocityX * _simulationStep;
_posY = _posY + _velocityY * _simulationStep;
2015-12-10 15:00:14 +00:00
2015-01-14 20:07:41 +00:00
if (_posX >= _posTargetX) exitWith {}; // bullet passed the target
};
_posY - _posTargetY
};
2015-12-10 15:00:14 +00:00
private _data = [_distance, _angleTarget, _maxElev, _initSpeed, _airFriction, _timeToLive, _simulationStep, _maxElev];
if (_data call _fnc_traceBullet < 0) exitWith {_maxElev - _angleTarget};
// Newton Method / Secand Method
2015-12-10 15:00:14 +00:00
private _angle1 = _angleTarget;
private _angle2 = _maxElev;
private _it2 = 0;
_data set [7, _maxElev];
private _f1 = _data call _fnc_traceBullet;
if (abs _f1 <= PRECISION) exitWith {0};
while {abs _f1 > PRECISION} do {
_data set [7, _angle2];
private _f2 = _data call _fnc_traceBullet;
2015-01-14 20:07:41 +00:00
_angle1 = _angle2;
2015-12-10 15:00:14 +00:00
_angle2 = _angle2 - _f2 * (_angle2 - _angle1) / (_f2 - _f1);
2015-01-14 20:07:41 +00:00
_f1 = _f2;
2015-12-10 15:00:14 +00:00
_it2 = _it2 + 1;
};
2015-12-10 15:00:14 +00:00
_angle2 - _angleTarget