java - 创建 Box2d LibGDX 后我的 body 有点下降

标签 java libgdx box2d game-development

我正在创建一个打砖 block 游戏来练习。我有一个用于游戏 Sprite ( bat 、砖 block 、球)的抽象父类 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;
    }
}

我的 bat :

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 工作正常,但球在创建 body 后掉落了 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)

最佳答案

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

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

关于java - 创建 Box2d LibGDX 后我的 body 有点下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205748/

相关文章:

JavaMail POP3 over SSL 连接失败;

java - libGDX 如何将用户点击与对象位置匹配?

c++ - Box2D:让 body 随机下降

c++ - box2d:在 ResetMassData 上获取 SIGABRT,b2Assert(m_I > 0.0f) - 中心惯性、质心

java - 由 : java. sql.SQLException : Connection is not associated with a managed connection. org.jboss.resource.ada 引起

java - 使用 Java 将文件从 Linux 复制到 NAS Share (Apache FileUtils FileCopy)

java - 如何在 Java 中提取特定于语言环境格式的日期字段?

java - 将 JRuby 与 libGDX 桌面可运行 JAR 结合使用

java - libGDX、Scene2d 按钮状态

javascript - Box2D 一个夹具用于多个物体