0 votes

hi, how to detect all tile under my area2d?
when i use onArea2Dbodyentered, 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?
enter image description here

in Engine by (421 points)

1 Answer

0 votes

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

was something wrong with this ?

by (7,925 points)

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.

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 worldtomap 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.

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 worldtomap pos)
  4. next i can detect all it with circleShape Area2d, using area_entered

quite :)

  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 = worldtomap(globalmouseposition)
globalposition = mapto_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 :) You can also make it more fluid, separating snapping function for main shape and minisquares

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.