How to know how many percent an Area2D is in another Area2D (GDScript)

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

I made a script that detect when the Area2D of the player enter another Area2D, and when the Area2D of the player enter 2 others Area2D, i need to tell to the script to do something with the Area2D that is less entered by the Area2D of the player. How do i do that ? Here is an example:


in this image, the Area2D of the player enter into both the Area2D 1 and the Area2D 2. And here the Area2D that is less entered by the Area2D of the player is the “Area2D 2”.
And i need the code to detect that by itself.

Sorry if its not understandable, i tried to do my best to explain.

1 : Use ditance_to
Measures distance when collision occurs
2 : Raycast
Raycast works when there is a collision and you can measure distance with

ramazan | 2022-03-21 09:01

:bust_in_silhouette: Reply From: Zylann

There is no built-in way to get this information. You have to code some specific logic yourself.

The easiest way would be to just check the distance to both areas you collide with, and pick the closest.

If you reaaaaally want the area whose overlapping surface is greater, you will have to do the math depending on which collision shapes you used, though that might be a bit complicated.

Note: if the overlapping area is from a TileMap, distance checks won’t work because a tilemap’s position is meaningless. You’d need tile position, which you could compute by converting hit position into tile position with world_to_map.

Ok, thanks for your help, iam a begginer trying things to learn Godot so i may keep that for later. Thanks you all for your help.

Toyuu | 2022-03-23 12:03