I'm currently in doubt if I can use coordinates(X,Y) to find if there is an shape in said coordinate, and if there is I would get/find the node which the coordinate is in belongs to?
Asked
Active
Viewed 121 times
1 Answers
0
If you want to find what PhysicsBody2D
s (StaticBody2D
, RigidBody2D
, KinematicBody2D
) are at a given position, you can use a RayCast2D
of length 0, placed at that position.
Or, you can use a Physics2DDirectSpaceState
in a similar fashion as the RayCast2D
would do behind the scenes…
Or better yet, use intersect_point
:
var position:Vector2 = whatever_position_you_want_to_query()
var world:World2D = get_world_2d()
var space_state:Physics2DDirectSpaceState = world.direct_space_state
var collisions:Array = space_state.intersect_point(position)
for collision in collisions:
var body:PhysicsBody2D = collision["collider"]
print(body)
Or a shorter (with less explicit type information) version:
var position:Vector2 = whatever_position_you_want_to_query()
for collision in get_world_2d().direct_space_state.intersect_point(position):
print(collision["collider"])
Note that the function get_world_2d
is defined in CanvasItem
and thus available in any derived types (Node2D
, Control
).

Theraot
- 31,890
- 5
- 57
- 86
-
This was spot on! Once again, thank you for the detailed explanation. – WannabeDev Aug 23 '21 at 17:06