2011-12-14 12 views
6

Non riesco a capire come ruotare correttamente un font Bitmap. Penso che tu modifichi la matrice di trasformazione dello SpriteBatch. Tuttavia, provando a ruotare che ruota il testo attorno ad un punto, e non so come ruotarlo rispetto al testo stesso.Disegna un BitmapFont ruotato in libgdx

risposta

4

si può provare il seguente codice:

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

Si dovrebbe approfondire il significato del vettore quando si fa setToRotation – chairbender

+1

non ha funzionato per me, quello che mi manca qui? –

5

È possibile creare un glifo in una sprite. In questo modo, puoi manipolare il tuo testo come uno sprite.

codice Esempio:

Nota, questo restituirà uno Sprite di un singolo glifo. (Eg char 'A' si trasforma in uno 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; 
} 
+0

Non rispetta la larghezza e l'altezza del carattere, ad esempio se si prende un carattere "", non lo mostra in alto, se si imposta ch come spazio "", non rispetta la larghezza dello spazio . – LeSam

0

vorrei solo aggiungere .. suppongo immagine di base di carattere che hai dentro qualche atlante .. quindi è necessario aggiungere gli originali TextureRegion sot gliph src come è proprio rispetto a quella data regione Texture così

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

La prima risposta da Lunatikul non ha funzionato nel mio caso 2D. Taglia il mio testo a solo mezza lettera. Ero successo con il seguente:

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