Camera Zoom without changing render position in sdl

Im trying to implement a proper Camera into my Game. At the moment i have some Gameobjects with an Image component and i subtract the Render Position of the Image with the Camera position. The problem is when i zoom into the game my Camera moves into the left corner. What i want to is that the Camera stay at the position where i have zoomed in.

Code:

tRect.x = Convert.ToInt32(((_ParentTransform.GetPosition().X * Camera.Camera.Instance._Scale.X) - (_Width * Camera.Camera.Instance._Scale.X)/2) - Camera.Camera.Instance._Scale.X * (Camera.Camera.Instance._Position.X ) );
tRect.y = Convert.ToInt32(((_ParentTransform.GetPosition().Y * Camera.Camera.Instance._Scale.Y) - (_Height * Camera.Camera.Instance._Scale.Y)/2) - Camera.Camera.Instance._Scale.Y * (Camera.Camera.Instance._Position.Y) );
tRect.h = (int)(_Height * Camera.Camera.Instance._Scale.X);
tRect.w = (int)(_Width * Camera.Camera.Instance._Scale.Y);

_Height, _Width is my Image Size.

while (SDL.SDL_PollEvent(out e) != 0)
        {


            switch (e.type)
            {
                case SDL.SDL_EventType.SDL_QUIT:

                    SDL.SDL_Quit();
                    Environment.Exit(0);
                    break;
                case SDL.SDL_EventType.SDL_MOUSEWHEEL:
                    if (e.wheel.y > 0)
                    {
                        Camera.Camera.Instance._Scale.X +=  .05f;
                        Camera.Camera.Instance._Scale.Y += .05f;

                    }
                    else if (e.wheel.y < 0)
                    {
                        Camera.Camera.Instance._Scale.X -= .05f;
                        Camera.Camera.Instance._Scale.Y -= .05f;
                    }
                    break;
            }
        }

I update the Camera position to its parent.The start position is (0, 0).

public void Update(float deltaTime)
    {
        _Position = _Parent.GetComponent<TransformComponent>().GetPosition();


    }

Hey, I believe that happens because you are not centering your camera according to the screen size. Essentially, the center of your camera is the top-left corner, so it ends up zooming there.

This may be solved by adding half the screen raw width&height to the final position of the rect, without applying the scale modifier to them.

@Andrei_Rafael Oh you right Thanks! :slight_smile: The camera zoom works fine now but why does the camera position start in the top right corner and moves then to the center?

Hmm Iā€™m not quite sure I understand what you mean. Could you please explain in more detail?

I fixed it, just had some wrong numbers. Thanks for your help :slight_smile:

1 Like