Deukalion
Deukalion

Reputation: 2665

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?

Only the player moves here and the world transforms around him.

Example

I must say that being the number #1 language in game programming there's very little guides that explains simple things as "TopDown Camera in 2D"...

Upvotes: 3

Views: 2490

Answers (2)

Deukalion
Deukalion

Reputation: 2665

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(&ObjectWorld, 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 player.

Upvotes: 0

Gnietschow
Gnietschow

Reputation: 3180

For your problem you should read something about the different spaces: object space, world space and camera space. (Mostly referred in relation to a 3D-space, but I'll try to explain it for 2D)

Object space Each object should be modelled or in your case drawn to be in objectspace. This means that for each object their is a matrix, which determine the center, e.g. of your player.

World space This is your game world. It describes the position and orientation of each object in your scene.

Camera space This describes the current view at your game world. The camera is assumed to be fix at the middle of your screen and you move the whole world to fit to the current viewpoint (e.g. translating with the negative position).

For your case it means, that you need a matrix for each object, which describes the offset that the image is correctly centered at the position (objectspace). Then you need to transform the object into the world space. Therefore you need a matrix with the position and orientation of the object and multiply it with the objectmatrix. Finally you have to take the viewpoint into account and multiply the cameramatrix, so that the object are transformed from worldspace to viewspace. The resulting matrix is the one you use for rendering.

Hope that helps :)

Upvotes: 2

Related Questions