问题
How can I draw text (font) on top of my static box2d body?
Background: Im able to draw sprites on top of bodies correctly.
I do this by having a box2dSpriteBatch
which is projected on box2dCamera like so box2dSpriteBatch.setProjectionMatrix(box2dCamera.combined);
And then simply rendering the sprite in the correct spot using getPosition
of the body.
But if i try the same way for fonts, unfortunately it scales incredibly large, and if i try to scale the font down it doesn't appear. So im assuming I need to draw the text in window coordinates instead of box2d.
My attempt:
in show():
windowCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
in my Render method i call
windowSpriteBatch.setProjectionMatrix(windowCamera.combined);
also in Render i render the font
whiteFont.setScale(0.7f, 0.7f); sb.begin(); whiteFont.draw(sb, "2", body.getPosition().x, (questionLineBody.getPosition().y)*PPM); sb.end();
回答1:
The solution was to Basically * my x position by PPM.
ie.
Add these lines to the Render method:
windowCamera.position.y = box2dCamera.position.y * PPM;
windowCamera.update();
windowSpriteBatch.setProjectionMatrix(windowCamera.combined);
And Render the font like so:
whiteFont.draw (sb,"2", body.getPosition().x * PPM, body.getPosition().y * PPM);
来源:https://stackoverflow.com/questions/27258453/draw-font-on-top-of-box2d-body