Skip to content

Instantly share code, notes, and snippets.

@tinkerer-red
Last active November 16, 2023 15:15
Show Gist options
  • Save tinkerer-red/863ba0a65a8805785d9135422c395a5d to your computer and use it in GitHub Desktop.
Save tinkerer-red/863ba0a65a8805785d9135422c395a5d to your computer and use it in GitHub Desktop.
GML Collision Array Functions
#region jsDoc
/// @func collision_circle_array()
/// @desc This function is the same as the collision_circle() function, only instead of just detecting one instance / tile map in collision at a time, it will detect multiple instances or tile maps.
/// @param {Real} x1 : The x coordinate of the center of the circle to check.
/// @param {Real} y1 : The y coordinate of the center of the circle to check.
/// @param {Real} rad : The radius (distance in pixels from its center to its edge).
/// @param {Object} obj : Asset or Object Instance or Tile Map Element ID or Array An object, instance, tile map ID, keywords all/other, or array containing these items
/// @param {Boolean} prec : Whether the check is based on precise collisions (true, which is slower) or its bounding box in general (false, faster).
/// @param {Boolean} notme : Whether the calling instance, if relevant, should be excluded (true) or not (false).
/// @param {Array} array : The array to use to store the IDs of colliding instances.
/// @param {Boolean} ordered : Whether the list should be ordered by distance (true) or not (false).
/// @returns {Array}
#endregion
function collision_circle_array(_x1, _y1, _radius, _obj, _prec=false, _notme=true, _arr=undefined, _ordered=false) {
static __list = ds_list_create();
collision_circle_list(_x1, _y1, _radius, _obj, _prec, _notme, __list, _ordered);
_arr = __push_list_into_arr(_arr, __list);
ds_list_clear(__list);
return _arr;
}
#region jsDoc
/// @func collision_ellipse_array()
/// @desc This function is the same as the collision_ellipse() function, only instead of just detecting one instance / tile map in collision at a time, it will detect multiple instances or tile maps.
/// @param {Real} x1 : The x coordinate of the left side of the ellipse to check.
/// @param {Real} y1 : The y coordinate of the top side of the ellipse to check.
/// @param {Real} x2 : The x coordinate of the right side of the ellipse to check.
/// @param {Real} y2 : The y coordinate of the bottom side of the ellipse to check.
/// @param {Object} obj : Asset or Object Instance or Tile Map Element ID or Array An object, instance, tile map ID, keywords all/other, or array containing these items
/// @param {Boolean} prec : Whether the check is based on precise collisions (true, which is slower) or its bounding box in general (false, faster).
/// @param {Boolean} notme : Whether the calling instance, if relevant, should be excluded (true) or not (false).
/// @param {Array} array : The array to use to store the IDs of colliding instances.
/// @param {Boolean} ordered : Whether the list should be ordered by distance (true) or not (false).
/// @returns {Array}
#endregion
function collision_ellipse_array(_x1, _y1, _x2, _y2, _obj, _prec=false, _notme=true, _arr=undefined, _ordered=false) {
static __list = ds_list_create();
collision_ellipse_list(_x1, _y1, _x2, _y2, _obj, _prec, _notme, __list, _ordered);
_arr = __push_list_into_arr(_arr, __list);
ds_list_clear(__list);
return _arr;
}
#region jsDoc
/// @func collision_line_array()
/// @desc This function is the same as the collision_line() function, only instead of just detecting one instance / tile map in collision at a time, it will detect multiple instances / tile maps.
/// @param {Real} x1 : The x coordinate of the start of the line.
/// @param {Real} y1 : The y coordinate of the start of the line.
/// @param {Real} x2 : The x coordinate of the end of the line.
/// @param {Real} y2 : The y coordinate of the end of the line.
/// @param {Object} obj : Asset or Object Instance or Tile Map Element ID or Array An object, instance, tile map ID, keywords all/other, or array containing these items
/// @param {Boolean} prec : Whether the check is based on precise collisions (true, which is slower) or its bounding box in general (false, faster).
/// @param {Boolean} notme : Whether the calling instance, if relevant, should be excluded (true) or not (false).
/// @param {Array} array : The array to use to store the IDs of colliding instances.
/// @param {Boolean} ordered : Whether the list should be ordered by distance (true) or not (false).
/// @returns {Array}
#endregion
function collision_line_array(_x1, _y1, _x2, _y2, _obj, _prec=false, _notme=true, _arr=undefined, _ordered=false) {
static __list = ds_list_create();
collision_line_list(_x1, _y1, _x2, _y2, _obj, _prec, _notme, __list, _ordered);
_arr = __push_list_into_arr(_arr, __list);
ds_list_clear(__list);
return _arr;
}
#region jsDoc
/// @func collision_point_array()
/// @desc This function is the same as the collision_circle() function, only instead of just detecting one instance / tile map in collision at a time, it will detect multiple instances or tile maps.
/// @param {Real} x : The x coordinate of the point to check.
/// @param {Real} y : The y coordinate of the point to check.
/// @param {Object} obj : Asset or Object Instance or Tile Map Element ID or Array An object, instance, tile map ID, keywords all/other, or array containing these items
/// @param {Boolean} prec : Whether the check is based on precise collisions (true, which is slower) or its bounding box in general (false, faster).
/// @param {Boolean} notme : Whether the calling instance, if relevant, should be excluded (true) or not (false).
/// @param {Array} array : The array to use to store the IDs of colliding instances.
/// @param {Boolean} ordered : Whether the list should be ordered by distance (true) or not (false).
/// @returns {Array}
#endregion
function collision_point_array(_x, _y, _obj, _prec=false, _notme=true, _arr=undefined, _ordered=false) {
static __list = ds_list_create();
collision_point_list(_x, _y, _obj, _prec, _notme, __list, _ordered);
_arr = __push_list_into_arr(_arr, __list);
ds_list_clear(__list);
return _arr;
}
#region jsDoc
/// @func collision_rectangle_array()
/// @desc This function is the same as the collision_circle() function, only instead of just detecting one instance / tile map in collision at a time, it will detect multiple instances or tile maps.
/// @param {Real} x1 : The x coordinate of the left side of the rectangle to check.
/// @param {Real} y1 : The y coordinate of the top side of the rectangle to check.
/// @param {Real} x2 : The x coordinate of the right side of the rectangle to check.
/// @param {Real} y2 : The y coordinate of the bottom side of the rectangle to check.
/// @param {Object} obj : Asset or Object Instance or Tile Map Element ID or Array An object, instance, tile map ID, keywords all/other, or array containing these items
/// @param {Boolean} prec : Whether the check is based on precise collisions (true, which is slower) or its bounding box in general (false, faster).
/// @param {Boolean} notme : Whether the calling instance, if relevant, should be excluded (true) or not (false).
/// @param {Array} array : The array to use to store the IDs of colliding instances.
/// @param {Boolean} ordered : Whether the list should be ordered by distance (true) or not (false).
/// @returns {Array}
#endregion
function collision_rectangle_array(_x1, _y1, _x2, _y2, _obj, _prec=false, _notme=true, _arr=undefined, _ordered=false) {
static __list = ds_list_create();
collision_rectangle_list(_x1, _y1, _x2, _y2, _obj, _prec, _notme, __list, _ordered);
_arr = __push_list_into_arr(_arr, __list);
ds_list_clear(__list);
return _arr;
}
//helper function
function __push_list_into_arr(_arr, _list) {
gml_pragma("forceinline");
if (_arr == undefined) {
_arr = array_create(ds_list_size(_list), undefined);
var _offset = 0;
}
else {
var _offset = array_length(_arr);
array_resize(_arr, _offset+ds_list_size(_list))
}
var _i=0; repeat(ds_list_size(_list)) {
_arr[_offset+_i] = _list[| _i];
_i+=1;}//end repeat loop
return _arr;
}
@tinkerer-red
Copy link
Author

added optimization for when no array was supplied, now it will pregenerate an array size which fits the list's size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment