在 XNA/Monogame 中旋转然后平移矩阵时抖动

问题描述

我在 Monogame/XNA 中遇到了问题,我将位置/旋转/比例存储在矩阵中,以便更轻松地通过育儿操作这些值。一切似乎都很顺利,除了当我旋转矩阵时,对象似乎在四处抖动和摇晃。相关代码如下:

        // Executes every frame
        internal void Draw()
        {
            float RadRot = MathHelper.Toradians(Rotation);

            // Sets world matrix for things that draw
            if (Parent != null) {
                WorldMatrix = Matrix.CreateScale(Size.X,Size.Y,1) * -Matrix.CreateTranslation(Pivot.X,Pivot.Y,0) * Matrix.CreateRotationZ(RadRot);
                WorldMatrix.Translation = Vector3.Zero; // Rotating seems to create a strange positioning issue,this fixes it
                WorldMatrix *= Matrix.CreateTranslation(RelativePosition.X,RelativePosition.Y,0) * Matrix.CreateRotationZ(MathHelper.Toradians(Parent.Rotation)) * <FIX JITTER> * Matrix.CreateTranslation(Parent.DecomposedPosition.X,Parent.DecomposedPosition.Y,0);
            } else
            {
                // For objects without parents
                WorldMatrix = Matrix.CreateScale(RelativeSize.X,RelativeSize.Y,0) * Matrix.CreateRotationZ(RadRot);
                WorldMatrix.Translation = Vector3.Zero; // This fixes the jitter issue,but in the code above (in the if statement),I can't do this when translating the child around the parent
                WorldMatrix *= Matrix.CreateTranslation(RelativePosition.X,0);
            }

            Vector3 TempPos = Vector3.Zero;
            Quaternion TempRot = Quaternion.Identity;
            Vector3 TempSize = Vector3.Zero;
            WorldMatrix.Decompose(out TempSize,out TempRot,out TempPos);

            DecomposedPosition = new Vector2(-TempPos.X,-TempPos.Y);
            DecomposedRotation = MathHelper.Toradians(Rotation); // Can't convert quaternions,but this works
            DecomposedSize = new Vector2(MathF.Round(TempSize.X),MathF.Round(TempSize.Y));

            foreach (Component2D comp in Components)
            {
                comp.Draw();
            }
        }

这是 comp.Draw 调用的:

MyGame.SpriteDrawing.Draw(Image,new Rectangle(Linkedobject.DecomposedPosition.ToPoint(),Linkedobject.DecomposedSize.ToPoint()),RenderRegion,Color,Linkedobject.DecomposedRotation,Linkedobject.Pivot,SpriteEffects.None,Linkedobject.Layer + (SubLayer / 10000));

这是它的实际效果

Jitter

额外注意:无论对象的大小如何,都会发生这种抖动

解决方法

尝试以不同的方式组织场景:

  • 每个对象都可以存储它的比例 (Vector3)、旋转 (Quaternion) 和位置 (Vector3)
  • 每个对象都应该有一个结果矩阵的属性(在局部空间中),这将是这样的乘法,按以下顺序:Matrix.CreateScale(scale) * Matrix.CreateRotationQuaterion(rotation) * Matrix.CreateTranslation(position) )
  • 现在,当你有了上面的局部空间矩阵时,用它来创建 WorldMatrix,它是父世界矩阵和这个对象局部空间矩阵的乘法,或者只有局部空间矩阵 - 当没有父存在时。
  • 尽量只将每个元素的世界矩阵传递给你的绘图代码,避免分解
  • 如果你有一个很大的层次结构,你可以在不改变的情况下缓存本地矩阵结果
,

看来我只是使用了错误的 SpriteBatch.Draw()。我使用的是 Rectangle 版本,而我所需要的只是使用 Vector2 版本来防止舍入错误。

MyGame.SpriteDrawing.Draw(Image,LinkedObject.DecomposedPosition,RenderRegion,Color,LinkedObject.DecomposedRotation,LinkedObject.Pivot * RenderRegion.Size.ToVector2(),LinkedObject.DecomposedSize / RenderRegion.Size.ToVector2(),SpriteEffects.None,LinkedObject.Layer + (SubLayer / 10000));