You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

setCenter()方法无法将精灵纹理对齐Box2D夹具中心的问题

2D平台游戏Box2D精灵与夹具偏移问题排查

我正在用Box2D+正交Tiled地图开发一款简单2D平台游戏,物理系统运行正常——通过Box2D调试渲染器可确认玩家夹具和相机移动均无异常。但加载纹理替换调试图形后,使用setCenter()方法无法将精灵纹理与夹具对齐,始终存在偏移。我尝试过通过纹理宽高取半来调整位置,也调试过世界/相机/屏幕单位坐标,但偏移问题仍未解决。

相关代码

1. Box2D玩家物理定义代码

//define player's physical behaviour
public void definePlayer() {
    //definitions to later use in a body
    BodyDef bdef = new BodyDef();
    bdef.position.set(120 / Constants.PPM, 60 / Constants.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    //Define needed components of the player's main fixture
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(8 / Constants.PPM, 16 / Constants.PPM); //size of the player hitbox
    //set the player's category bit
    fdef.filter.categoryBits = Constants.PLAYER_BIT;
    //set which category bits the player should collide with. If not mentioned here, no collision occurrs
    fdef.filter.maskBits = Constants.GROUND_BIT |
                        Constants.GEM_BIT | 
                        Constants.BRICK_BIT | 
                        Constants.OBJECT_BIT |
                        Constants.ENEMY_BIT |
                        Constants.TREASURE_CHEST_BIT |
                        Constants.ENEMY_HEAD_BIT |
                        Constants.ITEM_BIT;

    fdef.shape = shape;
    b2body.createFixture(fdef).setUserData(this);
}

2. Player构造函数中纹理Region初始化代码

//define in box2d
definePlayer();
//set initial values for the player's location, width and height, initial animation.
setBounds(0, 0, 64 / Constants.PPM, 64 / Constants.PPM);
setRegion(playerStand.getKeyFrame(stateTimer, true));

3. 玩家更新逻辑代码

public void update(float delta) {
    //center position of the sprite on its body 
    // setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2);
    setCenter(b2body.getPosition().x, b2body.getPosition().y);
    
    setRegion(getFrame(delta));
    
    //set all the boolean flags during update cycles approprietly. DO NOT manipulate b2bodies
    //while the simulation happens! therefore, only set flags there, and call the appropriate
    //methods outside the simulation step during update
    checkForPitfall();
    checkIfAttacking();
}

显示效果

使用上述代码后,精灵朝右、朝左时均存在明显偏移;后续按照建议仅保留setCenter(b2body.getPosition().x, b2body.getPosition().y);调用,朝右、朝左时的偏移问题依然存在。

精灵纹理翻转代码

if((b2body.getLinearVelocity().x < 0 || !runningRight) && !region.isFlipX()) {
    region.flip(true, false);
    runningRight = false;
} else if ((b2body.getLinearVelocity().x > 0 || runningRight) && region.isFlipX()) {
    region.flip(true, false);
    runningRight = true;
}

这段翻转代码仅通过判断玩家b2body的X轴速度或runningRight标志位,结合纹理区域的翻转状态调用LibGDX的flip()方法,并未修改夹具坐标,因此我对偏移问题的成因感到困惑。

内容的提问来源于stack exchange,提问作者phoot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:35:13