hi, how to detect all tile under my area2d?

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

hi, how to detect all tile under my area2d?
when i use _on_Area2D_body_entered, i will get just node TileMap:
when i do like this, it wont detect all tile.

func _on_Area2D2_body_entered(body: Node) -> void:
	var aread2d = get_node("Area2D2")
	var posWorld2map = body.world_to_map(aread2d.global_position)
	print(body.get_cellv(posWorld2map))
	print("map to world = ", body.world_to_map(aread2d.global_position))
	
	
	if body.get_cellv(posWorld2map) == 8:
		body.set_cellv(posWorld2map,10)

how should i do it?

:bust_in_silhouette: Reply From: Inces

I already answerde this question :
https://forum.godotengine.org/121904/how-can-i-detect-cell-tile-around-my-player

was something wrong with this ?

no, that the right way do detect all tile around my player/object, thank you.
but this time, i want to find all tile under area2d,
example if i use other shape other than circleShape like star shape, i want all tile under that shape can be change,
so first i need all tile position under that aread2d shape. i already put collison on that tile.

potatobanana | 2021-12-05 15:48

Oh, that is much more complicated task
TileMap is not natural to detect detailed collisions. If You really need every possible custom shape to interact with tilemap like this, I would try to make a custom node to adapt shape of collision into tilemap

This node ( lets call it squarer ) should consist of Area2D and empty collision shape. When shape is chosen in your project squarer should first instance big square of smaller areas ( with small suare collision shape ) in rows and columns aligned with tilemap scale. In process() squarer will have a code to snap to tilemap - having its position modified to world_to_map of mouse cursor. Finally, on click, squarer will iterate through small square areas that are colliding with the main shape, and each of smaller square position will be a global position of tile You need.

Inces | 2021-12-06 09:35

if i understand correctly.

  1. make new scene
  2. then make area2d with single cell tile size.
  3. then instance that new scene on top of tile cell i want to detect( exmple cell 8, then put in world_to_map pos)
  4. next i can detect all it with circleShape Area2d, using area_entered

potatobanana | 2021-12-06 16:11

quite :slight_smile:

  1. Make a new scene. It is Area. We will call it Squarer. Its children :
    a) Collision shape. Leave empty in editor, You will choose your shape and range by code
    b) Node2D. It will be container for tiny areas later. Lets call it Container

  2. Make a new scene. It will be subscene of Squarer later. It is Area. We will call it minisquare. Set its collision shape to square of tile size.

3.You will instance Squarer scene when in need, and free it when used.
In ready() it will have something like this ( pseudocode )

for x in range(-size,size)
       for y in range(-size,size)
              var inst = minisquare.instance() #preload it earlier
              inst.position += Vector2(tile_size * x,tile_size * y)
              

4.IN process() Squarer will have:
var mouse = world_to_map(global_mouse_position)
global_position = map_to_world(mouse)

5.On click or press buttom Squarer will have :

var collidingtiles = []
For miniarea in get_overlapping_areas():
          collidingtiles.append(miniarea.world_to_map(global_position)

This is pseudocode, so You will have to add details for syntax to work with all your references :slight_smile: You can also make it more fluid, separating snapping function for main shape and minisquares

Inces | 2021-12-06 20:48