# Why is the player not resting in the center of the camera?

Is it possible to create a combination of the following two cameras described in Andrew Russell's answer? XNA Platformer Sample: Supporting multiple resolutions?

I want to implement a scrolling camera that is focused on the player. In addition, I want to scale the viewport, so that the game looks on every resolution the same.

I already tried to combine the two cameras, but the player is not resting in the center of the screen. Currently scaling the viewport to any resolution higher than 800×480 de-centers the character. The character moves out of the viewport after a while.

My code:

Vector2 vp, gameWorldSize = new Vector2(800, 480);
Matrix camera;
float ScaleX, ScaleY, Scale;
float TranslateX, TranslateY;

ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

TranslateX = (vp.X - (gameWorldSize.X * Scale)) / 2f - player.Playerposition.X + gameWorldSize.X / 2;
TranslateY = (vp.Y - (gameWorldSize.Y * Scale)) / 2f - player.Playerposition.Y + gameWorldSize.Y / 2;

camera = Matrix.CreateScale(Scale, Scale, 1) * Matrix.CreateTranslation(TranslateX, TranslateY, 0);


What is wrong? Is it not possible to combine the two cameras?

## 1 Answer

I think should do the trick:

ScaleX = vp.X / gameWorldSize.X;
ScaleY = vp.Y / gameWorldSize.Y;
Scale = Math.Min(ScaleX, ScaleY);

camera = Matrix.CreateTranslation(-Player.Position)
* Matrix.CreateScale(Scale, Scale, 1)
* Matrix.CreateTranslation( ViewPort.Width/2, Viewport.Height/2, 0);

• Thanx. It works perfectly. May 3, 2014 at 8:20