Libgdx how to display monster name above their head

Thank you for your answer. Can you provide any examples from what i’ve posted.

Well i think i’m on the right way. I’m using a second camera and font gets drawn right. But problem is that, if player moves, the text moves with him and do not stand still above monster head.

I’m gonna assume once again that you have not set the projection matrix as it sounds like its moving with the screen. So do this with the camera.

yourSpriteBatch.setProjectionMatrix(secondcamera.combined);

do this where you first created the second camera and began drawing your entities

Quick crash course:

Orthogonal matrix is a projection matrix. It maps the screen coordinates you give (X 0-800, Y 0-600) to normalized unit coordinates (X 0-1, Y 0-1). This is for the OpenGL backend of libgdx. It makes it so 100 pixels is 100, not 100/800 or 100/600. (Assuming 800x600 screen)

Model-View matrix is a transformation matrix. Its a matrix made from the position, rotation, and the scale(size, 0 being virtually invisible). This is the base point of where the model would be - in this case a 2d square (two triangles). This doesn’t account for the size. If you were to draw all points of the square here, they would be in one corner of the triangle (the origin).

That is why you then transform from the origin to wherever the point needs to be. This is done per vertex (point). You multiply these 3 together in this order. OrthoMatrix * ModelMatrix * Vec3(offsetx,offsety,offsetz).

The Camera matrix, or whatever people want to call it, fits inbetween model and vertex offset. This is a further offset from the Model matrix. It allows to emulate the objects by offsetting them, for the fact there is not such thing as a camera in game programming. “move the world around instead of player”

… bottom line …

Make sure you have an ortho matrix, camera matrix, and a model matrix generated from the position, rotation, and scale. libgdx may do most of the work for you, so you should research more on libgdx in these areas - not necessarily matrix portion, but make sure something is happening to get the disired result.

Thank you everyone, for trying to help me. I have updated the first post. Since i know im near to the solution, but it does not work :frowning:

I newer Used Libgdx ^^ - but i can show how it must be in layout


	@Override
	public void render(float deltaTime) {
		// clear screen
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		// seprate update logic
		update(deltaTime);

		// set camera to what we see
		worldRenderer.renderer.setView(camera);
		worldRenderer.renderer.render();
		//Why? dublicate render 
--		worldRenderer.render();

		//this is painted in 2D - screen space - like GUI (inventory button etc)
		//don't need change projection matrix you already in orthographics projection 
		//maybe need reset matrix to base - if Libgdx have same worlds matrix and GUI
--		Assets.font.draw(game.spriteBatch, "Firefox", uiPosition.x / 2, uiPosition.y / 2);
		// Game GUI
++		GUI.draw();
	}
	//////
	public worldRenderer.renderer.render(){
		world.getPlayer().render();
			for (Monster monster : world.getMonsters()) {
				monster.render();
			}
		///Render Text - HP bars DMG numbers etc
		//Don't need change camera matrix 
		world.getPlayer().render_Text(); // + y offset to be over Player head - "Player"
		for (Monster monster : world.getMonsters()) {
			monster.render_Text();// + y offset - "Firefox" (x,y = monster Position)
		}
	}

Because for some unknown reasons ppl that use Libgdx can’t answer :stuck_out_tongue:

p.s Libgdx is not simple for beginners - maybe try RPG Maker, or Unity, Unreal

Hello,

Thank you for your respone. About duplicate render, one is render the tiled map and another is rendrering creatures.
Without setting a new projection matrix, then the GUI stuff i render gets unit scaled, which is not ideal.

Just use one camera and use batch.setProjectionMatrix to that camera.
set it to null when drawing gui

I tried:

game.spriteBatch.setProjectionMatrix(null);

Got NullPointerException error.

Hhhm, you could also use 2 batches, spriteBatch/hudBatch.

Then im back at square one. Where the name does not attach to the monster. If you read my first post, it will happen same thing. Also i’m share same batch with my hud.

draw the name in the monster´s render function not in the hud then it should be fine

Well that would work if i was not using unit scale :-\

Also wanna Thank you for trying to help me, really appricate it :slight_smile:

font.setScale(w.e. you use);
draw name
font.setScale(1); //reset

Sorry, but this did not work. I scaled it down as much as possible, but the font gets disorted and does not display whole name:

http://1.1m.yt/N8CazFU.png

Don’t try zoom font) its 2d - create new one with prefer size 8-14 ttf

"Fonts in libgdx are scaled in pixel dimensions and not your game world units. The best course of action here is to maintain two sperate cameras to render your game: world, and ui.

The world camera will be in your world units as you have it now. The ui camera will be in pixel units scaled to the game window. Use the project and unproject methods on the camera classes to position your ui elements correctly. Render the world using the world camera then do a ui pass over that with the ui camera."

My way is ideal, but i need help to setup it correctly.

It’s not )
(becouse you scale texture of font Atlas)
wrong stackexchange answer ^^


//
right one

Or use official doc

But if it’s not ideal, then how will i draw health bar at unit scale aswell?

Create 5-10 fonts that you needed

  • they all have same img quality as you create them, if you not try to scale them.

If you have in game zoom and wants to zoom in-out camera with font zoom

you can scale font texture – for 10-20% its ok, but on 200-300% it looks terrible
so you can swap on bigger/smallest Texture font
//
or try use https://github.com/libgdx/libgdx/wiki/Distance-field-fonts

up: