Search code examples
javamatrixrotationlibgdxbitmap-fonts

Draw a BitmapFont rotated in libgdx


I can't seem to figure out how to rotate a Bitmap font correctly. I think you modify the transformation matrix of the SpriteBatch. However, trying to rotate that rotates the text around some point, and I don't know how to rotate it relative to the text itself.


Solution

  • you can try the following code:

    Matrix4 mx4Font = new Matrix4();
    BitmapFont font;
    SpriteBatch spriteFont;
    
    font = new BitmapFont(Gdx.files.internal("data/font/agencyFB.fnt"), Gdx.files.internal("data/font/agencyFB.png"), true); //must be set true to be flipped
    mx4Font.setToRotation(new Vector3(200, 200, 0), 180);
    spriteFont.setTransformMatrix(mx4Font);
    spriteFont.begin();
    font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
    font.draw(spriteFont, "The quick brown fox jumped over the lazy dog", 100, 110);
    spriteFont.end();