簡體   English   中英

libgdx box2d,傾斜創建時紋理/主體不在正確的位置

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

當我創建一個Body並在該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();
    }
}

這是我循環繪制項目符號的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);
        }

關於我做錯事情的任何提示嗎? 我希望子彈總是從玩家圈的中心開始,而且也要以正確的角度開始。 然后,我打算增加一些速度,以便它們“射擊”。

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

我還附上了屏幕截圖,拍攝和拍攝之間的重力使一切都有些下降,但是最初左下的子彈最初出現在正確的位置,而其他所有子彈都從屏幕上掉落了(有些是我鏡頭”從不可見)。

截圖

我的錯誤是與originX和originY有關。 已更正為以下內容,現在可以使用:

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