0 votes

Hello guys :) :

I'm learning the warpmouseposition(to: Vector2) method and I don't know why, but I can't make the mouse to warp in the desire position, this is my tree scene:

Node2d (parent) (script)
Area2d
Collisionshape2d

parent script:

extends Node2D

onready var Area2d_n := $Area2d
onready var Collisionshape2d_n:=$Collisionshape2d
onready var position_1:Vector2=  $Area2d.global_position

func _on_Defense_Area_mouse_exited() -> void:
    print("area_exited")
    Input.warp_mouse_position(position_1)

these are the transforms position of the nodes:
Node2d= (0,0)
Area2d =(256,256)
Collisionshape2d= (0,0) relative to his parent Area2d

So my intention is to limit the mouse position. When the mouse leaves the Area2d go back to the center of the Area2d. But it adds a weird offset, and even trying this code:

 onready var position_1:Vector2=  $Area2d.get_global_transform_with_canvas().origin

Still give me a weird offset that is closest to 0,0 and not in the center of the Area2d center position. Anyone knows why and how to fix it?

Thanks a lot.

in Engine by (26 points)

1 Answer

0 votes

I answer myself :)

I found the solution here:

I quote:

Screen space and world space aren't equivalent. When you use
globalposition you get coordinates in world space. Think about it:
the screen is limited by the bounds of the viewport, but the world
goes from -infinity to +infinity on all 3 (or 2 if 2d) axes. When you
take a node's global position, you get a position that is somewhere on
this infinite plane. But the warp
mouse_position expects a coordinate
in within the bounds of the viewport.

You need to convert from world to screen space in order for that to
work. To do that, multiply by the viewport transform: newmousepos =
getviewporttransform() * global_position

Theoretically I think I can understand it but I would need concrete examples apart from the mouse to know where that conversion is needed.

by (26 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 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 webmaster@godotengine.org with your username.