Advertisement
Badwrong

GameMaker - Collision Line Thick

Oct 11th, 2022 (edited)
2,874
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     A small library for collision lines with "thickness".  Static sensor objects
  3.     are used and stretched to then call built-in collision functions.
  4.  
  5.     The object __collision_line_thick must be created and have a sprite with the following settings:
  6.         width          = 1
  7.         height         = 2
  8.         origin         = middle left
  9.         collision mask = full image, rectangle with rotation
  10.                
  11.     The object should also have persistent set to TRUE so that room changes will not cause the
  12.     instance to be destroyed.
  13. */
  14.  
  15. /// @function                   collision_line_thick(_x1, _y1, _x2, _y2, _thickness, _object)
  16. /// @param {real} _x1           The x coordinate of the start of the line
  17. /// @param {real} _y1           The y coordinate of the start of the line
  18. /// @param {real} _x2           The x coordinate of the end of the line
  19. /// @param {real} _y2           The y coordinate of the end of the line
  20. /// @param {real} _thickness    The thickness of the line to check for collisions
  21. /// @param {index} _object      The object to check for instance collisions
  22. /// @description                A thick collision line that returns the the first instance collided with
  23.  
  24. function collision_line_thick(_x1, _y1, _x2, _y2, _thickness, _object)
  25. {
  26.     static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick);
  27.    
  28.     with (_sensor)
  29.     {
  30.         x = _x1;
  31.         y = _y1;
  32.         image_xscale = point_distance(_x1, _y1, _x2, _y2);
  33.         image_yscale = _thickness * 0.5;
  34.         image_angle  = point_direction(_x1, _y1, _x2, _y2);
  35.    
  36.         return instance_place(x, y, _object);
  37.     }
  38. }
  39.  
  40.  
  41. /// @function                   collision_line_thick_list(_x1, _y1, _x2, _y2, _thickness, _object, _list, _ordered)
  42. /// @param {real} _x1           The x coordinate of the start of the line
  43. /// @param {real} _y1           The y coordinate of the start of the line
  44. /// @param {real} _x2           The x coordinate of the end of the line
  45. /// @param {real} _y2           The y coordinate of the end of the line
  46. /// @param {real} _thickness    The thickness of the line to check for collisions
  47. /// @param {index} _object      The object to check for instance collisions
  48. /// @param {index} _list        The DS list to use to store the IDs of the colliding instances
  49. /// @param {boolean} _ordered   Whether the list should be ordered by distance (true) or not (false)
  50. /// @description                A thick collision line that returns the number of instances collided with
  51.  
  52. function collision_line_thick_list(_x1, _y1, _x2, _y2, _thickness, _object, _list, _ordered)
  53. {
  54.     static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick);
  55.    
  56.     with (_sensor)
  57.     {
  58.         x = _x1;
  59.         y = _y1;
  60.         image_xscale = point_distance(_x1, _y1, _x2, _y2);
  61.         image_yscale = _thickness * 0.5;
  62.         image_angle  = point_direction(_x1, _y1, _x2, _y2);
  63.  
  64.         return instance_place_list(x, y, _object, _list, _ordered);
  65.     }
  66. }
  67.  
  68.  
  69. /// @function                   collision_line_thick_impact(_x1, _y1, _x2, _y2, _thickness, _object)
  70. /// @param {real} _x1           The x coordinate of the start of the line
  71. /// @param {real} _y1           The y coordinate of the start of the line
  72. /// @param {real} _x2           The x coordinate of the end of the line
  73. /// @param {real} _y2           The y coordinate of the end of the line
  74. /// @param {real} _thickness    The thickness of the line to check for collisions
  75. /// @param {index} _object      The object to check for instance collisions
  76. /// @description                A thick collision line that returns a struct with instance ID and impact x/y position
  77.  
  78. function collision_line_thick_impact(_x1, _y1, _x2, _y2, _thickness, _object)
  79. {
  80.     static _sensor = instance_create_depth(0, 0, -16000, __collision_line_thick);
  81.    
  82.     with (_sensor)
  83.     {
  84.         x = _x1;
  85.         y = _y1;
  86.         image_xscale = point_distance(_x1, _y1, _x2, _y2);
  87.         image_yscale = _thickness * 0.5;
  88.         image_angle  = point_direction(_x1, _y1, _x2, _y2);
  89.    
  90.         var _inst = instance_place(x, y, _object),
  91.             _rx = _x1,
  92.             _ry = _y1;
  93.                
  94.         if (_inst != noone) {
  95.                
  96.             var _count = ceil(log2(image_xscale)) + 1,
  97.                 _p0 = 0,
  98.                 _p1 = 1;
  99.            
  100.             _x2 -= _x1;
  101.             _y2 -= _y1;
  102.        
  103.             repeat (_count) {
  104.            
  105.                 var _np = _p0 + (_p1 - _p0) * 0.5,
  106.                     _nx = _x1 + _x2 * _np,
  107.                     _ny = _y1 + _y2 * _np;
  108.  
  109.                     image_xscale = point_distance(x, y, _nx, _ny);
  110.                     var _inst2   = instance_place(x, y, _object);
  111.                
  112.                 if (_inst2 != noone) {
  113.                     _inst = _inst2;
  114.                     _rx = _nx;
  115.                     _ry = _ny;
  116.                     _p1 = _np;
  117.                 } else _p0 = _np;
  118.             }
  119.         }
  120.    
  121.         return { id : _inst, x : _rx, y : _ry };
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement