Remove tiles in TileMap that overlapped by a Area2D

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

Hello everyone, new member here. I need a little help, I asked this question before on reddit but I haven’t found a solution yet.

I have a main Scene World, in that I have a TileMap and a Area2D (see here).
TileMap is the brown wall texture, and Area2D is the Godot logo.

My goal here is find whichever tile overlapped by Area2D and remove them. As you can see the Area2D overlap 2 tiles and I able to print the current overlapped tiles which they have the same ID (?).

extends Area2D

func _on_ClearBox_body_entered(body):
	for i in self.get_overlapping_bodies():
		if i is TileMap:

I can’t seem to remove the overlapped tile. I tried to use set_cellv but I can’t get the position of those tiles, i.global_position() return (0,0)

What I’m missing here? Thanks everyone.

:bust_in_silhouette: Reply From: exuin

The tilemap counts as one giant body. Since in this case the area is a rectangle, you can use Rect2 methods to find the overlapping cells.

Thank you for your comment. But can you explain further the Rect2 methods?

khanhvdv | 2022-12-11 07:52

Use the intersects method

so area_rect.intersects(Rect2(cell_x, cell_y, tile_x, tile_y))

exuin | 2022-12-11 17:47