【问题标题】:MonoGame/XNA Scaling and OriginMonoGame/XNA 缩放和起源
【发布时间】:2016-08-21 12:48:01
【问题描述】:

我希望能够缩放我的精灵并将它们保持在与缩放之前相同的位置。我使用精灵的中心作为原点参数,因为我也希望能够旋转我的精灵。

我确定解决方案将是微不足道的,但我找不到解决此问题的适当/通用解决方案。

如果不是很清楚我做了一些图片来展示我的代码,这段代码的结果以及我想要实现的目标。

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

【问题讨论】:

  • 欢迎来到 Stackoverflow 。您能否将代码粘贴到问题的正文中,这样可以更轻松地阅读您的问题而无需在页面之间移动,并且如果您链接到的页面被删除,我们仍然为未来的读者提供您问题中的代码.

标签: c# xna monogame


【解决方案1】:

有人帮助我并找到了我的问题的解决方案 (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);
}

谢谢大家!

【讨论】:

    【解决方案2】:

    嗯,你肯定会保持你的性格。字面上地。您需要真正移动您的角色,以便他的腿始终保持在地面上。

    如果这是你的全部代码,那么这意味着你还没有物理学。在这种情况下,您必须将角色的位置向上/向下移动其屏幕高度的一半(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.Y += .05f; 的唯一原因是让您看到它可以更快地完成,并且您可以使您的代码更具可读性。

    另外,由于 XNA 中的垂直 (Y) 轴是倒置的,要让角色向上移动,您必须从他的位置中减去,反之亦然。

    一些额外的提示:

    • 所有精灵的类是个好主意,而且所有输入(输入)的类也很棒

    • 不要在 4 个地方写 .05f,而是在这些地方创建一个变量,并将其值设置为 .05f

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2013-05-06
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2014-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多