GMS 2 Zoom Centered To Player

JeanSwamp

Member
I'm trying to make a zoom for my player on a sidescrolling platformer. Currently everything works flawlessly expect when switching from zooming in to out, since the pos is relative to the top left corner and not the center, there's a quick repositioning on switch that looks very ugly.

I've tried a lot of things and I can't figure it out.. Here's the current code:

Code:
follow = oPlayer;
    xTo = follow.x;
    yTo = follow.y;
  
    x = lerp(x,xTo,0.2);
    y = lerp(y,yTo,0.2);
  
    zoom_level = 0.5;

    var view_w = camera_get_view_width(view_camera[0]);
    var view_h = camera_get_view_height(view_camera[0]);

    zoom_w = lerp(view_w, zoom_level * default_zoom_width, rate);
    zoom_h = lerp(view_h, zoom_level * default_zoom_height, rate);

    zoom_x = view_w_half * zoom_level;
    zoom_y = view_h_half * zoom_level;

   camera_set_view_size(cam, zoom_w, zoom_h);
   camera_set_view_pos(cam,x-zoom_x,y-zoom_y);
Like I said everything works as I want, I'm just missing a way to fix the repositioning. I've seen a lot of tutorials about centering the camera with a shift values, but I don't want the camera to be "centered" in the middle of the view, but on the player.

Hopefully someone can help me out! Thanks
 

Rob

Member
When you're zooming in/out you're changing the width/height of the view.
To centre the view on anything, whether it's the player or the middle of a room, you want to calculate half the width and height of the view as it is currently, and then minus those numbers from the x/y of whatever you want to center on.

As long as you understand that then it's just implementing it into code.
 

SSJCoder

Member
I think I found the problem, when you are still lerping the zoom width/height using:
Code:
zoom_w = lerp(view_w, zoom_level * default_zoom_width, rate);
zoom_h = lerp(view_h, zoom_level * default_zoom_height, rate);
you use the fully-lerped zoom width/height here:
Code:
zoom_x = view_w_half * zoom_level;
zoom_y = view_h_half * zoom_level;
so, you need to change that ^ to:
Code:
zoom_x = zoom_w / 2;
zoom_y = zoom_h / 2;
 
Top