The right way to translate coordinates and get a position relative to the parent node

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

I am in the middle of coding a system to make a platformer with slope detection.

The characters were saved as char.scn and instantiated to a “stage” where they interact with objects.

Then, I shot 9 rays in different positions on the fixed_process function to start the magic and also created 9 lines for the rays for a visual representation on the _draw function.

var raycast_step = 320
space_state = get_world_2d().get_direct_space_state()    
var raycast_step_multiplier = 5

ray bottom_right:

bottom_right = space_state.intersect_ray(get_corner("bottom_right"), get_corner("bottom_right") + Vector2(raycast_step_multiplier*raycast_step,0), [self] )

func draw_lines():
	if main._debug:
		#bottom_right
		draw_line(get_corner("bottom_right"), get_corner("bottom_right") + Vector2(raycast_step_multiplier*raycast_step,0),Color(50,0,0),1.0)

get_corner function:

func get_corner(corner):
	var corners = {
		top_left = get_node("top_left").get_pos()+Vector2(skin,skin),
		top_right = get_node("top_right").get_pos() +Vector2(-skin,skin),
		bottom_left = get_node("bottom_left").get_pos()+Vector2(skin,-skin),
		bottom_right = get_node("bottom_right").get_pos()+Vector2(-skin,-skin)
	}
	if corner == null:
		return corners
	else:
		return corners[corner]

When I checked for hits by printing the dictionary that results from:
the bottom_right var, I got nothing at all. This would test for collisions on the bottom-right side of the object.

After a lot of tries to pimpoint what was wrong in my code I decided to put an obstacle at the top of the screen to see if the position for the rays weren’t using the node’s position wrongly and finally I discovered that somehow, even though I used the same position code to shot the ray and also to draw the line, somehow the rays are messed-up while the lines draw are ok, even following the character.

So, what am I doing wrong? Why are the lines drawn ok but the raycast is not being shot from the right position(coming and following the character)?

:bust_in_silhouette: Reply From: henkz

intersects_ray expects global coordinates but you give it coordinates in “actor” local space.
The lines draw fine because they are drawn on the actor itself, so they are transformed with the actor.
I guess you could just add your actors global pos to both vectors you pass to intersects_ray. Unless you plan to rotate your actor, in that case you would probably xform them by your actors transform.