2
\$\begingroup\$

I created a little jump'n'run game. The character(Mario) is always the center of the viewport. The game is running correctly if I use the resolution 800x480 pixel. But when I use another resolution(for example 1280x768), the sprites in the viewport look completely different to the sprites in the 800x480 viewport. I want that the viewport(and the sprites in the viewport) looks always the same, on every single resolution.

How can I do that? Should I change something on my camera matrix parameters or what should I change so that the viewport and the proportions of the sprites look always the same?

I use Monogame to run the game on different Windows Phone devices.

My camera class:

 public class Camera 
{ 
    public Vector2 Cameraposition; 

    public Camera(Vector2 cameraposition) 
    { 
        Cameraposition = cameraposition; 
    } 

    public void Update(GameTime gameTime, Vector2 CamPosition) 
    { 
        Cameraposition = CamPosition; 
    } 

    public Matrix GetMatrix() 
    { 
        return new Matrix(1, 0, 0, 0, 0, 1, 0, 0, Cameraposition.X, Cameraposition.Y, 1, 0, -Cameraposition.X, -Cameraposition.Y, 0, 1); 
    } 
}

In Game1:

//I use the camera in Game1 like this:
//At the beginning(in LoadContent): 
Vector2 cameraposition = new Vector2(PlayerStartpos.X - GraphicsDevice.Viewport.Width / 2, PlayerStartpos.Y - GraphicsDevice.Viewport.Height / 2); 
camera = new Camera(cameraposition); 

//Updating the camera position
 protected override void Update(GameTime gameTime) 
    { 
        player.Update(gameTime); 
        Newcameraposition = new Vector2(player.Playerposition.X - GraphicsDevice.Viewport.Width / 2, player.Playerposition.Y - GraphicsDevice.Viewport.Height / 2); 
        camera.Update(gameTime, Newcameraposition); 

        base.Update(gameTime); 
    } 

//Drawing: 
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, null, camera.GetMatrix()); 
 ...
\$\endgroup\$
5
  • \$\begingroup\$ Your camera class has to take into account the aspect (width / height) of the screen. \$\endgroup\$
    – NPS
    Commented Apr 19, 2014 at 17:49
  • \$\begingroup\$ How can I do that? \$\endgroup\$ Commented Apr 19, 2014 at 19:03
  • \$\begingroup\$ Instead of creating the matrix manually, try using the helper functions such as Matrix.CreateTranslation(positionX, positionY, 0)); \$\endgroup\$
    – Fletcher D
    Commented Apr 19, 2014 at 19:37
  • \$\begingroup\$ @Fletcher D That doesn't help me because the GraphicsDevice.Viewport.Width and GraphicsDevice.Viewport.Height are not considered. Is it possible to change the camera matrix so that it fits for every single resolution? \$\endgroup\$ Commented Apr 19, 2014 at 20:05
  • \$\begingroup\$ @Homer_Simpson I don't think Fletcher means to use that code specifically. You can multiply matrices together to combine their operations. For example: you could combine a translation matrix (as per Fletcher's example) and a scaling matrix (CreateScale) that considers the viewport size. Order is important. This will make your code far clearer and easier to work with, than creating a matrix element-by-element. \$\endgroup\$ Commented Apr 20, 2014 at 10:23

1 Answer 1

1
\$\begingroup\$

Your question is not an exact duplicate, so I won't try to close it. But it's close enough that I feel comfortable in simply sending you over to my old answer on Supporting Multiple Resolutions.

That answer is specifically for the XNA Platformer Sample, so you should probably download a copy of that to play around with to get a feel for how the code in my answer works. Then you can apply it to your own game.


Note, in particular, how I use matrix multiplication to combine two matrix operations, as mentioned in comments on your question:

Matrix camera = Matrix.CreateScale(scale, scale, 1)
        * Matrix.CreateTranslation(translateX, translateY, 0);
\$\endgroup\$
4
  • \$\begingroup\$ Is "Matrix.CreateTranslation(positionX, positionY, 0));" the same as "new Matrix(1, 0, 0, 0, 0, 1, 0, 0, Cameraposition.X, Cameraposition.Y, 1, 0, -Cameraposition.X, -Cameraposition.Y, 0, 1);"? Because I get a different result if I move my camera with "Matrix.CreateTranslation(positionX, positionY, 0));". \$\endgroup\$ Commented Apr 21, 2014 at 11:33
  • \$\begingroup\$ No, they are not the same. However your custom matrix does not appear to be performing any kind of sensible transformation operation. Which is another good reason to construct your transformations from the Create* methods. A translation matrix is: (1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, X, Y, Z, 1). \$\endgroup\$ Commented Apr 21, 2014 at 12:13
  • \$\begingroup\$ I added the scaling, but now the input isn't working correctly. \$\endgroup\$ Commented Apr 22, 2014 at 20:15
  • \$\begingroup\$ @Homer_Simpson That's a separate problem - please create a separate question for it (add links between the two, if you like). I've reverted your question to its original state. Please don't keep tacking additional questions onto a question that's already been answered. \$\endgroup\$ Commented Apr 23, 2014 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .