0 votes

I'm casting a point and would like to get the node that the point collides with and then check if that node containst a specific component/script. I'm basically making an RPG interact system. My question is, how do I get the node from the return of the intersects_point()?

in Engine by (46 points)

1 Answer

+1 vote
Best answer

From the reference:

Check whether a point is inside any shape. The shapes the point is inside of are returned in an array containing dictionaries with the following fields:

  • shape: Shape index within the object the point is in.

  • metadata: Metadata of the shape the point is in. This metadata is different
    from Object.getmeta(), and is set with Physics2DServer.shapeset_data().

  • collider_id: Id of the object the point is in.

  • collider: Object the point is inside of.

  • rid: RID of the object the point is in.

The return value of intersect_point is an array containing one dictionary per intersecting collision body.
Each one of those dictionaries has a key named collider that has the colliding CollisionObject2D (a Node) as its value.

If the intersecting shape doesn't belong to a Node, collider will be null (should only happen if you work with Physics2DServer directly).

If you want an array containing just the nodes, you could use this code:

var return_value = direct_space_state.intersect_point(...)
var colliding_bodies = []
for dictionary in return_value:
    if typeof(dictionary.collider) == TYPE_OBJECT:
        colliding_bodies.push_back( dictionary.collider )
by (1,082 points)
selected by
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 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 webmaster@godotengine.org with your username.