Draw a BitmapFont rotated in libgdx

Rahat Ahmed picture Rahat Ahmed · Dec 14, 2011 · Viewed 8k times · Source

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.

Answer

Gerrit Paul van Dijk picture Gerrit Paul van Dijk · Mar 28, 2013

You can create a glyph into a sprite. That way , you can manipulate your text as a sprite.

Example code:

Note, this will return a Sprite of a single glyph. (E.g. char 'A' is transformed into 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;
}