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

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

Hold your horses. You are overreacting. If you would have asked this question here then we would have presented you with two camera extensions, both professional quality in terms of stability and performance. I would also have shared this extension specifically aimed at input handling: https://github.com/britzl/defold-input and this basic example: https://github.com/britzl/publicexamples/tree/master/examples/click_game_object

We should perhaps mention these in one of our manuals, but for now we recommend them here. Ping @sicher

4 Likes

Yes. I wonder what would be a good place to put this information. Maybe a FAQ entry and something in the input manul perhaps?

1 Like

Please spend at least a little time to read up on the product and ask questions before jumping to conclusions. Also, we try to keep the tone civil here.

3 Likes

First I would like to apologize for the tone of my first post. I don’t usually go off like that and it was at the end of a long day where I was trying to do something that should have been simple.

Second I always try to find if my question has been asked already before posting it. It had and that thread led me to this one (sorry I lost track of the original thread).

Third this is not the only issue I have had with the engine. One of the first was when I found out there was no simple way to lock a mobile game into landscape orientation when almost every mobile game I play is played in landscape mode.

Last I would like to give a bit of my backstory. I have an autistic son, a 4-year old son, and a 2-year old daughter who all really loved a certain balloon popping game on their kindles. The problem was that it was so full of adds they would often click them and get frustrated wanting the game back. I have a degree in game development that I don’t use for work and I decided to try my hand at remaking the game so I could add things as they got older.

I originally made the game using phaser-ce and put it on a webserver. Then the issue was they would click on the address bar and get frustrated. So I googled ‘2d mobile game engine’ and this was one of the top picks. At first it seemed simple enough. I caught the line ‘this is technically a 3d engine optimized for 2d’ in the documentation and that didn’t bother me.

I first started being frustrated with the editor. No code folding, not being able to expand a node if it selected, funky keyboard shortcuts for my Dvorak keyboard (using scancodes instead of actual keys for the keyboard shortcuts). I grit my teeth and dealt with it. I worked through most of the tutorials to learn the ins and outs of the engine and then took a stab at building my balloon pop game. I didn’t need a loading screen or even a UI as this was going to be simply for personal use.

It started off going well. A little hiccup with the landscape orientation I ignored. I was able to add the background, and figured out how to use a tilemap for the balloons (which is stored as a single image). I figured out how to use the physics system to give the balloons a bit of gravity pulling them up. I figured out how to destroy the balloons when they leave the screen. Then I got to the tapping part and got stymied (yes I said stymied). That led to this post as I had spent several days learning the engine and tools to find out something as simple as tapping on a sprite couldn’t be handled by the engine. If there are plugins they should be mentioned in the documentation.

Since my initial post I have rediscovered love2d which has come out of beta since I last looked at it. It shows great promise as it took me less than a day to get to the same point in development as I got with defold in 3 days of work.

I wish you luck with defold. I think the engine needs a lot of work though and trying to gear a 3d engine for 2d games seems a bit more cumbersome than I would want to deal with.

3 Likes

Yes, there’s a very simple way. Set width and height in game.project to a landscape ratio (ie wider than the screen is high) and make sure that Dynamic Orientation is unchecked.

There’s currently a bug that if you leave the default values of 960x640 they will actually not be used and the game will end up in portrait mode anyway. This will be fixed in Defold 1.2.136 (in two weeks).

It is not as simple as it sounds. You as a developer have full control over the rendering pipeline. It means that you can do absolutely anything with how the game is rendered. You can render the entire game in the top left corner of the screen or zoomed in our inverted or anything else. This makes it impossible for the input system to know how to translate screen coordinates to world coordinates and by that also know exactly where on the screen a game object is rendered.

Some engines may have solved this by limiting the rendering or through an advanced camera system that takes care of input translation. We decided to not solve it for the user and instead let the user pick a solution that works for them. Two solutions are RenderCam and Defold-Orthographic, both available from our Asset Portal.

This is part of the problem. The second part is how to once you have a world coordinate know if that coordinate is touching a sprite. Some engines solve this for you. We don’t, well at least partially. For gui nodes there is gui.pick_node(node, x,y). For game objects there are a number of different solutions. One that is provided by the engine is through the use of collision objects. One collision object component together with the sprite and one collision component following the mouse or touch. Quite easy to set up.

Our ambition with Defold is not to solve all problems for our developers. Instead we try to provide a set of APIs and tools that allows developers to solve the problems themselves in a way that bests suits their game and their target platform(s). This philosophy doesn’t suit everyone, and for those that don’t like it there’s plenty of other game engines to use. Good luck with Love2D!

12 Likes