簡體   English   中英

MonoGame/XNA 縮放和起源

[英]MonoGame/XNA Scaling and Origin

我希望能夠縮放我的精靈並將它們保持在與縮放之前相同的位置。 我使用精靈的中心作為原點參數,因為我也希望能夠旋轉我的精靈。

我確信該解決方案將是微不足道的,但我找不到解決此問題的適當/通用解決方案。

如果不是很清楚,我做了一些圖片來顯示我的代碼,這段代碼的結果以及我想要實現的目標。

1 - 這是我的代碼,這是我縮放精靈時得到的結果,正如您所看到的精靈被縮放但它“移動”了:

正如這里的評論中所建議的那樣,代碼是:

    Vector2 scale = Vector2.One;
    float rotation = 0;

    public void Update(GameTime gameTime)
    {
        if (Input.IsKeyPressed(Keys.P))
            scale += new Vector2(0.05f, 0.0f);
        if (Input.IsKeyPressed(Keys.M))
            scale -= new Vector2(0.05f, 0.0f);

        if (Input.IsKeyPressed(Keys.O))
            scale += new Vector2(0.0f, 0.05f);
        if (Input.IsKeyPressed(Keys.L))
            scale -= new Vector2(0.0f, 0.05f);

        if (Input.IsKeyPressed(Keys.D))
            rotation += MathHelper.ToRadians(5);
        if (Input.IsKeyPressed(Keys.Q))
            rotation -= MathHelper.ToRadians(5);

        Input.Update(gameTime);
    }

    public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, Matrix.CreateScale(4));

        spriteBatch.Draw(Sprites.BACKGROUND, Vector2.Zero, new Rectangle(0, 0, 128, 128), Color.White);

        Vector2 marioPosition = new Vector2(64, 112 - 32);
        Rectangle source = new Rectangle(0,0,16,32);

        Vector2 origin = source.Size.ToVector2() / 2;

        spriteBatch.Draw(Sprites.MARIO, marioPosition + origin, source, Color.White, rotation, origin, scale, SpriteEffects.None, 0f);

        spriteBatch.End();
    }

2 - 這就是我想要實現的,我知道我可以通過移動我的原點來做到這一點,但我想把它保持在精靈的中心,這樣我就可以圍繞這個點應用旋轉: http : //pasteboard.co/ rzMfc0p.png

有人幫助我並找到了解決我的問題的方法(Pema99 @pemathedev),正如其他人所建議的那樣,解決方案確實是移動精靈,這里是移動精靈需要多少:

public void Update(GameTime gameTime)
{
    if (Input.IsKeyPressed(Keys.P))
        scale.X += .05f;
    if (Input.IsKeyPressed(Keys.M))
        scale.X -= .05f;

    //Solution ---------------------------------------------------
    if (Input.IsKeyPressed(Keys.O))
    {
        float previousSize = source.Height * scale.Y;
        float newSize = source.Height * (scale.Y + .05f);
        scale.Y += .05f;
        marioPosition.Y -= (Math.Abs(previousSize - newSize)/2)
    }
    if (Input.IsKeyPressed(Keys.L))
    {
        float previousSize = source.Height * scale.Y;
        float newSize = source.Height * (scale.Y - .05f);
        scale.Y -= .05f;
        marioPosition.Y += (Math.Abs(previousSize - newSize)/2)
    }
    //--------------------------------------------------------------

    if (Input.IsKeyPressed(Keys.D))
        rotation += MathHelper.ToRadians(5);
    if (Input.IsKeyPressed(Keys.Q))
        rotation -= MathHelper.ToRadians(5);

    Input.Update(gameTime);
}

謝謝大家 !

嗯,你肯定會保持你的性格。 字面上地。 您需要的是實際移動您的角色,以使他的腿始終留在地面上。

如果這是你的全部代碼,那么這意味着你沒有物理(還)。 在這種情況下,您必須將角色的位置向上/向下移動屏幕高度的一半(image.height * scale),並且僅當他的垂直比例發生變化時才這樣做。 一旦你把物理放在適當的位置,這將無法正常工作,但就目前而言,這是未經測試但建議的代碼。

public void Update(GameTime gameTime)
{
    if (Input.IsKeyPressed(Keys.P))
        scale.X += .05f;
    if (Input.IsKeyPressed(Keys.M))
        scale.X -= .05f;

    if (Input.IsKeyPressed(Keys.O))
    {
        scale.Y += .05f;
        marioPosition.Y -= scale.Y * (Sprites.MARIO.Height / 2f);
    }
    if (Input.IsKeyPressed(Keys.L))
    {
        scale.Y -= .05f;
        marioPosition.Y += scale.Y * (Sprites.MARIO.Height / 2f);
    }

    if (Input.IsKeyPressed(Keys.D))
        rotation += MathHelper.ToRadians(5);
    if (Input.IsKeyPressed(Keys.Q))
        rotation -= MathHelper.ToRadians(5);

    Input.Update(gameTime);
}

我改變scale += new Vector2(0.0f, 0.05f);的唯一原因scale += new Vector2(0.0f, 0.05f); scale.Y += .05f; 是為了讓您看到它可以更快地完成,並且可以使您的代碼更具可讀性。

此外,由於 XNA 中的垂直 (Y) 軸倒置,要使角色向上移動,您必須從他的位置中減去,反之亦然。

一些額外的提示:

  • 適用於所有精靈的類是一個好主意,而且適用於所有輸入 (Input) 的類也很棒

  • 不要在 4 個地方寫 .05f,而是在這些地方創建一個變量,並將其值設置為 .05f

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM