Advertisement
RefresherTowel

GameMaker - Collision Line Point

Jul 18th, 2023
1,558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***** cloned from badwrong https://pastebin.com/6YM6ykBC ********/
  2.  
  3. /// @function                   collision_line_point(_x1, _y1, _x2, _y2, _object, _precision, _notme)
  4. /// @param {real} _x1           The x coordinate of the start of the line
  5. /// @param {real} _y1           The y coordinate of the start of the line
  6. /// @param {real} _x2           The x coordinate of the end of the line
  7. /// @param {real} _y2           The y coordinate of the end of the line
  8. /// @param {index} _object      The object to check for instance collisions
  9. /// @param {bool} _precision    Whether or not to use precise collision checking (slower)
  10. /// @param {bool} _notme        Whether or not to check for collision with the calling instance
  11. /// @description                A collision line function which returns an array with the instance and impact point of collision
  12.  
  13. function collision_line_point(_x1, _y1, _x2, _y2, _object, _precision, _notme)
  14. {
  15.     var _inst = collision_line(_x1, _y1, _x2, _y2, _object, _precision, _notme),
  16.         _rx = _x1,
  17.         _ry = _y1;
  18.            
  19.     if (_inst != noone) {
  20.        
  21.         var _count = ceil(log2(point_distance(_x1, _y1, _x2, _y2))) + 1,
  22.             _p0 = 0,
  23.             _p1 = 1;
  24.            
  25.         _x2 -= _x1;
  26.         _y2 -= _y1;
  27.        
  28.         repeat (_count) {
  29.            
  30.             var _np = _p0 + (_p1 - _p0) * 0.5,
  31.                 _nx = _x1 + _x2 * _np,
  32.                 _ny = _y1 + _y2 * _np,
  33.                 _px = _x1 + _x2 * _p0,
  34.                 _py = _y1 + _y2 * _p0,
  35.                 _inst2 = collision_line(_px, _py, _nx, _ny, _object, _precision, _notme);
  36.                
  37.             if (_inst2 != noone) {
  38.                 _inst = _inst2;
  39.                 _rx = _nx;
  40.                 _ry = _ny;
  41.                 _p1 = _np;
  42.             } else _p0 = _np;
  43.            
  44.         }
  45.     }
  46.    
  47.     // Returns instance id (or noone), and the impact x/y
  48.     return [_inst, _rx, _ry];
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement