Problems with using Input.warp_mouse and Camera2D together

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

So the gist of what I’m trying to do is get a game character to choose to teleport by pressing space, and then selecting the destination with the mouse. It’s set up so that after you press the spacebar, you have 3 seconds during which you can click on a new location for your character. I’m using a 2d camera that the character drags around, and I want the mouse to end up at exactly the same position on the screen as the character at the end of teleportation.

I’ve been trying:

self.set_global_pos(get_global_mouse_pos())

to teleport the character and then:

Input.warp_mouse_pos(self.get_global_pos() - get_node(“Camera2D”).get_camera_screen_center() + Vector2(640, 400))

(–> this is for a 1280 x 800 screen, and the Input.warp_mouse positions the cursor so that the left-top edge of your current screen is (0,0), by the way)
but the mouse cursor doesn’t end up on the character. I’ve tried many variations on this line, but none have been successful, though logically I think it should work. There seems to be a changing pattern to where the cursor ends up, but I can’t figure out where the problem is, though I suspect it might be with the camera drag margins. Does anyone have any ideas, or any solutions?

On an additional note, I assume that warping just doesn’t work correctly with custom cursors? It seems like the cursor stays in the same place after the warp until you actually move it around, at which point it jumps to the new location. These might have similar root causes, I’m not sure…I wish there was a way around this.

:bust_in_silhouette: Reply From: avencherus

You will want to print out these values to make sure your assumptions are correct. Start with the get_camera_screen_center(), in my testing it puts out (0,0), so you’re basically subtracting by 0 in your formula.

The warp function as I can tell, deals with the coordinates of the viewport, and not the canvas.

Assuming the camera centers on the player and you want to snap the cursor to center, that’s going to be warp_mouse_pos(Vector2(w/2, h/2)) Regardless of where the camera is located.

Otherwise if the character can appear off center, you’ll want to do your offset calculations by subtracting from the global positions of both the player and the camera, and account for any types of other offsets and the screen dimensions. You’ll want a number that corresponds to the viewport’s rectangle.

Thank you for the answer!

I printed out all these values (with a less clouded mind), and I found out what the problem was. Basically, my value assumptions were correct, but the program wasn’t updating the Camera Screen Center until the start of the next frame; I’m not sure why that’s the case-is that intended? When I finagled the warp line to be in front of the teleport line, the code worked fine.

There are a couple of things like things about Godot like this that aren’t intuitive at all and suck up so much time, since there aren’t ready answers for everything online yet. I suppose that’s why there are Q&As like this. :slight_smile:

subtlearc | 2016-11-29 18:59

Yeah, quite right about that. I find myself probing the sequencing of things quite often.

Glad you found an easy solution.

Sometimes given the ordering of things in the processing loops and scene tree, I find myself occasionally resorting to calling deferred functions, or doing a yield().

avencherus | 2016-11-29 19:56