# 2D Matrix Transformation (with a Player and Ground)

I have a simple game that I'm trying to do for learning purposes, but Matrices are a bit hard, especially in DirectX.

I currently have a tilesystem that renders tiles at the screen and a character position at the center (on startup)

When the player move to the left, the ground should move to the right so he's always at the center of the screen. I use a Sprite to transform the Tiles and the player, but having trouble because the ground is moving and the player stands still on the ground still positioned at the center.

This is a topdown 2D game so I only need to transform Position (and perhaps rotation)

My Camera class has this method:

``````D3DXMatrixTransformation2D(&View, NULL, 0, &Scale, &Center, D3DXToRadian(Rotation), &Position);
``````

I've also tried (for the Camera):

``````D3DXMatrixTranslation(&View, Position.x, Position.y, 0);
``````

then when I render the ground sprite I set it to transform to the camera, but the Player has it's own Matrix that I use when he's moving.

Exactly like the one above but with his position, rotation and such...

What am I missing and what do I need?

I only have a tilesystem that fits the width/height of the screen so I should see when he's moving but he's standing still at the center and the ground and the player is moving.

Do I need to invert the matrix so that the ground moves in the opposite direction?

-
I have one matrix for the camera that translates the scene, so it should be centered on the player and another matrix on the player, but when I move my Camera whenever the player moves, the player moves more and the camera is behind although I use the same transformation, Scale, Center, Rotation and Position... Right now I have a Sprite object that renders the "tiles" and I create another Sprite object that renders the player, I set camera transform on the "tiles" and the player matrix on the player sprite. Something is very weird. – Deukalion Feb 2 '13 at 14:54

With the help of people at the chat I realized what the problem was with my system, altough I tried similiar things but probably in the wrong way / order.

For a simple 2D TopDown camera with just positioning:

Camera2D Translation:

``````D3DXMatrixTranslation(&CameraView, Position.x, Position.y, 0);
``````

For each object in the camera's view:

``````D3DXMATRIX ObjectWorld;
D3DXMatrixTransformation2D(&world, NULL, 0, NULL, NULL, 0, &Position);

D3DXMATRIX Translation;
D3DXMatrixMultiply(&Translation, ObjectWorld, CameraView);
``````

and then to render the sprite:

``````sprite->SetTransform(&Translation);
sprite->Draw(Texture, NULL, NULL, NULL, Color);
``````

altough this method requires me to move the camera in the opposite direction that the player is moving, so if player is moving +100, the camera should move -100 to still be positioned at the same location relative to the camera, but it's also possible to Invert the matrix with

``````D3DXMatrixInverse();
``````
-