我刚刚开始使用LibGDX和Box2d开发我的新Android游戏。 我现在有一个船类,其中包含对船体和附属于该船的引擎阵列的引用。 为了调试目的,我想提请发动机的位置和他们的部队向量峰(为一个红色方块) 首先我想这个代码LibGDX Box2d getWorldPoint问题
public void draw(Batch batch, float parentAlpha)
{
for(AttachedEngine e : Engines)
{
Vector2 globalPosition = body.getWorldPoint(e.localPosition);
Vector2 globalPower = body.getWorldPoint(e.localForce);
batch.draw(img, Gdx.graphics.getWidth()/2 + globalPosition.x, Gdx.graphics.getHeight()/2 + globalPosition.y, 5, 5);
batch.draw(img, Gdx.graphics.getWidth()/2 + globalPower.x, Gdx.graphics.getHeight()/2 + globalPower.y, 5, 5);
}
}
但它会导致我在广场错误的地方是(不工作) 然后我试图将此代码
public void draw(Batch batch, float parentAlpha)
{
for(AttachedEngine e : Engines)
{
batch.draw(img, Gdx.graphics.getWidth()/2 + body.getWorldPoint(e.localPosition).x, Gdx.graphics.getHeight()/2 + body.getWorldPoint(e.localPosition).y, 5, 5);
batch.draw(img, Gdx.graphics.getWidth()/2 + body.getWorldPoint(e.localForce).x, Gdx.graphics.getHeight()/2 + body.getWorldPoint(e.localForce).y, 5, 5);
}
}
和它的工作。 你们可以解释一下为什么这些代码有所不同? 第一种解决方案对我来说更自然,但我不知道它为什么不起作用。 答案显然是微不足道的,但我太困惑了。 这是因为几年编程的,我其实真的不知道发生了什么事情在第一时间,我找不到计算器解决方案,所以我要问你们
编辑
它工作在这样:
public void draw(Batch batch, float parentAlpha)
{
for(AttachedEngine e : Engines)
{
Vector2 globalPosition = body.getWorldPoint(e.localPosition);
batch.draw(img, Gdx.graphics.getWidth()/2 + globalPosition.x, Gdx.graphics.getHeight()/2 + globalPosition.y, 5, 5);
Vector2 globalPower = body.getWorldPoint(e.localForce);
batch.draw(img, Gdx.graphics.getWidth()/2 + globalPower.x, Gdx.graphics.getHeight()/2 + globalPower.y, 5, 5);
}
}
还我发现,当我初始化globalPower只是globalPostion后(如在代码的第一部分)这样
Vector2 globalPosition = body.getWorldPoint(e.localPosition);
Vector2 globalPower = body.getWorldPoint(e.localForce);
两个vect2都包含全局坐标(它们是相同的),所以我必须在它们之间插入draw。