Please Help - Big problem with coordinates system

I’m new to Defold and I’am dealing with the differences between the screen and game world coordinate systems. I come from Solar2D, where it is very easy to position an object. While my learning ability and logic are relatively good, I still don’t understand Defold.

I’ve added the “Mobile Template” from the Defold templates and made some small adjustments to it:

1- I added the defold-orthographic library to my project. I don’t use any camera component.
2- In the init() function of the library render I saved the dimensions of the screen in a module to later use them:

local my_module = require ( "scripts.my_module" )
  my_module.sW = render.get_window_width()
  my_module.sH = render.get_window_height()

3- Then in main.script I try to position my game object (its center) in the bottom-left corner of the screen, to test the screen_to_world function:

local camera = require "orthographic.camera"

msg.post( "camera", "use_projection", { projection = hash("FIXED_ZOOM") })
    
local x0, y0
    local v3_val = camera.screen_to_world( hash("/camera"), vmath.vector3(0, 0, 0) )
     x0, y0 = v3_val.x, v3_val.y
    go.set_position( vmath.vector3(x0,  y0,  0), "go1" ) -- logo

The result:

How to position an object relative to the device screen, regardless of the configured projection?

1 Like

It’s hard to say why it is not working for you. It should work. I can recommend that you attach a minimal project as a zip here.

I also answered your question in the other forum thread just now. I’m reposting my answer here too:

I’ve updated the sample-screen-to-world-coordinates project so that it also shows how to anchor game objects to the corners of the screen.

The sample project will also let you change the camera component zoom and also set the equivalent of Fixed Fit Projection mentioned in the render manual:

Thank you @britzl
I’ve tested the sample proyect above, and works perfectly, but as soon as you change the width of display property into your game.project file, doesn’t work.
I changed the width to 450 and this is the result:

Did you make any other modifications? The camera isn’t even centering on the player game object anymore… ?!

Ah, I noticed that you are using Defold 1.8.0. Starting in Defold 1.8.1 there is no longer any need to call “acquire_camera_focus”, it is done automatically. But the project will not work if you are using 1.8.0. I’ve put back that line of code so that the sample project is backwards compatible with 1.8.0 and older. Please update and try again.

Thank you @britzl
I updated Defold just now… Downloaded “sample-screen-to-world-coordinates” project again from Github.
When I run the project I got this:

the logos doesn’t appear…
Pressing the ‘Tab’ key, they also do not show in Zoom 3. The only way they show well (in the corners) is in AUTO ZOOM:

By the way, the project (https://github.com/defold/sample-screen-to-world-coordinates/tree/master) include the “acquire_camera_focus” . It is placed into the init() function of camera_controls.script

I know. I added it there two hours ago:

The crosshairs you see in the lower left and top right corners are the two game objects that are always anchored there regardless of the aspect ratio or zoom or whatever projection and view that you use. This code achieves it:

The logos are positioned at the bottom left and top right corners of the area specified in game.project width and height. The logos will not be shown when the zoom is set to 2x since you then have a hardcoded zoom that makes you see only half as much as specified in game.project. The same is obviously true for a 3x zoom.

The logos will be shown when the zoom is 1x (ie no zoom) or when set to Auto which will adapt the zoom level so that the area covered by game-project width and height is always visible.

Thank you so much for your help @britzl
I now understand better, but what if I want to include the defold-orthographic library and use that code from the sample in Github… the sample use a camera component. Do I have to include a camera component in irder ti use that code? I ak you because I’m thinking in a completely 2D game, without cameras.

I would recommend that you use a camera even if it is never moving. A camera is not only used when you need to move around in a large game world and only render a portion of it, it is also useful in a 2D game which is played on a single screen.

BUT if you don’t want to use a camera then don’t use Defold-Orthographic or the default camera component. The only thing you need then is the view and projection matrices that are used in your render script. I wrote about this elsewhere. You can share them in a Lua module or as global variables.

Thank you @britzl
I prefer to use a camera component as you said, because in some near future I would like to use the 3D features in 2D games.

The only drawback of not using the defold-orthographic library or the default camera is that when the default projection (Stretch) is changed to Fixed Fit or Fixed, the world coordinate system will shift. The default projection is not ideal because it stretches the content if the screen ratio changes. Therefore, it is practically necessary to change the default projection to Fixed Fit or Fixed, and by doing so, the world coordinate system will shift. Then, alternatives like the screen_to_world function will be needed to determine the coordinates of the screen edges.

Correct. And with the solution provided here it is not very difficult to position something at the screen edges?

I’m testing it now. I chose the “Mobile Game” template, I included it the “defold-orthographic” library and trying to position an object in the bottom-left corner woth the two options (defold-orthograpic and the sample from Github). I created a lua module called camera with the functions from the sample in Github.

Using the Github example I don’t get the object positioned correctly, and when I use the fold-orthographic library I just get an error -attempt to index local ‘camera’ (a nil value)- which I don’t get with the first option, where everything is fine in the code, it’s just that the object is not positioned correctly. Could you take a look at my project and tell me what I’m doing wrong, please?
Mobile Game.rar

There are several issues:

  1. You have two cameras in the project. First there is the camera.script from Defold-Orthographic and then also a Defold Camera component.
  2. You were not using the correct camera id for the Defold-Orthographic camera. It has to be a hash(), not a string
  3. Some other smaller things were also a bit confusing

I’ve cleaned up your project so that it only uses Defold-Orthographic with FIXED_AUTO projection. It correctly positions the game object in the lower left corner. Here’s the project:

Mobile_game-fixed.zip (411.2 KB)

2 Likes

Great! … thank you so much @britzl … I’ll tested it and let you know…

Awesome… the project works perfect… :grinning:

I’ve this doubt: in the main.script there isn’t any setup for the projection. Nevertheless, the result doesn’t appear to be the default projection (Stretch) because the circle is not stretched. So, why don’t you include any setup for the projection mode? It seems to be either FIXED_AUTO or FIXED_ZOOM.

The setup is made on the script properties of camera.script. Select the script and modify properties in the Properties panel:

You can use FIXED_ZOOM, FIXED_AUTO or DEFAULT

1 Like

Right! Thank you once more, @britzl