问题描述
如何在我的静态box2d主体顶部绘制文本(字体)?
How can I draw text (font) on top of my static box2d body?
背景:我无法在物体顶部正确绘制精灵.为此,我将box2dSpriteBatch
投影到box2dCamera上,就像box2dSpriteBatch.setProjectionMatrix(box2dCamera.combined);
一样,然后使用主体的getPosition
简单地将精灵渲染到正确的位置.
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.
但是,如果我尝试以相同的方式处理字体,不幸的是,它的缩放比例非常大,如果我尝试将字体缩小,则不会出现.因此,我假设我需要在窗口坐标而不是box2d中绘制文本.
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.
我的尝试:
- show()中的
-
:
windowCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
in show():
windowCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
windowSpriteBatch.setProjectionMatrix(windowCamera.combined);
也在渲染"中渲染字体
whiteFont.setScale(0.7f, 0.7f);sb.begin();whiteFont.draw(sb, "2", body.getPosition().x, (questionLineBody.getPosition().y)*PPM);sb.end();
whiteFont.setScale(0.7f, 0.7f);sb.begin();whiteFont.draw(sb, "2", body.getPosition().x, (questionLineBody.getPosition().y)*PPM);sb.end();
推荐答案
解决方案基本上是通过PPM来*我的x位置.
The solution was to Basically * my x position by PPM.
即.
将这些行添加到渲染"方法中:
Add these lines to the Render method:
windowCamera.position.y = box2dCamera.position.y * PPM;
windowCamera.update();
windowSpriteBatch.setProjectionMatrix(windowCamera.combined);
然后像这样渲染字体:
whiteFont.draw (sb,"2", body.getPosition().x * PPM, body.getPosition().y * PPM);
这篇关于在box2d主体顶部绘制字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!