Advertisement
Badwrong

GMS2 Edge and ball collision

Jan 25th, 2022 (edited)
1,150
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Edge and ball collision event
  2.  
  3. with other
  4. {
  5.     var _x = (bbox_left + bbox_right) * 0.5, _y = (bbox_top  + bbox_bottom) * 0.5,
  6.         _w = sprite_width * 0.5, _h = sprite_height * 0.5,
  7.         _cos = dcos(image_angle), _sin = -dsin(image_angle),
  8.         _wcos = _w * _cos, _wsin = _w * _sin,
  9.         _hcos = _h * _cos, _hsin = _h * _sin,      
  10.         _x1 = _x - _wcos + _hsin,
  11.         _y1 = _y - _wsin - _hcos,
  12.         _x2 = _x + _wcos + _hsin,
  13.         _y2 = _y + _wsin - _hcos;
  14. }
  15.  
  16. var _vx = _x2 - _x1,
  17.     _vy = _y2 - _y1,
  18.     _dot = _vx*_vx + _vy*_vy,
  19.     _proj_x = x - _x1,
  20.     _proj_y = y - _y1,
  21.     _t  = max(0, min(_dot, _vx * _proj_x + _vy * _proj_y)) / _dot,
  22.     _collision_x = _x1 + _t * _vx,
  23.     _collision_y = _y1 + _t * _vy,
  24.     _vx = x - _collision_x,
  25.     _vy = y - _collision_y,
  26.     _dist = sqrt(_vx * _vx + _vy * _vy),
  27.     _radius = sprite_width * 0.5;
  28.    
  29. if (_dist < _radius)
  30. {
  31.     _vx /= _dist;
  32.     _vy /= _dist;
  33.     x = _collision_x + _vx * _radius;
  34.     y = _collision_y + _vy * _radius;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement