简体   繁体   English

纹理不在Box2d主体上绘制

[英]Textures not drawing over Box2d Body

I'm trying to overlay a texture on a Box2d object I've created in my game world. 我试图在我的游戏世界中创建的Box2d对象上叠加纹理。 But the coordinates of the textures are just wrong. 但是纹理的坐标是错误的。 The x and y-axis of the textures are very far from the actual objects location in the world. 纹理的x轴和y轴与实际对象在世界上的位置相距甚远。

This is the line of code responsible for drawing the texture: 这是负责绘制纹理的代码行:

batch.draw(khumbtexture, bodyKhumb.getPosition().x ,bodyKhumb.getPosition().y );

The result is the texture is offset by a vector of (150,150). 结果是纹理偏移了(150,150)向量。 How do I fix this? 我该如何解决?

Box2D uses meters for it's coordinate system. Box2D使用仪表作为其坐标系。 Your batch might be operating in screen coordinates or however you defined its projection matrix, which may cause differences when trying to draw at a Box2D coordinate. 您的批处理可能在屏幕坐标下运行,或者您定义了其投影矩阵,这可能会导致在尝试绘制Box2D坐标时产生差异。 Can you post some code on how you set up your SpriteBatch? 您可以张贴一些代码来设置SpriteBatch吗?

Here is one way doing it. 这是一种方法。 1. Setup a Camera 2. Set your SpriteBatch to use the Camera to draw instead of its own internal one 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);

Then in your render method 然后在您的渲染方法中

camera.update();
batch.setProjectionMatrix(camera.combined);
//clear screen here
//draw your stuff in Box2D meter coordinates
batch.draw( texture,1,2); 

Reference for first part: http://www.java2s.com/Open-Source/Android/Game/libgdx/com/badlogic/gdx/tests/box2d/Box2DTest.java.htm 第一部分参考: 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