I'm trying to overlay a texture on a Box2d object I've created in my game world. 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.
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). How do I fix this?
preguntado el 10 de marzo de 12 a las 07:03
Box2D uses meters for it's coordinate system. 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. Can you post some code on how you set up your 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
// 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