Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted

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();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.