繁体   English   中英

纹理不在Box2d主体上绘制

[英]Textures not drawing over Box2d Body

我试图在我的游戏世界中创建的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); 

第一部分参考: http : //www.java2s.com/Open-Source/Android/Game/libgdx/com/badlogic/gdx/tests/box2d/Box2DTest.java.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM