Advertisement
Badwrong

GameMaker - Collision Line Point

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