Is there any nice way of transforming a screen coordinate to world space?

(Apart from having to track how the current camera projection is set and doing it manually)
Something like render.screen_to_world( my_screen_coord ) would be very useful.

Basically a Defold version of Unity’s Camera.ScreenToWorldPoint:
http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

What is your use-case?

I’ve positioned and zoomed the camera, and I then want to know on which side of the character the user tapped/clicked

In Unity, I’d transform the mouse position from screen space to world space, then transform that point from world space to the character’s local space, then normalize that vector to get the direction

Alternatively, transform the character’s world position to screen-space, and then do

You know the camera world position and where screen-space is in relation to that so you can do something like this in on_input():

local cam_pos = go.get_position("/camera")
local screencenter = vmath.vector3(tonumber(sys.get_config("display.width", "0")) * 0.5, tonumber(sys.get_config("display.height", "0")) * 0.5, 0)

local touch_pos = vmath.vector3(action.x, action.y, 0) + cam_pos - screencenter

If your render script changes what’s actually drawn (i.e. zoomed camera for instance), you will have to compensate for that.

How do I get the current width rather than the width/height setting in the project?
sys.get_config(“display.width”, “0”) seems to return 1280 even if I resize the window, and I presume on mobile it would still return 1280.

Yes, that is the project setting. The current window size can be found with render.get_window_width() and render.get_window_height().

In that case I need a reference to a render, no?
Getting this error when I try the code:
bad argument #2 to ‘get_window_height’ (RenderScriptInstance expected, got userdata)

Sorry, I forgot. That has to be in the render script.

This should definitely be something built-into the engine.
Just the fact that there’s no way to get the world-space position of where you clicked, if the window is ever any other resolution than the default, is rather tedious and limiting.
I’ll work it out on my end, but it’s something Defold should have in the future

4 Likes

I’m not sure a built in method is possible, given that you are free to render your game any way you want in a custom render script. You can have different views and projections on different render-passes, for instance.

It’s perfectly possible to calculate the position. The render script is accessible in the message passing system. You can have it send updates continuously somewhere, or just set up a custom message that passes back the current camera/view information so you can calculate the world position.

Another option is to let the render_script set the current screen dimensions in a module and access that module from anywhere to get screen dimensions in your scripts.

Yep, that’s now I solved it now!

function M.screen_to_world(v) local res = M.screen_res -- Screen resolution in pixels local cam_rect = M.camera_rect -- Rectangle camera covers in world space local world_x = M.remap( 0, res.x, cam_rect.x, cam_rect.x+cam_rect.w, v.x ) local world_y = M.remap( 0, res.y, cam_rect.y, cam_rect.y+cam_rect.h, v.y ) return vmath.vector3( world_x, world_y, v.z ) end

That said, I would still suggest having a built-in function for it, where you can simply specify which camera to use, and then give it a screen point, and have that be transformed to a world point. The point it returns could simply be a point on the near clip plane.

3 Likes

That said, I would still suggest having a built-in function for it, where you can simply specify which camera to use, and then give it a screen point, and have that be transformed to a world point. The point it returns could simply be a point on the near clip plane.

@Acegikmo I second that!
@sicher I think the camera usage could and should be simplified further. Especially for beginners that’s a confusing topic. (And they wouldn’t have to dive into the render pipeline inner workings to early on)

Agreed. The camera is extremely barebones, yes. It works with the default render script, as long as you keep camera position at lower left corner instead of center. It is in the pipeline to do a proper design and implementation of a camera but it’s currently not very high on the prio-list.

1 Like

This should not be listed as a 2D game engine. If I wanted to deal with this I would have used Unity or Unreal to build my game. I want to build a SIMPLE 2D GAME clicking a sprite should not be this complicated. And this doesn’t even answer the question of how to detect if a generated sprite was clicked from the main script.

Defold you are now getting deleted after spending hours learning the ins and outs of getting the base game going! Because you LIED when you said you were a 2D game engine!

Why is your first post this instead of asking questions to get help with what you want to do?

and

both have what you want in easy to use ways.

3 Likes

Defold: An engine of LIES

— Can only make games in the 5th Dimension!!! —

6 Likes