Draw BitmapFont rotated in libgdx

I can’t figure out how to rotate the font of the bitmap correctly. I think you are modifying the SpriteBatch transformation matrix. 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.

+6
source share
4 answers

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(); 
+4
source

You can create a glyph in a sprite. This way you can manipulate the text as a sprite.

Code example:

Note that this will return a sprite of one character. (For example, char 'A' is converted to a sprite.)

 /** Creates a sprite from a glyph. * * @param ch * @return Sprite */ public Sprite getGlyphSprite (char ch) { Glyph glyph = Globals.g.font.getData().getGlyph(ch); Sprite s = new Sprite(Globals.g.font.getRegion().getTexture(), glyph.srcX,glyph.srcY,glyph.width, glyph.height); s.flip(false, true); s.setOrigin(glyph.width/2, glyph.height/2); return s; } 
+5
source

I would just add .. Suppose you have a font image inside some kind of atlas .. so you need to add the originals TextureRegion sot gliph src, since this is just relative to the specified Texture area, so

 BitmapFont font = ... BitmapFont.Glyph glyph = font.getData().getGlyph(ch); int srcX = glyph.srcX + font.getRegion().getRegionX(); int srcY = glyph.srcY+ font.getRegion().getRegionY(); Sprite s = new Sprite(font.getRegion().getTexture(), srcX,srcY,glyph.width, glyph.height); 
0
source

The first answer from Lunatikul did not work in my 2D case. This cuts my text by only half a letter. I was successful with the following:

 batch.begin(); batch.setTransformMatrix(new Matrix4().setToRotation(0,0,1,<insert angle here>)); font.draw(batch, "Hallo Welt", 100, 100); batch.end(); 
0
source

Source: https://habr.com/ru/post/903789/


All Articles