Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java libGDX-box2d绘制框太大&;太小_Java_Android_Libgdx_Box2d - Fatal编程技术网

Java libGDX-box2d绘制框太大&;太小

Java libGDX-box2d绘制框太大&;太小,java,android,libgdx,box2d,Java,Android,Libgdx,Box2d,所以,我只想绘制与精灵大小相同的box2debugrenderer。 我使用了相同的SpriteBatchsameCamera和相同的Viewport。我花了8个小时,试图在谷歌周围找到一个解决方案,但仍然没有解决这个问题 这是我得到的: 我只是换了一行: PolygonShape shape = new PolygonShape(); shape.setAsBox(32, 32); 一些教程说要除以2。我试着得到第二张照片的结果 这是我的剧本 PlayScreen.java: public

所以,我只想绘制与精灵大小相同的
box2debugrenderer
。 我使用了相同的
SpriteBatch
same
Camera
和相同的
Viewport
。我花了8个小时,试图在谷歌周围找到一个解决方案,但仍然没有解决这个问题

这是我得到的:

我只是换了一行:

PolygonShape shape = new PolygonShape();
shape.setAsBox(32, 32);
一些教程说要除以2。我试着得到第二张照片的结果

这是我的剧本

PlayScreen.java

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
,从Sprite类扩展而来:

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);
    }
}
public class Player扩展了Sprite{
私人动画;
专用浮点状态计时器;
私人世界;
私人机构;
//调试
公共玩家(世界){
stateTimer=0;
这个世界=世界;
loadAnim();
}
私有void loadAnim(){
阵列温度=新阵列(40);
纹理=新纹理(“sprite.png”);
临时添加(新纹理区域(纹理,3*32,3*32,32,32));
临时添加(新纹理区域(纹理,2*32,3*32,32,32));
临时添加(新纹理区域(纹理,32,3*32,32,32));
临时添加(新纹理区域(纹理,0,3*32,32,32));
动画=新动画(0.1f,temp,Animation.PlayMode.LOOP);
BodyDef BodyDef=新的BodyDef();
车身定义位置设置(100100);
bodyDef.type=bodyDef.BodyType.DynamicBody;
body=world.createBody(bodyDef);
多边形形状=新的多边形形状();
形状设置框(32,32);
FixtureDef fixture=新的FixtureDef();
fixture.shape=形状;
创建夹具(夹具);
立根(0,0,32,32);
设定位置(100100);
}
公共无效更新(浮动增量){
stateTimer+=delta;
setRegion(getKeyFrame());
设置大小(32,32);
}
公共纹理区域getKeyFrame(){
返回anim.getKeyFrame(stateTimer,true);
}
}

你能解释一下到底发生了什么吗?

Box2d和纹理有不同的起源

身体的起源是它的中心

纹理的原点位于左下角

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

伪代码:

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

否则,您可以将box2d主体的原点设置为“左下角”,但如果您遵循其他教程,则可能会给您带来麻烦。

您也可以在绘制精灵之前,通过调用精灵的函数setOriginCenter()将精灵的原点设置为中心


因此,spritebatch将从中心绘制它们,就像您的长方体一样

您还需要缩小世界的比例,因为box2d世界以米为单位,纹理以像素为单位绘制