Libgdx text rotation

I want to turn BitmapFont into libgdx. There are topics on this subject. Draw BitmapFont rotated to libgdx However, the first solution I am trying to cut is changing the text, all the text is not displayed (diagonal cut). And he also rotates it 90 degrees when he says that it should be 180. I just don’t understand.

the code:

public void show() { sr = new ShapeRenderer(); w = Gdx.graphics.getWidth(); h = Gdx.graphics.getHeight(); textRotation = new Matrix4(); textRotation.setToRotation( new Vector3(200,200, 0), 180); rectThickness = h / 120; c1 = Color.WHITE; c2 = Color.WHITE; f = new BitmapFont(Gdx.files.internal("fonts/MyFont.fnt"), Gdx.files.internal("data/MyFont.png"), true); f.setScale(h/500); sb = new SpriteBatch(); } private void renderPlayerScores() { //callend in render sb.setTransformMatrix(textRotation); f.setColor(players[0].getColor()); sb.begin(); f.draw(sb, "Player 1: "+ Integer.toString(scores[0]), 100, 110); sb.end(); } 
+4
source share
1 answer

One way to do this is to use scene2d tags. They are very easy to work with, and you can do what you want if you read the Scene2D user interface and Shortcuts . However, some people prefer not to use Scene2D. The following code does what you want, but this is a bit of extra work to avoid using Scene2D.

 public SpriteBatch spriteBatch; private int posX = 100; private int posY = 100; private float angle = 45; private String text = "Hello, World!"; private BitmapFont font; private Matrix4 oldTransformMatrix; Matrix4 mx4Font = new Matrix4(); @Override public void show() { font = new BitmapFont(Gdx.files.internal("someFont.ttf")); oldTransformMatrix = spriteBatch.getTransformMatrix().cpy(); mx4Font.rotate(new Vector3(0, 0, 1), angle); mx4Font.trn(posX, posY, 0); } @Override public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); spriteBatch.setTransformMatrix(mx4Font); spriteBatch.begin(); font.draw(spriteBatch, text, 0, 0); spriteBatch.end(); spriteBatch.setTransformMatrix(oldTransformMatrix); } 

I hope some of them may come in handy.

+2
source

All Articles