How to convert a GUI node's position to a game world position? [SOLVED]

In other words, I want to move a game object (GO) to the position of a GUI node. How can I get the correct coordinates in the game world that correspond to that GUI node?

camera.screen_to_world(gui.get_position(node_id)) ?

I think you should use
gui.get_screen_position(node) into the camera.screen_to_world()

However, you cant use gui inside game objects, nor go inside gui, so you’ll have to pass the information via message passing (or defold-event library, this is exactly the kind of cross-context situation to use it)

1 Like

Yes, as @Mathias wrote.

Example of screen to world:

Opossite to GUI following GO, but it also might be useful: publicexamples/examples/gui_follows_go at master · britzl/publicexamples · GitHub

2 Likes

I can’t believe I completely overlooked the gui.get_screen_position API. Now I know exactly how to handle this issue. Thanks everyone for the help!

1 Like

Hello everyone,

I think it should be noted that gui.get_screen_position() returns a position with the z component set to 0 but camera.screen_to_world() expects a position with the z component equal to the distance between the camera and the resulting world position( API reference (Camera) ).

This is especially important in the case of cameras with perspective projection.

In the special case someone wants to move a GO to the position of a GUI node, and uses a camera with perspective projection it would look something like this:

gui_node_screen_pos = gui.get_screen_position(node)
gui_node_screen_pos.z = camera_pos.z - go_pos.z -- distance between the camera and the GO along the z axis (default forward axis, assuming camera was not rotated)
new_go_pos = camera.screen_to_world(gui_node_screen_pos)
5 Likes