Use meters instead of pixels as units

#1

I am used to programming games with Unity, where tiles have a size of 1 unit × 1 unit, independently of the resolution of the image they are using. With Defold tiles have a size of N units × N units, where N is the width and height in pixels of the image.

I personally prefer working in logical units (let’s call them meters), like in Unity. For instance, saying that an object accelerates at 9.81 ms2 is IMHO much cleaner than saying that an object accelerates at 9.81 · whatever pxs2. As for a more practical reason, I don’t even have decent assets right now, which means the resolution of my images might change in the future!

From what I’ve found, Defold’s world units are still logical units, but they coincide with pixel units. I tried creating a custom render script, but had no luck. I didn’t find anything else in the documentation that could help.

So, how would I accomplish what I described with Defold? Any help would be greatly appreciated.

0 Likes

#2

You can zoom in your camera with the built-in render script by sending it a message, see the manual here.

You should also edit the physics scale in your game.project (manual here). Usually it’s set to 0.02, so 50 pixels/meter.

Usually I prototype games with placeholder images that are quite large. I build everything with plenty of game objects so I can scale everything however I like in the editor, so the image scale doesn’t matter. Then later when things are more final and I’m optimizing, I scale the images to the final size and remove any game objects that I don’t need.


I’m just curious, is this possible to change in Unity? Recently I tried Unity and I wanted to do the exact opposite of what you want (change the default unit from “meters” to pixels), and googling did not turn up any way to do it, except by editing the import settings on every single image in your game.

2 Likes

#3

I tried doing what you suggested, however the world is zoomed in only at run time. What I want is to be able to edit the tilemap such that each tile occupies only one unit. Below is an example of what I mean (in red is the desired outcome):

As for Unity, if you want to use pixel units instead of meters you have to set the Pixels Per Unit to 1 in the Import Settings for your atlas image, and set the tilemap grid Cell Size to (N, N, 0), where N is the width and height of each tile:

1 Like

#4

Hmm. Well if you want the rulers on the viewport to show units like that in the tilemap editor don’t think it’s possible, but it shows you the cell coordinates separately, so I don’t see how that would really matter.

In your collection you can scale down the game object that the tilemap is on so it matches the scale you want. (This actually got broken in the editor in the last Defold update, but should be fixed soon. If you need to downgrade to 1.2.160, go here. )
(use the sha: 1a8e53ae9c38a09b742d38dffc6a9f2efdbe6e97)

Testing with 64px tiles, I scaled my game object to 1/64 (unfortunately if you type 1/64 into the scale field it rounds it to 0.02).


Gotcha. Yeah I found that in Unity. There’s just no way to set the default values, and I was too lazy to edit dozens of textures by hand. (Obviously Defold doesn’t let you conveniently change it either.)

3 Likes

#5

That solves it! Since I just started using Defold I thought it wasn’t in the engine’s capabilities to scale tilemaps (in the same way that Unity can’t rotate them). Of course, scaling would have been the obvious first approach, but I would never have thought that it was a bug.

Thank you very much, cheers! :smiley:

3 Likes