• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Smooth Camera Zoom

F

Frolacosta

Guest
I am trying to learn the new camera system in GMS:2, and I am trying to get the camera to gradually zoom out to a certain point when the spacebar is pressed. I set up my camera using this wonderful guide: https://forum.yoyogames.com/index.php?threads/guide-meet-the-new-camera-system.12269/

The zoom functionality in this guide works on using the mouse scrollwheel, however I need mine to work with a spacebar press. I currently have this code.

Code:
if(keyboard_check_pressed(vk_space)) {
     camera_set_view_size(view_camera[0],zoomWidth,zoomHeight);
}
Issue is, this code zooms out instantly, whereas I need the code to zoom out gradually.
Does anyone have any ideas on how this could be achieved?
 
R

Robin Gherghetta

Guest
create:
zoom_level = 1;

//Get the starting view size to be used for interpolation later
default_zoom_width = camera_get_view_width(view_camera[0]);
default_zoom_height = camera_get_view_height(view_camera[0]);


step:
//this is cahnges the zoom based on scolling but you can set it how ever you like
zoom_level = clamp(zoom_level + (((mouse_wheel_down() - mouse_wheel_up())) * 0.1), 0.5, 2);

//Get current size
var view_w = camera_get_view_width(view_camera[0]);
var view_h = camera_get_view_height(view_camera[0]);

//Set the rate of interpolation
var rate = 0.2;

//Get new sizes by interpolating current and target zoomed size
var new_w = lerp(view_w, zoom_level * default_zoom_width, rate);
var new_h = lerp(view_h, zoom_level * default_zoom_height, rate);

//Apply the new size
camera_set_view_size(view_camera[0], new_w, new_h);

var vpos_x = camera_get_view_x(view_camera[0]);
var vpos_y = camera_get_view_y(view_camera[0]);

//change coordinates of camera so zoom is centered
var new_x = lerp(vpos_x,vpos_x+(view_w - zoom_level * default_zoom_width)/2, rate);
var new_y = lerp(vpos_y,vpos_y+(view_h - zoom_level * default_zoom_height)/2, rate);


This code is mostly from some official gm tutorial
 
F

Frolacosta

Guest
create:
zoom_level = 1;

//Get the starting view size to be used for interpolation later
default_zoom_width = camera_get_view_width(view_camera[0]);
default_zoom_height = camera_get_view_height(view_camera[0]);


step:
//this is cahnges the zoom based on scolling but you can set it how ever you like
zoom_level = clamp(zoom_level + (((mouse_wheel_down() - mouse_wheel_up())) * 0.1), 0.5, 2);

//Get current size
var view_w = camera_get_view_width(view_camera[0]);
var view_h = camera_get_view_height(view_camera[0]);

//Set the rate of interpolation
var rate = 0.2;

//Get new sizes by interpolating current and target zoomed size
var new_w = lerp(view_w, zoom_level * default_zoom_width, rate);
var new_h = lerp(view_h, zoom_level * default_zoom_height, rate);

//Apply the new size
camera_set_view_size(view_camera[0], new_w, new_h);

var vpos_x = camera_get_view_x(view_camera[0]);
var vpos_y = camera_get_view_y(view_camera[0]);

//change coordinates of camera so zoom is centered
var new_x = lerp(vpos_x,vpos_x+(view_w - zoom_level * default_zoom_width)/2, rate);
var new_y = lerp(vpos_y,vpos_y+(view_h - zoom_level * default_zoom_height)/2, rate);


This code is mostly from some official gm tutorial
That's the code from the guide I linked which I mentioned doesn't work for my needs. I have since solved this myself using this code


Code:
if(keyboard_check_pressed(vk_space)) {
viewWidth += (zoomWidth-viewWidth)/10;
viewHeight += (zoomHeight-viewHeight)/10;
camera_set_view_size(view_camera[0],viewWidth,viewHeight);
}
 
G

glueckgabi

Guest
@Robin Gherghetta

Oh my god thank you! Your code works flawlessly. You just saved me from a massive headache.

Quick question though, how do you adjust how far the camera can zoom out. My room is 2880x720 but it seems to only zoom out to 960x540?

Thank you again
 
Top