libgdx box2d,当以一定角度创建时纹理/主体不在正确的位置

时间:2016-01-27 21:30:37

标签: java libgdx box2d

当我创建一个Body并在该主体所在的位置绘制一个纹理时,如果该角度设置为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);
        }

我做错了什么提示?我希望子弹始终从玩家圈的中心开始,但也要以正确的角度开始。然后我打算增加一些速度,以便他们“拍摄”。

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

我还附上了截图,拍摄和拍摄之间的重力都有所下降,但左下方的子弹最初出现在正确的位置,而其他所有的子弹都从屏幕上掉下来了(我拍摄的其他一些人从未见过。

screenshot

1 个答案:

答案 0 :(得分:1)

我的错误在于originX和originY。更正如下,它现在有效:

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