Trouble converting mouse event location to in-game location on click with camera zoom

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Brandork

For the life of me I can’t seem to figure out how to get my sprites to move to the correct position. Everything works while moving the camera but adding zoom into the mix just really messes everything up.

When the Actor node detects a LMB click in the _input() method it creates a path to use for to use for pathfinding

func create_path_to_destination(
    event_position: Vector2,
    camera_position: Vector2 = Vector2(0,0)
):
    path = Navigation2d.get_simple_path(get_position(),
            event_position + camera_position)

Then on every _physics_process(delta) update I run the following code to move the character along the path

func move_to_destination(delta: float) -> void:
    var distance_to_walk = Stats.current_movement_speed * delta
    while distance_to_walk > 0 and path.size() > 0:
	var distance_to_next_point = position.distance_to(path[0])
	    if distance_to_walk <= distance_to_next_point:
	    	# warning-ignore:return_value_discarded
	    	move_and_slide(
	        		position.direction_to(path[0]) * Stats.current_movement_speed)
		else:
			path.remove(0)
	distance_to_walk -= distance_to_next_point

Again, this all works perfectly fine even if I move the camera but after zooming in or out is when this begins to break.

:bust_in_silhouette: Reply From: Whalesstate

var position = event.position * camera.zoom
try it , hope it helps

As a follow up question if you happen to see this, how did you know that would work? Does that knowledge come from your own personal experience with something similar or is it just general math skills that I am lacking?

Brandork | 2021-09-20 05:30

personal experience , dealing with control nodes and nodes2d while each one have a different space is tricky and this is one of the common problems that you will deal with , because control nodes position is using screen position and node2d or node3d are using world positions.

Whalesstate | 2021-09-20 11:54