How to draw text in world coordinate space?

607 Views Asked by At

I'm creating a game in libGDX. I want to create some UI elements (buttons and stuff), because of my app design, I would like to draw them in the world space, as other game objects.

I'm using Freetype generator that generates a bitmap font from true type font files(.ttf). The problem is that the dimension of the font is in pixels.

Orthographic camera that I use to to render the world, has viewport size of approximately 10x10, so when I generate a font at the size of 10, it covers almost whole screen(too big) and also looks very ugly because generated bitmap for the font is too small (too few pixels).

What I want is to create sprite, draw it at same size(world space) and draw text over it, and basicly create a button.

Is there some well established way how to deal with this?

1

There are 1 best solutions below

0
Jerry Lundegaard On BEST ANSWER

Thanks to clarifying comments, I've came up with the solution.

I took a point at which I wanted to draw the text, projected it to the screen space by my world camera. Then I flipped y axis by:

point.y = viewportHeight - point.y;

Then I unprojected it with ScreenViewport (separate viewport for drawing the text, is uses camera of the size of the screen so 1unit == 1pixel). Now I can draw text in projection where 1unit = 1pixel, on the point that is at the same place on the screen as previously chosen point in world space.

I also wanted to be able to draw text inside rectangular boundaries. For this I chose another point. At this point text should end. Did the same procedure as with start point, and then calculated width

targetWidth = endpoint.x - startpoint.x;

Then I used GlypthLayout class to get actual width of my text at some(generated) font size.

actualWidth = glyphLayout.width;

And when I scaled font like this

font.getData().setScale(targetWidth / actualWidth);

my font get scaled so drawed text is wide as target width.

But be aware of another problem! When I generate bimap font via FreetypeGenerator with size bigger when approximately 300, some letters don't draw, and are missing. (probably bug).