简体   繁体   English

libgdx box2d,倾斜创建时纹理/主体不在正确的位置

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

When I create a Body and draw a Texture where that body is, if the angle is set to 0.0f, it appears exactly where expected. 当我创建一个Body并在该Body所在的位置绘制纹理时,如果将角度设置为0.0f,它将恰好出现在预期的位置。 Upright and at the centre x and y of the Player (the black circle). 垂直放置在播放器的x和y中心(黑色圆圈)。 When this angle changes, the x and y of the texture appear to be completely off. 当此角度改变时,纹理的x和y似乎完全偏离。 In fact they tend to be off the screen as I can only see them visibly sometimes as they fall with the gravity. 实际上,它们倾向于不显示在屏幕上,因为我有时只能看到它们在重力作用下掉落。

Here's the method where I create a new bullet: 这是我创建新项目符号的方法:

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();
    }
}

And here's the part of the render method where I loop and draw the bullets: 这是我循环绘制项目符号的render方法的一部分:

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);
        }

Any tips for what I'm doing wrong? 关于我做错事情的任何提示吗? I want the bullets to always start at the centre of the Player Circle, but also at the correct angle. 我希望子弹总是从玩家圈的中心开始,而且也要以正确的角度开始。 I then intend to add some velocity so they 'shoot'. 然后,我打算增加一些速度,以便它们“射击”。

The full code can be found here on Github https://github.com/SamRuffleColes/PlayerCircle/blob/master/core/src/es/rufflecol/sam/playercircle/PlayerCircle.java 完整的代码可以在Github上找到https://github.com/SamRuffleColes/PlayerCircle/blob/master/core/src/es/rufflecol/sam/playercircle/PlayerCircle.java

I've also attached a screenshot, everything has fallen a little with the gravity between shooting and taking this, but the bottom left bullets appeared in the right place initially, while all the others have fallen from off the screen (some others which I 'shot' were never visible). 我还附上了屏幕截图,拍摄和拍摄之间的重力使一切都有些下降,但是最初左下的子弹最初出现在正确的位置,而其他所有子弹都从屏幕上掉落了(有些是我镜头”从不可见)。

截图

My mistake was with the originX and originY. 我的错误是与originX和originY有关。 Corrected to the below, it now works: 已更正为以下内容,现在可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM