0
\$\begingroup\$

I'm trying to make an pseudo-camera in my SDL2 game. The WASD input movement is already working fine (it's a camera that points aims on the center of the target), but i'm struggling on making an zoom effect. Maybe I need more Math knowledge to know how to do this...

There is the camera object, which type is an SDL_Rect. Its position starts on 0, 0, and the size is 1280x720 (the same resolution as the game window). But as I said before, the camera is aiming on the center of the target (it has an offset).

And there is an update() function for every instance of a certain object, that also has an SDL_Rect and an texture attached to it, which is a block. And it calculates what position it should move based on the camera position:

void Block::Update()
{
    //The Game::zoom variable is of type "int", that starts at 0, and it can either decrease or increase.
    //set the block size and increase it based on the camera zoom factor, and set it as the "size" variable:
    size = 32 + Game::zoom;

    //Code for making the XY coords movements work:
    xpos = (trueXpos * size) - (Game::camera.x - (Game::camera.w / 2));
    ypos = (trueYpos * size) - (Game::camera.y - (Game::camera.h / 2));

    //Set the block resolution size based on the size variable:
    srcRect.h = size;
    srcRect.w = size;
    srcRect.x = 0;
    srcRect.y = 0;

    //And this is the part where i'm struggling. This actual code *kinda* works how i wanted.
    destRect.x = xpos - (Game::zoom + (Game::camera.x * (Game::zoom / 8)));
    destRect.y = ypos - (Game::zoom + (Game::camera.y * (Game::zoom / 8)));
    destRect.w = srcRect.w;
    destRect.h = srcRect.h;
}
\$\endgroup\$

You must log in to answer this question.

Browse other questions tagged .