Binary Seach for attach pos

This commit is contained in:
PabstMirror 2015-03-26 17:49:11 -05:00
parent 6a71a04ad9
commit 9ae4f39d05

View File

@ -15,8 +15,7 @@
*/
#include "script_component.hpp"
private ["_setupObject", "_setupClassname", "_itemClassname", "_placementText", "_attachToVehicle", "_placer", "_startingPosition", "_startingOffset", "_distanceFromCenter", "_closeInUnitVector", "_keepGoingCloser", "_closeInDistance", "_endPosTestOffset", "_endPosTest", "_startingPosShifted", "_startASL", "_endPosShifted", "_endASL", "_attachedObject", "_currentObjects", "_currentItemNames"];
private ["_setupObject", "_setupClassname", "_itemClassname", "_placementText", "_attachToVehicle", "_placer", "_startingPosition", "_startingOffset", "_startDistanceFromCenter", "_closeInUnitVector", "_keepGoingCloser", "_closeInMax", "_closeInMin", "_closeInDistance", "_endPosTestOffset", "_endPosTest", "_doesIntersect", "_startingPosShifted", "_startASL", "_endPosShifted", "_endASL", "_attachedObject", "_currentObjects", "_currentItemNames"];
if (GVAR(pfeh_running)) then {
[QGVAR(PlacementEachFrame),"OnEachFrame"] call BIS_fnc_removeStackedEventHandler;
@ -50,20 +49,23 @@ call EFUNC(interaction,hideMouseHint);
_startingPosition = _setupObject modelToWorld [0,0,0];
_startingOffset = _attachToVehicle worldToModel _startingPosition;
_distanceFromCenter = vectorMagnitude _startingOffset;
_startDistanceFromCenter = vectorMagnitude _startingOffset;
_closeInUnitVector = vectorNormalized (_startingOffset vectorFromTo [0,0,0]);
_keepGoingCloser = true;
_closeInDistance = 0;
while {_keepGoingCloser} do {
if (_closeInDistance >= _distanceFromCenter) exitWith {};
_closeInMax = _startDistanceFromCenter;
_closeInMin = 0;
_closeInDistance = _closeInDistance + 0.01; //10mm each step
while {(_closeInMax - _closeInMin) > 0.01} do {
_closeInDistance = (_closeInMax + _closeInMin) / 2;
// systemChat format ["Trying %1 from %2 start %3", _closeInDistance, [_closeInMax, _closeInMin], _startDistanceFromCenter];
_endPosTestOffset = _startingOffset vectorAdd (_closeInUnitVector vectorMultiply _closeInDistance);
_endPosTestOffset set [2, (_startingOffset select 2)];
_endPosTest = _attachToVehicle modelToWorld _endPosTestOffset;
_doesIntersect = false;
{
if (_doesIntersect) exitWith {};
_startingPosShifted = _startingPosition vectorAdd _x;
_startASL = if (surfaceIsWater _startingPosShifted) then {_startingPosShifted} else {ATLtoASL _startingPosShifted};
{
@ -73,21 +75,29 @@ while {_keepGoingCloser} do {
//Uncomment to see the lazor show, and see how the scanning works:
drawLine3D [_startingPosShifted, _endPosShifted, [1,0,0,1]];
if (_attachToVehicle in lineIntersectsWith [_startASL, _endASL, _placer, _setupObject]) exitWith {_keepGoingCloser = false};
if (_attachToVehicle in lineIntersectsWith [_startASL, _endASL, _placer, _setupObject]) exitWith {_doesIntersect = true};
} forEach [[0,0,0.045], [0,0,-0.045], [0,0.045,0], [0,-0.045,0], [0.045,0,0], [-0.045,0,0]];
} forEach [[0,0,0], [0,0,0.05], [0,0,-0.05]];
if (_doesIntersect) then {
_closeInMax = _closeInDistance;
} else {
_closeInMin = _closeInDistance;
};
};
_closeInDistance = (_closeInMax + _closeInMin) / 2;
//Delete Local Placement Object
deleteVehicle _setupObject;
//Checks
if ((_closeInDistance >= _distanceFromCenter) || (!([_placer,_attachToVehicle,_itemClassname] call FUNC(canAttach)))) exitWith {
TRACE_2("no valid spot found",_closeInDistance,_distanceFromCenter);
if (((_startDistanceFromCenter - _closeInDistance) < 0.1) || {!([_placer,_attachToVehicle,_itemClassname] call FUNC(canAttach))}) exitWith {
TRACE_2("no valid spot found",_closeInDistance,_startDistanceFromCenter);
[localize "STR_ACE_Attach_Failed"] call EFUNC(common,displayTextStructured);
};
//Move it out slightly, for visability sake (better to look a little funny than be embedded//sunk in the hull)
//Move it out slightly, for visability sake (better to look a little funny than be embedded//sunk in the hull and be useless)
_closeInDistance = (_closeInDistance - 0.0085);
//Create New 'real' Object