XNA ViewPort-Projektion und SpriteBatch

Ich arbeite an einem XNA-Spiel und verwende ViewPort.Project und ViewPort.Unproject zum Übersetzen von und zu Weltkoordinaten. Derzeit verwende ich diese für jedes Objekt, das ich mit SpriteBatch zeichne. Was ich tun möchte, ist, eine Matrix zu berechnen, die ich an SpriteBatch.Begin senden kann, um die Bildschirmraumtransformation für mich durchzuführen.

Hier sind die Funktionen, mit denen ich derzeit in und aus dem Bildschirmbereich übersetze:

        Vector2 ToWorldCoordinates(Vector2 pixels)
    {
        Vector3 worldPosition = graphics.GraphicsDevice.Viewport.Unproject(new Vector3(pixels, 0),
                Projection, View, Matrix.Identity);
        return new Vector2(worldPosition.X, worldPosition.Y);
    }

    Vector2 ToScreenCoordinates(Vector2 worldCoords)
    {
        var screenPositon = graphics.GraphicsDevice.Viewport.Project(new Vector3(worldCoords, 0),
                Projection, View, Matrix.Identity);
        return new Vector2(screenPositon.X, screenPositon.Y);
    }

View ist auf Matrix.Identity eingestellt, und die Projektion ist wie folgt eingestellt:

Projection = Matrix.CreateOrthographic(40 * graphics.GraphicsDevice.Viewport.AspectRatio, 40, 0, 1);

Und so zeichne ich gerade:

            spriteBatch.Begin();
        foreach (var thing in thingsToDraw)
        {
            spriteBatch.Draw(thing.Texture, ToScreenCoordinates(thing.PositionInWorldCoordinates), thing.Color);
            spriteBatch.End();
        }
        spriteBatch.End();

Dies ist, was ich stattdessen tun möchte (unter Verwendung der XNA 4.0-Version von SpriteBatch.Begin ())

            // how do I calculate this matrix?
        Matrix myTransformationMatrix = GetMyTransformationMatrix();

        spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null,
            myTransformationMatrix);

        foreach (var thing in thingsToDraw)
        {
            // note: no longer converting each object's position to screen coordinates
            spriteBatch.Draw(thing.Texture, thing.PositionInWorldCoord,inates, thing.Color);
            spriteBatch.End();
        }
        spriteBatch.End();

Antworten auf die Frage(2)

Ihre Antwort auf die Frage