Las texturas no se dibujan sobre el cuerpo de Box2d

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

1 Respuestas

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

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

Reference for first part:

respondido 10 mar '12, 09:03

yup... tried that !... ive given up on libgdx now to be honest... just not working for me ! - whythehack

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.