2014-09-30 66 views
2

我刚刚开始使用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。

回答

0

我检查过了,两个代码片段之间的唯一区别是globalPositionglobalPower在第一个片段中被提前拉出。

正如我所看到的那样,这只会给您带来一些可能性。

  1. 通过时间draw被调用时,在e.localPosition和/或e.localForce的值已经改变。 我的的唯一方法可能会看到这是一种可能性,如果您使用的库是多线程的。我不熟悉box2dlibgdx,所以我不能说这个问题。

  2. 您对应用程序的其他部分进行了更改,但您未在此处复制/粘贴导致发生故障。

真的,据我所知,这些是唯一的选择。