2D Matrix Transform (with player and ground)

I have a simple game I'm trying to make for educational purposes, but Matrices are a little more complex, especially in DirectX.

I currently have a filesystem that displays fragments on screen and the position of a character in the center (at startup)

When the player moves to the left, the ground must move to the right so that he is always in the center of the screen. I'm using Sprite to transform the Tile and Player, but I'm having problems because the ground is moving and the player is standing on the ground still in the center.

This is a 2D downward play, so I only need to transform the position (and possibly rotation)

The My Camera class has this method:

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

      

I also tried (for the camera):

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

      

then when I create a ground sprite, I set it to convert to a camera, but the Player has its own Matrix that I use when it moves.

Just like above, but with its position, rotation and such ...

What am I missing and what do I need?

I only have a tile that matches the width / height of the screen, so I should be able to see when it moves, but it is stationary in the center while the ground and the player are 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 have to say that being the # 1 language in game programming, there are very few tutorials that explain simple things like "TopDown Camera in 2D" ...

+3


source to share


2 answers


With the help of people chatting, I figured out what the problem is with my system, although I tried similar things but probably not ok / ok

For a simple 2D TopDown camera with simple positioning:

Camera2D Translation:

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

      

For each object on the camera screen:



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

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

      

and then to display the sprite:

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

      

although this method requires me to move the camera in the opposite direction that the player is moving, so if the player moves +100, the camera must move -100 to still be in the same position relative to the player.

0


source


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

Object Space Every object must be modeled, or in your case drawn, to be in object space. This means that for each object, their matrix, which defines the center, for example. your player.

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



Camera This describes the current view in your game world. It is assumed that the camera is in the middle of the screen and you move the whole world to match the current point of view (for example, translation with a negative position).

In your case, this means that you need a matrix for each object that describes the offset that the image is correctly centered in position (object space). Then you need to convert the object to world space. So you need a matrix with the position and orientation of the object and multiply it by the object matrix. Finally, you have to take the point of view and multiply the operator to make the object transform from world to view space. The resulting matrix is ​​the one you are using for rendering.

Hope that helps :)

+2


source







All Articles