Sidney
Sidney

Reputation: 65

C# Draw method does not draw to screen after adding transformation matrix to spritebatch

I have been trying my hand at making a simple 2D game in Monogame. I found a tutorial online for a Camera class to be used with scrolling the screen towards where my character is moving.

This is the camera class:

public class Camera
{
    Vector2 position;
    Matrix viewMatrix;

public Matrix ViewMatrix
{
    get { return viewMatrix; }
}

public int ScreenWidth
{
    get { return GraphicsDeviceManager.DefaultBackBufferWidth; }
}

public int ScreenHeight
{
    get { return GraphicsDeviceManager.DefaultBackBufferHeight; }
}

public void Update(Vector2 playerPosition)
{
    position.X = playerPosition.X - (ScreenWidth / 2);
    position.Y = playerPosition.Y - (ScreenHeight / 2);

    if(position.X <0)
    {
        position.X = 0;
    }
    if(position.Y <0)
    {
        position.Y = 0;
    }

    viewMatrix = Matrix.CreateTranslation(new Vector3(-position, 0));
}

And in the Game1.cs I use the following Draw method:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);


    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
    null, null, null, null, Camera.ViewMatrix);

    ScreenManager.Instance.Draw(spriteBatch);
    spriteBatch.End();


    base.Draw(gameTime);

}

When I use spriteBatch.Begin() without any arguments, the Titlescreen and mainmenu load just fine followed by the tilemap and the player. However when I add the Camera.Viewmatrix argument the screen stays Black. It does not seem to draw anything past the Graphicsdevice.Clear(Color.Black).

I would appreciate any and all input. And please let me know if you need to see more of my code.

UPDATE: The camera and matrix are fine I believe. The problem has to do with the fact that I have many classes, for example Screenmanager, Image, Tile, Layer, TitleScreen,... that are all using an Update and most of them a Draw method.

The actual drawing is being handled as shown above in the game1.cs by a singleton instance of the ScreenManager class using its draw method.

I suspect that it is trying to apply the matrix to the fadescreen/titlescreen in the start and that is causing errors such as not progressing past the black background. Perhaps because the camera position has no player position?

I have tried using a separate spritebatch.Begin() for the Camera and Player draw methods but this of course results in the error that I'm calling a new spriteBatch.Begin before the other one has ended.

I feel like this is solvable but I might have been staring at it too long to see the solution now.

I don't want to turn this into a 'look at my entire project and fix it for me' but if anyone recognizes this situation and has input, or even has run into the same thing and fixed it, I would very much appreciate your feedback.

Also, should anyone require more code to look at, again. feel free to let me know.

Upvotes: 2

Views: 120

Answers (1)

user3215326
user3215326

Reputation:

Try feeding it an Identity matrix. That should work and it would confirm that something must be wrong with your matrix. It's been a while since I did this, but it think this worked.

 public void Update(Vector2 playerPosition)
 {
   if(position.X <0)
   {
       position.X = 0;
   }
   if(position.Y <0)
   {
       position.Y = 0;
   }

   viewMatrix = Matrix.CreateTranslation(new Vector3(-playerPosition.X, -playerPosition.Y, 0.0f));
}

Upvotes: 0

Related Questions