I'm making a game in LibGDX. I use Box2d to manage my game world. I'm thinking of a way I can display things like a health bar or nickname related to the body. I'm currently converting world coordinates to screen coordinates using the project() method to move elements around in Stage accordingly. But there's a problem with this - I'd like elements to be visible as objects in the game world, not the UI. I mean taking into account camera zoom, scaling, etc.
For example, let's say I want to display a Label over a body. I can do this by converting the appropriate coordinates and assigning them to labels, but that doesn't scale the text to match the parameters of the camera. How can I get the desired effect?
Rendering things like text in the world-view can be done by moving the position of
Labels that are not added to any stage, simply call thedrawmethod directly on theLabeland pass aSpriteBatchthat has a projection matrix set from the world camera.You can also forgo using
Labels all together and "manually" render the text using direct draw-calls, this gives you a greater level of control at the cost of having to write a bit more code (I would do it this way). TheBitmapFontsdrawmethod will be the basic way of doing this.In the example below I am using both techniques to display some text that appears next to a Box2D body and it is also affected by the zooming in that the camera does:
The full source code for the example is below (it uses the default font from the libGDX examples);