libgdx box2d, texture/body 以一定角度创建时位置不正确

libgdx box2d, texture/body not in correct place when created at an angle

当我创建一个主体并在该主体所在的位置绘制一个纹理时,如果角度设置为 0.0f,它会准确地出现在预期的位置。直立并位于播放器的 x 和 y 中心(黑色圆圈)。当这个角度改变时,纹理的 x 和 y 似乎完全偏离了。事实上,它们往往不在屏幕上,因为我有时只能在它们随重力下落时看到它们。

这是我创建新项目符号的方法:

private void shoot(final float delta) {
    gameTime += delta;
    final float timeSinceLastShot = gameTime - lastBulletShotTime;
    //5 bullets a second, kerpow
    if (timeSinceLastShot > 0.2f) {
        lastBulletShotTime = gameTime;
        shot.play();

        final float shotX = player.getX() + ((player.getWidth() / 2) - (bulletTexture.getWidth() / 2));
        final float shotY = player.getY() + ((player.getHeight() / 2) - (bulletTexture.getHeight() / 2));

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(shotX, shotY);

        Body body = world.createBody(bodyDef);
        float angle = player.getRotation() * MathUtils.degreesToRadians;
        body.setTransform(body.getPosition().x, body.getPosition().y, angle);
        System.out.println("angle rad: " + angle);
        bullets.add(body);

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(bulletTexture.getWidth() / 2, bulletTexture.getHeight() / 2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 1f;
        Fixture fixture = body.createFixture(fixtureDef);

        shape.dispose();
    }
}

这是我循环绘制项目符号的渲染方法的一部分:

for (Body bullet : bullets) {
            final float x = bullet.getPosition().x;
            final float y = bullet.getPosition().y;
            final float originX = x + (bulletTexture.getWidth() / 2);
            final float originY = y + (bulletTexture.getHeight() / 2);
            final float width = bulletTexture.getWidth();
            final float height = bulletTexture.getHeight();
            final float scaleX = 1.0f;
            final float scaleY = 1.0f;
            final float rotation = bullet.getAngle() * MathUtils.radiansToDegrees;
            final int sourceX = 0;
            final int sourceY = 0;
            final int sourceWidth = bulletTexture.getTextureData().getWidth();
            final int sourceHeight = bulletTexture.getTextureData().getHeight();
            final boolean flipX = false;
            final boolean flipY = false;

            batch.draw(bulletTexture,
                    x, y,
                    originX, originY,
                    width, height,
                    scaleX, scaleY,
                    rotation,
                    sourceX, sourceY,
                    sourceWidth, sourceHeight,
                    flipX, flipY);
        }

有什么关于我做错的提示吗?我希望子弹始终从 Player Circle 的中心开始,而且角度正确。然后我打算增加一些速度,这样他们 'shoot'.

完整代码可以在 Github https://github.com/SamRuffleColes/PlayerCircle/blob/master/core/src/es/rufflecol/sam/playercircle/PlayerCircle.java

上找到

我还附上了一张截图,在拍摄和拍摄之间,一切都随着重力下降了一点,但左下角的子弹最初出现在正确的位置,而其他的都从屏幕上掉下来了(其他一些我 'shot' 从未见过)。

我的错误在于 originX 和 originY。更正为以下内容,现在有效:

final float originX = bulletTexture.getWidth() / 2;
final float originY = bulletTexture.getHeight() / 2;