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:
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 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.
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
You have two cameras in the project. First there is the camera.script from Defold-Orthographic and then also a Defold Camera component.
You were not using the correct camera id for the Defold-Orthographic camera. It has to be a hash(), not a string
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:
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.