TileMap issues

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GodotNoob999

I need my explosion to destroy tiles, but when the explosion area collides with the tilemap, it returns: [TileMap:1278], meaning I cannot get an individual tile’s position.

:bust_in_silhouette: Reply From: kidscancode

The whole point of a TileMap is that the tiles are batched together and treated as a single image and a single collider. This makes tilemaps much more efficient, but means you can’t treat individual tiles as separate objects.

If you want to find which tiles are affected by the explosion, you’ll have to find that using the explosion’s position - convert it to map coordinates using world_to_map() - and calculating the tiles that are within a certain distance of that point. You can remove a tile by setting its index to -1.

I already came to that conclusion - I wanted to know if there was a better way.

What’s the best way of calculating what tiles were hit? My explosion is circular, so I cannot use Rect2.

GodotNoob999 | 2018-06-12 17:52

The “Midpoint Circle Algorithm” is probably your best bet here. Here’s a starting point I found with a quick search: 2d - How can I calculate "All squares within R" in a natural looking manner? - Game Development Stack Exchange

kidscancode | 2018-06-12 18:02

Thanks for your help!

GodotNoob999 | 2018-06-12 18:08

In case anyone’s interested, I ended up with this:

func purge_circle(pos, radius):       
    for y in range(-radius - 1, radius + 1):
	    for x in range(-radius - 1, radius + 1):
		    if (x * x) + (y * y) <= (radius * radius):
			    set_cellv(pos + Vector2(x, y), -1);

GodotNoob999 | 2018-08-09 11:37