C# 单游戏/XNA缩放和原点

C# 单游戏/XNA缩放和原点,c#,xna,monogame,C#,Xna,Monogame,我希望能够缩放我的雪碧,并将它们保持在缩放前的相同位置。我使用精灵的中心作为原点参数,因为我希望能够旋转精灵 我确信这个解决方案将是微不足道的,但我找不到一个合适的/通用的解决方案来解决这个问题 如果不是很清楚,我制作了一些图片来展示我的代码、代码的结果以及我想要实现的目标 1-这是我的代码和结果,这是我缩放精灵时得到的结果,您可以看到精灵已缩放,但它已“移动”: 如注释所示,以下代码为: Vector2 scale = Vector2.One; float rotation

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

我确信这个解决方案将是微不足道的,但我找不到一个合适的/通用的解决方案来解决这个问题

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

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-这就是我想要实现的,我知道我可以通过移动原点来实现这一点,但我想让它保持在精灵的中心,这样我就可以围绕这一点进行旋转:

好吧,你肯定是在保持角色的位置。字面上你需要的是移动你的角色,使他的腿始终保持在地面上

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

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

一些额外提示:

  • 为所有精灵创建一个类是一个好主意,同时,为所有输入(input)创建的类也是一个好主意

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


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

谢谢大家

欢迎来到Stackoverflow。你可以将你的代码粘贴到问题的主体中吗?这样可以更容易地阅读你的问题,而不必在页面之间移动。如果你链接到的页面被删除,我们仍然会将你的问题中的代码提供给未来的读者。