libGDX Box2DDebugRenderer Draw Box Too Big&太小

时间:2015-10-12 12:31:26

标签: java android libgdx box2d

所以,我只想绘制与精灵大小相同的Box2DDebugRenderer。 我使用了相同SpriteBatchCameraViewport相同的PolygonShape shape = new PolygonShape(); shape.setAsBox(32, 32); 。我花了8个小时,尝试在谷歌附近寻找解决方案,但仍未解决问题。

这是我得到的: 32x32 16x16

我只是改变了这一行:

PlayScreen.java

有些教程对div说2.我试过我得到的结果就像第二张图片。

这是我的剧本

public class PlayScreen implements Screen { private SpriteBatch batch; private TiledMap tileMap; private TiledMapRenderer tiledMapRenderer; private OrthographicCamera cam; private Player player; private World world; private Box2DDebugRenderer debugRenderer; private FitViewport gamePort; public PlayScreen(HookaHookaGame game) { this.batch = game.getSpriteBatch(); this.world = new World(new Vector2(0, -20), true); // Create cam cam = new OrthographicCamera(); gamePort = new FitViewport(800, 600, cam); gamePort.apply(true); //initially set our gamcam to be centered correctly at the start of of map // cam.position.set(400, 300, 0); // cam.update(); // Load tilemap tileMap = new TmxMapLoader().load("simulation01.tmx"); tiledMapRenderer = new OrthogonalTiledMapRenderer(tileMap, batch); // Create box2d debug renderer debugRenderer = new Box2DDebugRenderer(); // Create player sprite player = new Player(this.world); } @Override public void show() { } @Override public void render(float delta) { //cam.update(); // Update sprite player.update(delta); //Clear the game screen with Black Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Draw tilemap tiledMapRenderer.setView(cam); tiledMapRenderer.render(); // Set camera to spritebatch batch.setProjectionMatrix(cam.combined); // Draw sprite batch.begin(); batch.draw(player.getKeyFrame(), 300, 300); player.draw(batch); batch.end(); // Draw box2d debug renderer debugRenderer.render(world, cam.combined); } @Override public void resize(int width, int height) { gamePort.update(width, height); } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } }

Player.java

public class Player extends Sprite { private Animation anim; private float stateTimer; private World world; private Body body; // Debug public Player(World world) { stateTimer = 0; this.world = world; loadAnim(); } private void loadAnim() { Array<TextureRegion> temp = new Array<TextureRegion>(40); Texture texture = new Texture("sprite.png"); temp.add(new TextureRegion(texture, 3*32, 3*32, 32, 32)); temp.add(new TextureRegion(texture, 2*32, 3*32, 32, 32)); temp.add(new TextureRegion(texture, 32, 3 * 32, 32, 32)); temp.add(new TextureRegion(texture, 0, 3*32, 32, 32)); anim = new Animation(0.1f, temp, Animation.PlayMode.LOOP); BodyDef bodyDef = new BodyDef(); bodyDef.position.set(100, 100); bodyDef.type = BodyDef.BodyType.DynamicBody; body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.setAsBox(32, 32); FixtureDef fixture = new FixtureDef(); fixture.shape = shape; body.createFixture(fixture); setBounds(0, 0, 32, 32); setPosition(100, 100); } public void update(float delta) { stateTimer += delta; setRegion(getKeyFrame()); setSize(32, 32); } public TextureRegion getKeyFrame() { return anim.getKeyFrame(stateTimer, true); } } ,从Sprite Class扩展:

for

你能解释一下究竟发生了什么吗?

2 个答案:

答案 0 :(得分:2)

Box2d和Textures有不同的起源。

身体的起源是它的中心。

纹理的原点是左下角。

enter image description here

如您所见,如果您在同一位置绘制它们,则box2d对象的中心正好位于纹理的左下角。

伪代码:

batch.draw(texture, body.x - texture.width / 2, body.y - texture.heigth / 2);

否则,您可以将box2d正文的原点设置为“左下角”,但如果您按照其他教程进行操作可能会给您带来麻烦。

答案 1 :(得分:0)

你也可以在绘制精灵的函数 setOriginCenter()之前将精灵的原点放在中心

所以spritebatch会像你的盒子一样从中心绘制它们