0 votes

I tried getting the intersect_point() to work in a 2d world, but i cant make it get me any results. I'm using it in a node2d player scene and am checking for node2d scenes with static or rigid bodys attatched to. I couldnt find any examples how to use it, so i'm asking here.

I tried to make it work on the (0.0) coordinate first, but coudln't make it return anything.
What am i doing wrong?

Also how to check a position relative to my player scene?

Array intersect_point ( Vector2 point, int max_results=32, Array exclude=Array(), int layer_mask=2147483647, int type_mask=15 )

func _fixed_process(delta):
var space_state = get_world().get_direct_space_state(self.get_world().get_space())
var result = space_state.intersect_point( Vector2(0,0))

if (not result.empty()):
print("Hit at point: ",result.position)
in Engine by (81 points)

The doc says intersect_point will check if a point is inside a shape. Which kind of collider are you trying to detect?

You are getting the 3D direct space state, but are then using a 2D method called "intersect_point", maybe its related?

  1. What exactly is a shape in godot? Does a simple image already have a shape? Like a recangle of its measures? I thought it checked for collision shapes only. So i tried to test with a Staticbody2d with a rectangle CollisonShape2d.
  2. so "getworld()" is always 3d and i have to use "getworld2d().getdirectspacestate" instead?
  1. Well, collision shapes are the thing you need, sprites don't have a "shape" at all. To be fair, a collision shape on its own won't be enough, it would need a collisionbody as well.
  2. Yeah, that's correct

1 Answer

0 votes

Got it now, thanks you two. Just for reference, here is how it works:
The following code suggests how you could make your player-scene interact with. You should probably loop trough the result array, if you could get more than on instance at the same position. Also might group scenes with interact() together and check for it before calling interact().
But anyways it should show how to basically use intersect_point()

#check 10pixel left of current object for something with a collision shape:
var position=get_pos()+Vector2(-10,0)
var result = get_world_2d().get_direct_space_state().intersect_point(position, 1 )
if (not result.empty()):
    print("Hit this: "+str(result[0].collider)+"and called function interact() of that instance")
else: print("nothing hit")
by (81 points)
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.