Advertisement
MRDANEEYUL

GML - 3D collision

Jul 11th, 2020 (edited)
1,763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function collision_point_3d(_x, _y, _z, _object, _isPrecise, _shouldSkipSelf) {    
  3.     var _collisionList = get_collision_ds_list();
  4.     var _collisionCount = collision_point_list(_x, _y, _object, _isPrecise, _shouldSkipSelf, _collisionList, false);
  5.     for (var i = _collisionCount - 1; i >= 0; i--)
  6.     {
  7.         var _instance = _collisionList[| i];
  8.         if (has_z_collision(_instance, _z))
  9.             return _instance.id;
  10.     }
  11.     return null;
  12. }
  13.  
  14. function instance_place_3d(_x, _y, _z, _object) {      
  15.     var _collisionList = get_collision_ds_list();
  16.     var _collisionCount = instance_place_list(_x, _y, _object, _collisionList, false);
  17.     for (var i = _collisionCount - 1; i >= 0; i--)
  18.     {
  19.         var _instance = _collisionList[| i];
  20.         if (has_z_collision(_instance, _z))
  21.             return _instance.id;
  22.     }
  23.     return null;
  24. }
  25.  
  26. function place_meeting_3d(_x, _y, _z, _object) {   
  27.     return instance_place_3d(_x, _y, _z, _object) != null;
  28. }
  29.  
  30. function has_z_collision(_collider, _z) {
  31.     if (!instance_exists(_collider)) return false;
  32.    
  33.     var _colliderBottom = _collider.z;
  34.     var _colliderTop = _colliderBottom + _collider.height;
  35.    
  36.     return (_z >= _colliderBottom && _z < _colliderTop)
  37.         || (_z + height >= _colliderBottom && _z + height < _colliderTop);
  38. }
  39.  
  40. function get_collision_ds_list() {
  41.     if (!variable_instance_exists(id, "collisionList") || !ds_exists(collisionList, ds_type_list))
  42.         collisionList = ds_list_create();
  43.        
  44.     if (!ds_list_empty(collisionList))
  45.         ds_list_clear(collisionList);
  46.        
  47.     return collisionList;
  48. }
  49.  
  50. function tile_and_place_meeting_3d(_x, _y, _z, _object, _tileLayer) {
  51.     return tile_meeting_precise(_x, _y - ground_z_get(), _tileLayer) || instance_place_3d(_x, _y, _z, _object) != null;
  52. }
  53.  
  54. function tile_meeting_ground(_x, _y, _z, _tileLayer) {
  55.     var _tile = tile_get_at_point(_tileLayer, _x, _y - _z - (variable_instance_exists(id, "state") && state == STATE_DIVE ? 16 : 0));
  56.     return _tile != null && !tile_get_empty(_tile) && z <= ground_z_get();
  57. }
  58.  
  59. function has_water_collision() {
  60.        
  61.     var _centerX = center_x_bbox(id);
  62.     var _centerY = center_y_bbox(id);
  63.     return ((tile_meeting_ground(_centerX, _centerY, z, "Water")
  64.         || global.currentTime == timeType.day && tile_meeting_ground(_centerX, _centerY, z, "Water_Day")
  65.         || collision_point_3d(_centerX, _centerY, z + height, water_obj, false, true)
  66.         ) && !place_meeting_3d(x, y, z, bridge_obj));
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement