Can I check if position is overlapped by Area2D?

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

I just simply want to test if a position with coordinates x and y is inside Area2D or inside CollisionShape. I don’t use get_overlapping_bodies function, because position is not body. Can I do that, or it is only possible with collision bodies and function get_overlapping_bodies?

:bust_in_silhouette: Reply From: mateushmm14

i’m not sure if i understood your question, but yes, there’s a get_overlapping_areas ( ) function which works the same way as get_overlapping_bodies ( )

docs: Area2D — Godot Engine (3.0) documentation in English

Well, position is not an Area2D so that won’t work… but thank you for trying to help me

Marevixo | 2018-05-05 17:41

:bust_in_silhouette: Reply From: kidscancode

There are a few ways to approach this. Without knowing more about your setup, I’d probably go for the Physics2DDirectSpaceState solution using intersect_point().

You pass it a your location and it’ll tell you what objects it collides with. If you’re using collision layers/masks, you can be filter it so that it would only see the Area2D you care about.

var space_state = get_world_2d().direct_space_state
var result = space_state.intersect_point(point)

That’s exactly what I wanted, thanks!

Marevixo | 2018-05-05 17:42