【发布时间】:2012-03-10 07:36:47
【问题描述】:
我正在尝试在我在游戏世界中创建的 Box2d 对象上叠加纹理。但是纹理的坐标是错误的。纹理的 x 轴和 y 轴与世界上的实际对象位置相距甚远。
这是负责绘制纹理的代码行:
batch.draw(khumbtexture, bodyKhumb.getPosition().x ,bodyKhumb.getPosition().y );
结果是纹理偏移了 (150,150) 的向量。我该如何解决这个问题?
【问题讨论】:
我正在尝试在我在游戏世界中创建的 Box2d 对象上叠加纹理。但是纹理的坐标是错误的。纹理的 x 轴和 y 轴与世界上的实际对象位置相距甚远。
这是负责绘制纹理的代码行:
batch.draw(khumbtexture, bodyKhumb.getPosition().x ,bodyKhumb.getPosition().y );
结果是纹理偏移了 (150,150) 的向量。我该如何解决这个问题?
【问题讨论】:
Box2D 使用米作为它的坐标系。您的批次可能在屏幕坐标中运行,或者您定义了它的投影矩阵,这可能会在尝试在 Box2D 坐标处绘制时导致差异。 您能否发布一些有关如何设置 SpriteBatch 的代码?
这是一种方法。 1.设置相机 2. 设置你的 SpriteBatch 使用相机而不是它自己的内部绘图
// setup the camera. In Box2D we operate on a
// meter scale, pixels won't do it. So we use
// an orthographic camera with a viewport of
// 48 meters in width and 32 meters in height.
// We also position the camera so that it
// looks at (0,16) (that's where the middle of the
// screen will be located).
camera = new OrthographicCamera(48, 32);
camera.position.set(0, 15, 0);
然后在你的渲染方法中
camera.update();
batch.setProjectionMatrix(camera.combined);
//clear screen here
//draw your stuff in Box2D meter coordinates
batch.draw( texture,1,2);
【讨论】: