getting intersect_point() to work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By VincePrinceKing
:warning: Old Version Published before Godot 3 was released.

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)

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

Zylann | 2016-11-07 13:45

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

Bojidar Marinov | 2016-11-07 13:52

  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 “get_world()” is always 3d and i have to use “get_world_2d().get_direct_space_state” instead?

VincePrinceKing | 2016-11-07 14:25

  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

Bojidar Marinov | 2016-11-07 14:27

:bust_in_silhouette: Reply From: VincePrinceKing

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")
	(result[0].collider).interact()
else: print("nothing hit")