Problem with position for player and 2D camera

I'm not sure I understand how the Matrix class for a 2D camera works in XNA. I have a Player object that I'm trying to position in the middle of the screen and then I have the Camera object that I want to follow the Player. But The position of the player is at the top left corner!?

Preciate som help to understand and to improve my code.

Game1.cs

player = new Player(texturePlayer, new Vector2(screenSizeX / 2, screenSizeY / 2));


And in the Update method:

camera.Position = player.PlayerPosition;


The Camera class:

    class Camera
{
public Vector2 Position { get; set; }
private Viewport viewPort;

// Konstruktor
public Camera(Viewport viewPort)
{
this.viewPort = viewPort;
Position = Vector2.Zero;
}
public Matrix GetViewPortMatrix()
{
return Matrix.CreateTranslation(new Vector3(-Position, 0f));
}
}


I guess there is no need to calcualte an origin for the Matrix when the camera isn't going to rotate!?

And finally some part of the Player class. Note that I'm using a "timer" to achieve a movement of the player in steps. Is this OK or is there a better way to achieve this?

    class Player:GameObjects
{
//public Vector2 Position { get { return position; } }

private int walkingSpeed = 60;
private float walkingTimer = 100;
private float milliSeconds1;

// Konstruktor
public Player(Texture2D texture, Vector2 position) : base(texture, position)
{
}

public void Update(GameTime gameTime)
{
milliSeconds1 += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
}

public Vector2 PlayerPosition
{
get
{
return position;
}
set
{
position = value;
}
}

public void Up()
{
if (milliSeconds1 >= walkingTimer)
{
if(position.Y > 60)
position.Y -= walkingSpeed;

milliSeconds1 = 0;
}
}

public void Down()
{
if (milliSeconds1 >= walkingTimer)
{
if(position.Y < 630)
position.Y += walkingSpeed;

milliSeconds1 = 0;
}
}


1 Answer

Since you're not setting an origin in your camera class, the origin ends up being the top left corner of the screen instead of the center of the screen.

Without changing anything else, the easiest solution would be to change the assignment to:

camera.Position = player.PlayerPosition - new Vector2(screenSizeX/2f, screenSizeY/2f);


Otherwise, add the origin translation to the camera class, so that you don't need to compensate outside.

• OK Thanks! It's working! I hade the origin from the beginning, but that camera wasn't in the middle! Does the origin affect the position? I have question about the movement of the player, it's "flickering" when I move it, is there a way to get rid of this? Thanks! – 3D-kreativ Aug 5 '12 at 14:16
• Yes, the origin transformation affects all the transformations placed before it, which in this case includes the translation. About the flickering, is it related to the camera? If it is, the only thing I can think of is that perhaps you're assigning the camera position before updating the player, instead of doing the camera update at the end. – David Gouveia Aug 5 '12 at 17:03
• The flickering is gone thanks to your suggestion! Thanks! :) – 3D-kreativ Aug 5 '12 at 17:24