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?