0 votes

Hi,

A quick overview - I have a sprite that uses the Area2D signal _input_event to detect it has been clicked and then highlight grid tiles on a tilemap. I want to then click one of those highlighted tiles and move the sprite there, which is where the problem arises.

I am retrieving the global mouse coordinates when a mouse depress is detected and then using world_to_map to obtain the tilemap coordinates . Using _input(event)on my sprite script I can obtain event.position or event.global_position and that works fine until I change the camera position at which point I get the wrong tilemap coordinates.

I have tried using get_global_mouse_position and this works well even when the camera moves. However, using this method means that it will detect where the mouse last was rather than the click event, so if I move the mouse quickly after clicking it can give a false result. So my question is; what is the difference between using event.global_position and get_global_mouse_position in my case? Is there a better way to do this?

My scene structure is as follows:

Node2D (Root)
 -Camera2D
 -Tileset (parent)
   -Sprite (child)

Thanks

asked May 26 in Engine by McVities Chocolate (23 points)

1 Answer

+1 vote
Best answer

event.global_position gives you the position of the event that triggered the _input(event) function. It can be any kind of event, even mouse movement itself is an event., or a key stroke.
get_global_mouse_position() will instead give the position of the mouse position indipendently from any input.

if getglobalmouse_position works in your project, you can combine it with _input(event) so that it only triggers at the right moment.

func _input(event):
  if event.is_action_released("left_mouse"):
    var pos=get_global_mouse_position()

Or you could to some math and calculate what you need from the event.global_position and the translation/rotation/zoom of the camera

answered May 27 by Andrea (519 points)
selected May 27 by McVities Chocolate

Hi, Thanks for the answer. Still a little confused on the real difference between using event.globalposition vs getglobalmouseposition? See below: should these be the same?

func _input(event):
if event is InputEventMouseButton:
    if not event.pressed:
    #both print the same until camera zooms out, then both print different
        print ("get_global_mouse_position" , get_global_mouse_position())       
        print ("event.global_position" , event.global_position)     

no, in detail

  • get_global_mouse_position(): returns the position of the world below the mouse, this is affected by camera zoom (because greater the zoom, greater the world coordinate span).
  • object.get_local_mouse_position(): same as above, but the position is relative to the object (if the object is the camera, the center of the screen is always (0,0)). Basically is the same as doing get_global_mouse_position()-object.position
  • event.global_position returns the pixel coordinate of the event on the window (top left of the windows is always (0,0), bottom right corner is (window width,window height). Unaffected by camera position and zoom
  • event.position return the position of the mouse relative to the viewport. In normal cases the viewport=window screen, but you can modify the viewport size and stretch it. For example if you set the viewport.size=Vector2(100,100), the event.position will return (0,0) on the top left corner, and (100,100) on the bottom right.

Great, that clears everything up. Thank you!

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.