Advertisement
Badwrong

GameMaker - Collison Line Point

Mar 5th, 2025
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 0.90 KB | Source Code | 0 0
  1. // Uses a binary sweep to return the collision point of a line
  2. function collision_line_point(_x1, _y1, _x2, _y2, _obj, _prec, _notme)
  3. {
  4.     var _inst = collision_line(_x1, _y1, _x2, _y2, _obj, _prec, _notme),
  5.         _rx = _x2, 
  6.         _ry = _y2;
  7.        
  8.     if (_inst != noone)
  9.     {
  10.         var _t0 = 0,
  11.             _t1 = 1;
  12.            
  13.         repeat (ceil(log2(point_distance(_x1, _y1, _x2, _y2))) + 1)
  14.         {
  15.             var _t = _t0 + (_t1 - _t0) * 0.5,
  16.                 _bx = _x1 + (_x2 - _x1) * _t,
  17.                 _by = _y1 + (_y2 - _y1) * _t,
  18.                 _ax = _x1 + (_x2 - _x1) * _t0,
  19.                 _ay = _y1 + (_y2 - _y1) * _t0,
  20.                 _inst2 = collision_line(_ax, _ay, _bx, _by, _obj, _prec, _notme);
  21.                
  22.             if (_inst2 != noone)
  23.             {
  24.                 _inst = _inst2;
  25.                 _rx = _bx;
  26.                 _ry = _by;
  27.                 _t1 = _t;
  28.             }
  29.             else
  30.             {
  31.                 _t0 = _t;
  32.             }
  33.         }
  34.     }
  35.     // If there is no hit then _inst will be noone
  36.     return [_inst, _rx, _ry];
  37. }
Tags: gameMaker GMS2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement