1
\$\begingroup\$

i am currently trying to dive into 2D programming with DirectX, especially bulding a tile based sidescroller. I have worked with some chapters from a book (Advanced 2D Game Development) but always tried to create something on my own (to get the most out of it - because you never will learn anything if you are not doing it yourself). But now i am kinda stuck with the camera work in 2D. I do realize that there different matrices to tranform the objects to world space, then the world space to view space and then to projection (camera) space. Do i really need all of those for a 2D game?

I have a camera class (out of the book) and was trying to use it to "roam" around with the camera (i am currently drawing a grid, with D3DXLine - bigger than the screen - and i would like to float over it with my camera):

Camera::Camera()
{
    p_position = D3DXVECTOR3(0.0f, 0.0f, 10.0f);
    p_upDir = D3DXVECTOR3(0.0f, 1.0f, 0.0f);

    float aspectRatio = (float)g_engine->getWindowWidth() / (float)g_engine->getWindowHeight();

    this->setPerspective(3.14159f / 4, aspectRatio, 1.0f, 2000.0f);
}

Camera::~Camera() { }

void Camera::setPerspective(float fov, float aspectRatio, float nearPlane, float farPlane)
{
    this->setFOV(fov);
    this->setAspectRatio(aspectRatio);
    this->setNearPlane(nearPlane);
    this->setFarPlane(farPlane);
}

void Camera::setPosition(float x, float y, float z)
{
    this->p_position.x = x;
    this->p_position.y = y;
    this->p_position.z = z;
}

void Camera::Update()
{
    D3DXMatrixPerspectiveFovLH(&this->p_matrixProj, this->getFOV(), this->getAspectRatio(), this->getNearPlane(), this->getFarPlane());
    g_engine->getDevice()->SetTransform(D3DTS_PROJECTION, &this->p_matrixProj);

    D3DXMatrixLookAtLH(&this->p_matrixView, &this->p_position, &this->p_target, &this->p_upDir);
    g_engine->getDevice()->SetTransform(D3DTS_VIEW, &this->p_matrixView);
}

I am calling the Update() in every game_update()

Now i am wondering why there is no SetTransform(D3DTS_WORLD, ...) according to the camera movement. Is it just missing and i would have to implement it by myself, or do i need to set it elsewhere? I am kinda lost of how the "normal" set up would be to implement such a thing, and maybe a camera following the player (i guess i would have to the the World Transform according to the player position, am i right)?

Maybe someone can give me some clearance, or just a little tutorial

EDIT: Forgot to mention that it is not working right. I am currently Drawing two things: one Sprite with the D3DXSprite and the grid the D3DXLine.

EDIT2: Here is how i transform my sprites and set the world Matrix. It is still not working though :(. Any ideas?

void Sprite::transform()
    {
        D3DXMATRIX mat;
        D3DXVECTOR2 scale((float)this->_scaling, (float)this->_scaling);
        D3DXVECTOR2 center((float)(this->_width*this->_scaling)/2, (float)(this->_height*this->_scaling)/2);
        D3DXVECTOR2 trans((float)this->getX(), (float)this->getY());
        D3DXMatrixTransformation2D(&mat, NULL, 0, &scale, &center, (float)this->_rotation, &trans);
        g_engine->getSpriteHandler()->SetTransform(&mat);
    }

If you need any more code, please let me know! Thanks a lot!

\$\endgroup\$
  • \$\begingroup\$ I think it's better to start with a orthogonal projection matrix, rather than a perspective. Easier to learn. \$\endgroup\$ – Gustavo Maciel Jul 7 '13 at 16:21
  • \$\begingroup\$ I would write an answer, but I'm kinda busy right now. To make things short: World shouldn't be called on camera, it make vertices on the Object space going to World space. It should be called on the entities, accordingly to their position and rotation on the world. \$\endgroup\$ – Gustavo Maciel Jul 7 '13 at 16:24
  • \$\begingroup\$ So everywhere where i draw things i would call SetTransform(D3DTS_WORLD,...) with the Translation, Scale and Rotation matrix? For a Sprite i guess i would use the D3DXSprite->SetTransform(), but what about the D3DXLine? There is no SetTransform. Only a DrawTransform() is it the same? \$\endgroup\$ – puelo Jul 7 '13 at 16:31
  • \$\begingroup\$ Updated my answer with the transform() method of my sprite class. Is this the right way to do this? \$\endgroup\$ – puelo Jul 7 '13 at 16:37
  • \$\begingroup\$ Don't know for sure, but I think 2D transformation don't mix well with perspective projection. Try using an orthogonal one. \$\endgroup\$ – Gustavo Maciel Jul 7 '13 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.