(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.
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
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.
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)
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
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.
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.
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.
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!