【问题标题】:XNA - About the relation between world space and the screen spaceXNA - 关于世界空间和屏幕空间的关系
【发布时间】:2010-08-21 20:21:36
【问题描述】:

编辑:只是想让我的问题更清楚。我几乎很难看到像 Matrix.CreateTransformationZ 这样的东西在矩阵乘法的上下文中是如何工作的,更重要的是这对屏幕空间/世界空间有什么影响,所以我可以获得更清晰的画面。因此,也许有人可以更改代码或给我一个简短的 sn-p 来测试我可以在哪里使用它来绕轴旋转和/或绕轴旋转。我也改了例子。

所以我仍然很难想象矩阵如何与 xna 屏幕空间一起工作。

我给你举个例子:

public class Game1 : Microsoft.Xna.Framework.Game
{
    Texture2D shipTexture, rockTexture;


    Vector2 shipPosition = new Vector2(100.0f, 100.0f);
    Vector2 rockPosition = new Vector2(100.0f, 29.0f);

    int count;

    float shipRotation, rockRotation;
    float rockSpeed, rockRotationSpeed;
    bool move = true;

    const int rock = 0;
    const int ship = 1;

    Color[] rockColor;
    Color[] shipColor;

    float testRot = 0.0f;
    Vector2 shipCenter; int shipWidth, shipHeight;
    Vector2 rockCenter; int rockWidth, rockHeight;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    #region maincontent
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        rockSpeed = 0.16f;
        rockRotationSpeed = 0.3f;
        base.Initialize();
    }



    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        shipTexture = Content.Load<Texture2D>("Images\\ship");
        rockTexture = Content.Load<Texture2D>("Images\\asteroid");

        rockWidth = rockTexture.Width; rockHeight = rockTexture.Height;
        shipWidth = shipTexture.Width; shipHeight = shipTexture.Height;

        rockCenter = new Vector2(rockWidth / 2, rockHeight / 2);
        shipCenter = new Vector2(shipWidth / 2, shipHeight / 2);



        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        rockColor = new Color[rockTexture.Width * rockTexture.Height];
        rockTexture.GetData(rockColor);
        shipColor = new Color[shipTexture.Width * shipTexture.Height];
        shipTexture.GetData(shipColor);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

            /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

        spriteBatch.Draw(rockTexture, rockPosition,
            null, Color.White, testRot, rockCenter, 1.0f,
            SpriteEffects.None, 0.0f);

        spriteBatch.Draw(shipTexture, shipPosition,
            null, Color.White, shipRotation, shipCenter,
            1.0f, SpriteEffects.None, 0.0f);

        spriteBatch.End();
        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
    #endregion

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        testRot += 0.034906585f;
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        UpdateAsteroid(gameTime);
        RotateShip(gameTime);
        MoveShip(gameTime);
        // TODO: Add your update logic here
        CheckCollisions();
        base.Update(gameTime);
    }

    #region Collisions

    public Color PixelColor(int objectNum, int pixelNum)
    {
        switch (objectNum)
        {
            case rock:
                return rockColor[pixelNum];
            case ship:
                return shipColor[pixelNum];
        }

        return Color.White;
    }

    public bool PixelCollision(Matrix transformA, int pixelWidthA, int pixelHeightA, int A,
        Matrix transformB, int pixelWidthB, int pixelHeightB, int B)
    {
        Matrix temp = Matrix.Invert(transformB);
        Matrix AtoB = transformA * Matrix.Invert(transformB);

        Vector2 columnStep, rowStep, rowStartPosition;

        columnStep = Vector2.TransformNormal(Vector2.UnitX, AtoB);
        rowStep = Vector2.TransformNormal(Vector2.UnitY, AtoB);

        rowStartPosition = Vector2.Transform(Vector2.Zero, AtoB);

        for (int rowA = 0; rowA < pixelHeightA; rowA++)
        {
            // begin at the left
            Vector2 pixelPositionA = rowStartPosition;

            // for each column in the row (move left to right)
            for (int colA = 0; colA < pixelWidthA; colA++)
            {
                // get the pixel position
                int X = (int)Math.Round(pixelPositionA.X);
                int Y = (int)Math.Round(pixelPositionA.Y);

                // if the pixel is within the bounds of B
                if (X >= 0 && X < pixelWidthB && Y >= 0 && Y < pixelHeightB)
                {

                    // get colors of overlapping pixels
                    Color colorA = PixelColor(A, colA + rowA * pixelWidthA);
                    Color colorB = PixelColor(B, X + Y * pixelWidthB);

                    // if both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                        return true; // collision
                }
                // move to the next pixel in the row of A
                pixelPositionA += columnStep;
            }

            // move to the next row of A
            rowStartPosition += rowStep;
        }

        return false; // no collision
    }
    public Matrix Transform(Vector2 center, float rotation, Vector2 position)
    {

        return Matrix.CreateTranslation(new Vector3(-center, 0.0f)) *
            Matrix.CreateRotationZ(rotation) *
            Matrix.CreateTranslation(new Vector3(position, 0.0f));
    }

    public static Rectangle TransformRectangle(Matrix transform, int width, int height)
    {
        Vector2 leftTop = new Vector2(0.0f, 0.0f);
        Vector2 rightTop = new Vector2(width, 0.0f);
        Vector2 leftBottom = new Vector2(0.0f, height);
        Vector2 rightBottom = new Vector2(width, height);

        Vector2.Transform(ref leftTop, ref transform, out leftTop);
        Vector2.Transform(ref rightTop, ref transform, out rightTop);
        Vector2.Transform(ref leftBottom, ref transform, out leftBottom);
        Vector2.Transform(ref rightBottom, ref transform, out rightBottom);

        Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop), Vector2.Min(leftBottom, rightBottom));
        Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop), Vector2.Max(leftBottom, rightBottom));

        return new Rectangle((int)min.X, (int)min.Y,
            (int)(max.X - min.X), (int)(max.Y - min.Y));
    }

    private void CheckCollisions()
    {
        Matrix shipTransform, rockTransform;

        Rectangle shipRectangle, rockRectangle;

        rockTransform = Transform(rockCenter, rockRotation, rockPosition);
        rockRectangle = TransformRectangle(rockTransform, rockWidth, rockHeight);
        shipTransform = Transform(shipCenter, shipRotation, shipPosition);
        shipRectangle = TransformRectangle(shipTransform, shipWidth, shipHeight);

        if (rockRectangle.Intersects(shipRectangle)) // rough collision check
            if (PixelCollision( // exact collision check
            rockTransform, rockWidth, rockHeight, rock,
            shipTransform, shipWidth, shipHeight, ship))
                move = false;
    }
    #endregion

    #region Moves_and_Rotations

    private void UpdateAsteroid(GameTime gameTime)
    {
        float timeLapse = (float)gameTime.ElapsedGameTime.Milliseconds;

        if (move == true)
        {
            if ((rockWidth + rockPosition.X >= Window.ClientBounds.Width))
            {
                rockSpeed *= -1.0f;
                rockPosition.X += rockSpeed * timeLapse;
            }
            else if ((rockPosition.X <= 0))
            {
                rockSpeed *= -1.0f;
                rockPosition.X += rockSpeed * timeLapse;

            }
            else
                rockPosition.X += rockSpeed * timeLapse;

            const float SCALE = 50.0f;
            rockRotation += rockRotationSpeed * timeLapse / SCALE;

            rockRotation = rockRotation % (MathHelper.Pi * 2.0f);
        }
    }

    private float RotateShip(GameTime gameTime)
    {
        float rotation = 0.0f;
        float speed = gameTime.ElapsedGameTime.Milliseconds / 300.0f;

        if (!move)
            return rotation;

        KeyboardState keyboard = Keyboard.GetState();

        if (keyboard.IsKeyDown(Keys.Right))
            rotation = speed;
        else if (keyboard.IsKeyDown(Keys.Left))
            rotation = -speed;

        shipRotation += rotation;

        shipRotation = shipRotation % (MathHelper.Pi * 2.0f);
        return shipRotation;
    }

    private void MoveShip(GameTime gameTime)
    {
        const float SCALE = 20.0f;
        float speed = gameTime.ElapsedGameTime.Milliseconds / 100.0f;

        KeyboardState keyboard = Keyboard.GetState();

        if (keyboard.IsKeyDown(Keys.Up))
        {

            shipPosition.X += (float)Math.Sin(shipRotation) * speed * SCALE;
            shipPosition.Y -= (float)Math.Cos(shipRotation) * speed * SCALE;
        }
        else if (keyboard.IsKeyDown(Keys.Down))
        {
            shipPosition.X -= (float)Math.Sin(shipRotation) * speed * SCALE;
            shipPosition.Y += (float)Math.Cos(shipRotation) * speed * SCALE;
        }
    }
#endregion
}

这是我从 XNA Game Creators 那里得到的,它只是一种进行像素检测的方法。

  1. 在上面的变换方法中,矩阵乘法发生在我猜一个矩形上。屏幕空间/世界空间到底发生了什么?

  2. 为什么作者将矩阵乘以另一个矩阵的逆矩阵? (他提到,不知何故,这使得它相对于其他资产)

【问题讨论】:

    标签: xna linear-algebra


    【解决方案1】:

    屏幕空间大概与客户端空间相同。客户端空间从左上角的 (0,0) 到右下角的 (width, height)。 “向上”是 Y-。

    投影空间从左下角的 (-1,-1) 到右上角的 (1,1)。这就是 GPU 用于其最终渲染的内容。 SpriteBatch 为您处理此问题(相比之下:BasicEffect 要求您提供投影矩阵)。

    世界空间是你想要的任何东西。这是你的游戏发生的坐标系。在你的例子中,这似乎是相同客户空间。

    传统上,在执行此类操作时,您需要在自己的空间中定义一个对象。在您的示例中,岩石和船舶矩形被硬编码到函数TransformRectangle 中,作为变量topLeftbottomRight 的初始值。

    然后,每个对象都有一个世界矩阵。这会将对象从它自己的空间移动到它在世界空间中的位置。在您的示例中,这是shipTransformrockTransform。世界变换SpriteBatch.Draw 内完成,基于您传入的参数(使用纹理本身作为初始对象)。

    然后您就有了一个视图矩阵 - 您可以将其视为您的相机。您的示例没有其中之一。但是,例如,如果您想要平移视图以跟随玩家,您可以在此处使用从玩家位置创建的平移矩阵(并将其传递给SpriteBatch.Begin)。

    最后,您有一个投影矩阵,可将您的世界空间转换为投影空间,以便 GPU 可以渲染您的场景。

    现在一个可能的问题是 SpriteBatch 内部定义了一个投影矩阵,它将客户空间转换为投影空间(因此它基本上“假设”世界空间客户空间)。在您的示例中这不是问题,因为两个空格 相同。

    如果您的 World 空间与 Client 空间不同,并且您想使用 SpriteBatch,则必须创建一个额外的矩阵来从 World 空间转换为 Client 空间并将其插入到 View 之间和项目矩阵(即:将其与 View 相乘并将其传递给 SpriteBatch.Begin)。

    如果您的 World 空间定义了与 SpriteBatch 不同的“向上”(或“正确”)方式,那么您必须记住 SpriteBatch.Draw 使用的原始对象将“向上”定义为 Y -.

    【讨论】:

    • 你和史蒂夫的回答对我帮助很大,认为现在工作正常。
    【解决方案2】:

    我不认为是空间关系导致了您所看到的(在您的问题的第一版中)。矩阵在它所在的空间方面是灵巧的。如果你给它提供屏幕空间值,它会返回屏幕空间值。所以关系(屏幕/世界)不相关也不存在。

    例如,如果您想使用矩阵围绕 2d 屏幕的中心点环绕您的飞船:

    Vector2 screenCenter = new Vec2(width/2, h/2);// abbreviated
    Vector2 shipPosition = screenCenter;
    shipPosition.X += 25;//offset ship from center slightly
    
    shipPosition = Vector2.Transform(shipPosition, Matrix.CreateTranslation(-screenCenter));
    shipPosition = Vector2.Transform(shipPosition , Matrix.CreateRotationZ(someRadians));
    shipPosition = Vector2.Transform(shipPosition , Matrix.CreateTranslation(screenCenter));
    
    
    //although the above would typically be written as:
    shipPosition = Vector2.Transform(shipPosition - screenCenter, Matrix.CreateRotationZ(someAngle)) + screenCenter;
    

    请注意,所有值都只是屏幕空间值。世界/屏幕空间关系无关紧要。这就是如何使用矩阵围绕二维屏幕空间中的另一个点旋转一个点。对于 3d,它将是精确的代码,但带有 Z 分量(vector3)并使用 3d 世界空间。

    您的 comboMatrix(来自早期代码)和您的新代码 sn-p 中的 transform() 可能会绊倒您。将矩阵相乘时,就像将一个旋转添加到另一个旋转。所以你的 comboMatrix 就像 3 + 5 +(-3) ......你真正做的只是相当于 5。你的 comboMatrix 所做的只是 rotZ 的等价物......它没有翻译。和你的 Transform() 是相似的。当我将三个矩阵应用到上面的 shipPosition 时,我确保每个矩阵都应用到 shipPosition 以进行下一个操作。有时您可以在应用之前连接矩阵,但在您的情况下,不是。

    这有帮助还是我仍然错过了您的问题?

    【讨论】:

    • 2.) 哦,关于倒置矩阵。如果您将 Matrix 视为表示方向,假设 MatrixA 表示 90 度左方向, MatrixB 表示 130 度左方向。 A 和 B 之间的差异是 45 度左方向。现在 Matrix.Invert() 反转矩阵,因此 MatrixA 的逆是 90 度 Right 旋转。所以 MatrixB(左边 135)* InvA(右边 90)导致左边 45。因此,乘以另一个的倒数会产生差异。可以相对于第一个来考虑差异。
    • 这主要帮助了我。那么,如果我想不围绕屏幕空间中的任意点旋转 2d 火箭飞船,而是围绕世界空间和/或精灵的局部空间中的 z 轴旋转呢?使用 CreateRotation 矩阵还简单吗?
    • 另一件事。你说“comboMatrix 就像 3 + 5 +(-3) ......你真正做的只是相当于 5。你的 comboMatrix 所做的只是 rotZ 的等价物......它没有翻译。你的 Transform() 是类似...”另外,你的意思是当我之前做一个旋转-z时,因为我给它提供屏幕坐标,我基本上是围绕屏幕坐标原点旋转?好的,但是在我的程序中,要么没有发生旋转(意味着图像位置从未改变),要么发生了一些奇怪的旋转度数我猜屏幕坐标原点发生了。
    • 要在精灵的局部空间中围绕 z 轴旋转精灵,在 SpriteBatch.Draw() 中简单地将旋转浮点数设置为 0 和两个 Pi 之间的值仍然更容易。这更容易而不是使用矩阵进行精灵旋转。
    • >>好的,但是在我的程序中,要么没有旋转...改变它在屏幕原点(左上角)周围的位置。有翻译方面让我觉得你真的试图旋转精灵,但翻译部分相互抵消
    【解决方案3】:

    在 TestMatrix() 中:

    shipPosition = Vector2.Transform(shipPosition, rotZ);

    应该是

    shipPosition = Vector2.Transform(shipPosition, comboMatrix);

    【讨论】:

      【解决方案4】:

      概念:-平移、旋转、+平移。是一种使某物在原地旋转或自转的机制。 但是您将它应用于一个点(向量2)。在适当的位置进行点旋转几乎没有用。我相信你真正想要的是船精灵在原地旋转。这通常通过更改 shipRotation 变量来完成,该变量是一个浮点数,描述了您希望精灵旋转的角度差异量(从 0 开始)。

      由于某种原因,您将点(shipPosition)的旋转与船对象的旋转混淆了......

      在 2d 中,尽管矩阵中的数学运算与 3d 中的一样好,但 spritebatch.Draw() 方法使用单个浮点数来描述旋转,并且与 Matrix 生成的旋转数据没有直接关系。

      有趣的是,在 2d 中使用矩阵来转换点很好,但很多人不明白这一点。在这里,您试图理解它,但真的希望它作用于一个对象而不是一个点。所以,不要放弃矩阵。但只需在 Draw 调用中更改旋转值即可将精灵旋转到位。

      现在,如果我没有理解您的目标,请告诉我,我会尽力提供进一步的帮助。

      【讨论】:

      • 好吧,但或多或​​少,我想知道我在做矩阵乘法时看到的屏幕空间和世界空间之间的关系。
      猜你喜欢
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2012-12-28
      • 1970-01-01
      • 2013-02-17
      相关资源
      最近更新 更多