Advertisement
Faguss

dopolygonsintersect.sqf

Jun 4th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. /*
  2. [_v, _player_size, _building, _size] call DO_POLYGONS_INTERSECT"
  3.  
  4. 0 - player
  5. 1 - player size
  6.   0 - player w
  7.   1 - player h
  8.   2 - player offset x
  9.   3 - player offset y
  10. 2 - building
  11. 3 - building size
  12.   0 - building w
  13.   1 - building h
  14.   2 - building offset x
  15.   3 - building offset y
  16. */
  17.  
  18. private ["_pos1", "_d1", "_sind1", "_cosd1", "_x1", "_y1", "_w1", "_h1", "_pos2", "_d2", "_sind2", "_cosd2", "_x2", "_y2", "_w2", "_h2","_polygons", "_count", "_i", "_terminate", "_polygon", "_i1", "_count2", "_i2", "_p1", "_p2", "_normal", "_minA", "_maxA", "_j", "_count3", "_projected", "_minB", "_maxB"];
  19.  
  20. _pos1  = getPos (_this select 0);
  21. _d1    = getDir (_this select 0);
  22. _sind1 = sin _d1;
  23. _cosd1 = cos _d1;
  24. _x1    = (_pos1 select 0) + ((_this select 1) select 3)*_sind1 + ((_this select 1) select 2)*_cosd1;
  25. _y1    = (_pos1 select 1) + ((_this select 1) select 3)*_cosd1 - ((_this select 1) select 2)*_sind1;
  26. _w1    = (_this select 1) select 0;
  27. _h1    = (_this select 1) select 1;
  28.  
  29. _pos2  = getPos (_this select 2);
  30. _d2    = getDir (_this select 2);
  31. _sind2 = sin _d2;
  32. _cosd2 = cos _d2;
  33. _x2    = (_pos2 select 0) + ((_this select 3) select 3)*_sind2 + ((_this select 3) select 2)*_cosd2;
  34. _y2    = (_pos2 select 1) + ((_this select 3) select 3)*_cosd2 - ((_this select 3) select 2)*_sind2;
  35. _w2    = (_this select 3) select 0;
  36. _h2    = (_this select 3) select 1;
  37.  
  38. _polygons =
  39. [
  40.     [
  41.         [_x1 + -_h1*_sind1 + -_w1*_cosd1, _y1 + -_h1*_cosd1 - -_w1*_sind1],
  42.         [_x1 + -_h1*_sind1 +  _w1*_cosd1, _y1 + -_h1*_cosd1 -  _w1*_sind1],
  43.         [_x1 +  _h1*_sind1 +  _w1*_cosd1, _y1 +  _h1*_cosd1 -  _w1*_sind1],
  44.         [_x1 +  _h1*_sind1 + -_w1*_cosd1, _y1 +  _h1*_cosd1 - -_w1*_sind1]
  45.     ]
  46.     ,
  47.     [
  48.         [_x2 + -_h2*_sind2 + -_w2*_cosd2, _y2 + -_h2*_cosd2 - -_w2*_sind2],
  49.         [_x2 + -_h2*_sind2 +  _w2*_cosd2, _y2 + -_h2*_cosd2 -  _w2*_sind2],
  50.         [_x2 +  _h2*_sind2 +  _w2*_cosd2, _y2 +  _h2*_cosd2 -  _w2*_sind2],
  51.         [_x2 +  _h2*_sind2 + -_w2*_cosd2, _y2 +  _h2*_cosd2 - -_w2*_sind2]
  52.     ]
  53. ];
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. //https://stackoverflow.com/questions/10962379/how-to-check-intersection-between-2-rotated-rectangles
  61.  
  62. //private ["_polygons", "_count", "_i", "_terminate", "_polygon", "_i1", "_count2", "_i2", "_p1", "_p2", "_normal", "_minA", "_maxA", "_j", "_count3", "_projected", "_minB", "_maxB"];
  63. _i         = -1;
  64. _count     = count _polygons;
  65. _terminate = false;
  66.    
  67. while "_i=_i+1; _i < _count && !_terminate" do
  68. {
  69.     _polygon = _polygons select _i;
  70.     _i1      = -1;
  71.     _count2  = count _polygon;
  72.    
  73.     while "_i1=_i1+1; _i1 < _count2 && !_terminate" do
  74.     {
  75.         _i2     = (_i1 + 1) % _count2;
  76.         _p1     = _polygon select _i1;
  77.         _p2     = _polygon select _i2;
  78.         _normal = [(_p2 select 1) - (_p1 select 1), (_p1 select 0) - (_p2 select 0)];
  79.         _minA   = nil;
  80.         _maxA   = nil;
  81.         _j      = -1;
  82.         _count3 = count (_polygons select 0);
  83.        
  84.         while "_j=_j+1; _j < _count3" do
  85.         {
  86.             _projected = (_normal select 0) * (((_polygons select 0) select _j) select 0) + (_normal select 1) * (((_polygons select 0) select _j) select 1);
  87.            
  88.             if (Format ["%1",_minA]=="scalar bool array string 0xfcffffef") then {
  89.                 _minA = _projected
  90.             } else {
  91.                 if (_projected < _minA) then {_minA = _projected}
  92.             };
  93.            
  94.             if (Format ["%1",_maxA]=="scalar bool array string 0xfcffffef") then {
  95.                 _maxA = _projected
  96.             } else {
  97.                 if (_projected > _maxA) then {_maxA = _projected}
  98.             }
  99.         };
  100.            
  101.         _j      = -1;
  102.         _minB   = nil;
  103.         _maxB   = nil;
  104.         _count3 = count (_polygons select 1);
  105.        
  106.         while "_j=_j+1; _j < _count3" do
  107.         {
  108.             _projected = (_normal select 0) * (((_polygons select 1) select _j) select 0) + (_normal select 1) * (((_polygons select 1) select _j) select 1);
  109.            
  110.             if (Format ["%1",_minB]=="scalar bool array string 0xfcffffef") then {
  111.                 _minB = _projected
  112.             } else {
  113.                 if (_projected < _minB) then {_minB = _projected}
  114.             };
  115.            
  116.             if (Format ["%1",_maxB]=="scalar bool array string 0xfcffffef") then {
  117.                 _maxB = _projected
  118.             } else {
  119.                 if (_projected > _maxB) then {_maxB = _projected}
  120.             }
  121.         };
  122.  
  123.         if (_maxA < _minB || _maxB < _minA) then {_terminate=true}
  124.     }
  125. };
  126.    
  127. if (_terminate) then {false} else {true}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement