+2 votes

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?

in Engine by (29 points)

2 Answers

+5 votes
Best answer

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)
by (22,067 points)
selected by

That's exactly what I wanted, thanks!

+1 vote

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: https://godot.readthedocs.io/en/3.0/classes/class_area2d.html#class-area2d-get-overlapping-areas

by (40 points)

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

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.