Remka
Remka

Reputation: 69

GameMaker Studio 2 - Center + clamp camera on zoom

I am fairy new to GameMaker, and I am playing around, trying to create my first platformer following Shaun Spalding's tutorials, especially this one regarding player camera tracking. I am using GameMaker Studio 2 on a Mac.

I am trying to use a similar approach to allow smooth zooming during dramatic-ish scripted events, basically slowly ramping up or down the camera_set_view_size() params to achieve smooth zooms.

I currently have the current code on am oCamera object, which works for the zooming part.

Create event

/// @description Setup camera

zoom_level = 1;

cam = view_camera[0];
follow = oPlayer;
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;

default_zoom_width = camera_get_view_width(cam);
default_zoom_height = camera_get_view_height(cam);
currW = default_zoom_width;
currH = default_zoom_height;

Step event

/// @description Update camera

// Update zoom size
if keyboard_check_pressed(ord("I")) {
  zoom_level = 0.5;
} else if keyboard_check_pressed(ord("O")) {
  zoom_level = 1;
} else if keyboard_check_pressed(ord("P")) {
  zoom_level = 2;
}

// Update destination
if (instance_exists(follow)) {
  xTo = follow.x;
  yTo = follow.y;
}

// Update object position
x += (xTo - x) / 25;
y += (yTo - y) / 25;

// Update view size
zoomXTo = default_zoom_width * zoom_level;
zoomyTo = default_zoom_height * zoom_level;
currW += (zoomXTo - camera_get_view_width(cam)) / 25;
currH += (zoomyTo - camera_get_view_height(cam)) / 25;
camera_set_view_size(cam, currW, currH);

// Clamp camera 
x = clamp(x, view_w_half, room_width - view_w_half);
y = clamp(y, view_h_half, room_height - view_h_half);

// Update camera position
camera_set_view_pos(cam, x - view_w_half, y - view_h_half);

The zooming bit probably could be done a bit more concisely, but does the job. I am still wondering how to :

Been tinkering with these for a bit, and doesn't seem to able to figure how to do it, so any help would be appreciated.

Thanks a lot!

Upvotes: 0

Views: 3382

Answers (1)

0TastyPie0
0TastyPie0

Reputation: 11

I was also looking for a solution to this, and with your code I was able to put something together. Here you go:

Create Event

// Camera Setup
cam = view_camera[0];
cam_default_w = camera_get_view_width(cam);
cam_default_h = camera_get_view_height(cam);
follow = oPlayer;
view_w_half = camera_get_view_width(cam) * 0.5;
view_h_half = camera_get_view_height(cam) * 0.5;
xTo = xstart;
yTo = ystart;

// Zooming
zoomLevel = 1;
temp_cam_w = cam_default_w;
temp_cam_h = cam_default_h;

// Shaking
shake_length = 0;
shake_magnitude = 0;
shake_remain = 0;
buff = 32;

Step Event

// Update zoom size
if keyboard_check_pressed(ord("I")) {
  zoomLevel = 0.5;
} else if keyboard_check_pressed(ord("O")) {
  zoomLevel = 1;
} else if keyboard_check_pressed(ord("P")) {
  zoomLevel = 2;
}

// Update view size
zoomXTo = cam_default_w * zoomLevel;
zoomYTo = cam_default_h * zoomLevel;
temp_cam_w += (zoomXTo - camera_get_view_width(cam)) / 25;
temp_cam_h += (zoomYTo - camera_get_view_height(cam)) / 25;
var temp_cam_w_half = temp_cam_w/2;
var temp_cam_h_half = temp_cam_h/2;
camera_set_view_size(cam, temp_cam_w, temp_cam_h);

// Update destination
if (instance_exists(follow)) {
    xTo = follow.x;
    yTo = follow.y;
}

// Update object position
x += (xTo - x) / 15;
y += (yTo - y) / 15;

// Keep within room
x = clamp(x, temp_cam_w_half+buff, room_width-temp_cam_w_half-buff);
y = clamp(y, temp_cam_h_half+buff, room_height-temp_cam_h_half-buff);

// Camera shake
x += random_range(-shake_remain, shake_remain);
y += random_range(-shake_remain, shake_remain);
shake_remain = max(0, shake_remain-((1/shake_length)*shake_magnitude));

// Update camera view
camera_set_view_pos(cam, x-temp_cam_w_half, y-temp_cam_h_half);

Essentially what I changed was to make the "clamp" part and "set view position" know when a change in size has been made. Note that I used different variable names than you, since I am using this for my own project as well.

Upvotes: 1

Related Questions