创建Box2d LibGDX后,我的身体有些掉落

时间:2018-07-06 08:07:12

标签: java libgdx box2d game-development

我正在创建一个打砖块游戏进行练习。我有一个用于游戏精灵(蝙蝠,砖头,球)的抽象父类GameObject:

public abstract class GameObject extends Sprite {
    protected World world;
    protected Body body;

    public GameObject(Texture texture, float width, float height, Vector2 centerPosition, World world) {
        super(texture);
        this.world = world;

        setOrigin(width / 2, height / 2);
        setSize(width, height);
        setCenter(centerPosition.x, centerPosition.y);

        defineBody();
    }

    public void render(float delta) {
        setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
        draw(batch);
    }

    protected abstract void defineBody();

    public Body getBody() {
        return body;
    }
}

我的蝙蝠:

public class Bat extends GameObject {

    public Bat(World world) {
        super(batTexture, BAT_WIDTH, BAT_HEIGHT, new Vector2(WORLD_WIDTH / 2, 5 + BAT_HEIGHT / 2), world);
    }

    public void moveLeft(float delta) {
        body.getPosition().set(-BAT_MOVEMENT_SPEED * delta, 0);
        checkForBoundaries();
    }

    public void moveRight(float delta) {
        translateX(BAT_MOVEMENT_SPEED * delta);
        checkForBoundaries();
    }

    private void checkForBoundaries() {
        if (body.getPosition().x - getWidth() / 2 < 0) setX(getWidth() / 2);
        else if (body.getPosition().x + getWidth() / 2 > WORLD_WIDTH) setX(WORLD_WIDTH - getWidth() / 2);
    }

    @Override
    protected void defineBody() {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);

        body = world.createBody(bodyDef);

        FixtureDef fixtureDef = new FixtureDef();
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(getWidth() / 2, getHeight() / 2);
        fixtureDef.shape = shape;
        fixtureDef.friction = 0;
        fixtureDef.density = 700;
        fixtureDef.restitution = 1;

        body.createFixture(fixtureDef);
    }
}

我的球:

public class Ball extends GameObject {

    public Ball(World world) {
        super(ballTexture, BALL_RADIUS * 2, BALL_RADIUS * 2, new Vector2(WORLD_WIDTH / 2, BALL_Y), world);
    }

    @Override
    protected void defineBody() {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(getX() + getWidth() / 2, getOriginY() + getHeight() / 2);

        System.out.println("X: " + (getX() + getWidth() / 2) + " Y: " + (getY() + getHeight() / 2));

        body = world.createBody(bodyDef);

        System.out.println(bodyDef.position);
        System.out.println(body.getPosition());

        FixtureDef fixtureDef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(BALL_RADIUS);
        fixtureDef.shape = shape;
        fixtureDef.friction = 0;
        fixtureDef.density = 700;
        fixtureDef.restitution = 1;

        body.createFixture(fixtureDef);
    }
}

Bat可以正常工作,但是在创建物体后Ball掉落了30个单位:

Before world.createBody() bodyDef.position: X: 200.0 Y: 50.0
After world.createBody() bodyDef.position: (200.0,20.0)
After world.createBody() body.getPosition(): (200.0,20.0)

我的世界的引力为0。 如果我将球从50个单位上移到100个单位,则类似地,球正下降30个单位:

Before world.createBody() bodyDef.position: X: 200.0 Y: 100.0
After world.createBody() bodyDef.position: (200.0,70.0)
After world.createBody() body.getPosition(): (200.0,70.0)

1 个答案:

答案 0 :(得分:1)

 bodyDef.position.set(getX() + getWidth() / 2, getOriginY() + getHeight() / 2);

您正在使用y原点位置而不是y位置。只需将getOriginY()更改为getY()。