#Tile Collision That Returns The Tile Index

1 messages · Page 1 of 1 (latest)

warm light
#

This is one of those "ok this is sort of a specific use-case and I have alternative solutions but I would prefer it to work this way" type questions but:

I would like to be able to grab the exact tile index of a tile something collides with and while GM now supports colliding with tile layers, the functions only return the tilemap index which is still useful but it doesnt tell me the type of tile.

So my question is: whats a good approach to this? Im currently considering having the game grab points via the bounding box coordinates + offsets (while also accounting for the width being possibly bigger then the tile size while also accounting for being over multiple tiles, etc) but if people have better suggestions that would be Swell.

The specific context is a platformer engine that wants slopes and one-way solids to be interactable/detectable thru the movement code. I can't really depend on using only the center point of the sides of the bounding box because of it missing a second tile that it should be colliding with or falling off a ledge prematurely. I can use corners for this too but like, yeah. Its a whole thing.

warm light
#

Naturally i already got these sorta things working with objects but having it available via tiles would speed up level creation lol :v

long pond
#

classic GM approach is to use invisible objects for this, yep

#

otherwise you'll need to iterate over the tiles that the sprite is covering. this is easy enough for AABBs but gets trickier if you're using precise collisions

warm light
#

Yeah FORTUNATELY for movement collision between solids and non-solids I prefer to keep it as rectangular as possible but damn

tight beacon
#

so instead of having it return false, if you alter it to return a struct, you can have both:

//instead of returning false, save the collision result to a var like result, and return a struct with both the collision status and the tile index:
var col_result = false;
return { status : col_result, tile : _tile };
#

example:

///@description tile_meeting_precise(x,y,layer)
///@param x
///@param y
///@param layer
var col_result = false; //will be set to true if theres a collision
var _layer = argument2,
    _tm = layer_tilemap_get_id(_layer),
    _checker = obj_precise_tile_checker;
if(!instance_exists(_checker)) instance_create_depth(0,0,0,_checker); 


var _x1 = tilemap_get_cell_x_at_pixel(_tm, bbox_left + (argument0 - x), y),
    _y1 = tilemap_get_cell_y_at_pixel(_tm, x, bbox_top + (argument1 - y)),
    _x2 = tilemap_get_cell_x_at_pixel(_tm, bbox_right + (argument0 - x), y),
    _y2 = tilemap_get_cell_y_at_pixel(_tm, x, bbox_bottom + (argument1 - y));

for(var _x = _x1; _x <= _x2; _x++){
 for(var _y = _y1; _y <= _y2; _y++){
    var _tile = tile_get_index(tilemap_get(_tm, _x, _y));
    if(_tile){
     if(_tile == 1) col_result = true;

     _checker.x = _x * tilemap_get_tile_width(_tm);
     _checker.y = _y * tilemap_get_tile_height(_tm);
     _checker.image_index = _tile;

     if(place_meeting(argument0,argument1,_checker)) col_result = true;
    }
 }
}

return { status : col_result, tile : _tile };

something like that^