• 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 Dynamic Camera

Gamerev147

Member
Backstory:
So I recently started to switch over to Game Maker Studio 2. Much of the code is different. It's really hard for me to learn this new code when I knew the old code so well! Anyway, I need help creating a Dynamic Camera like Shaun Spaulding's video on a dynamic camera.

Attempts:
Since view_xview and view_yview have been removed, it is very difficult to achieve the same thing as the video. I tried using camera_get_view_x by doing:
Code:
var vx = camera_get_view_x(view_current);
var vy = camera_get_view_y(view_current);
But it isn't functioning the same as the old functions; view_xview and view_yview.

Question

I was wondering if someone already knew new code for a dynamic camera or if they could take the code from the video and convert it to Game Maker Studio 2 code for me? So in other words, How can I make a Dynamic Camera in Game Maker Studio 2?

All help is appreciated, thanks! :)
 
Last edited:
N

Never Mind

Guest
Code:
camera_set_view_pos(camera,x,y)
??

I don't use GMS2 yet so I'm guessing here.
I assume that function is similar to directly setting a views position the old way:
Code:
view_xview[0] = xx;
view_yview[0] = yy;

EDIT: As an alternative, this function camera_set_view_target(camera,object) should work the same as:
Code:
view_object[0] = id;
sets up the camera to follow a target object (instance).
If you assign the target to a camera object then you'll only need to worry about moving that around.

By the way I did not post this just for you! I think you may have some underlying issues with the way your learning/asking questions. I'd work on that first.
For instance, you failed to provide any of this information:
  • What you've tried
  • Results your getting
  • What you think might work
  • What you do understand
  • How you think things should be / could be
  • Specifically what you're trying to make happen
  • References to your current learning sources (Did you read carefully through the new functions list? Which parts confuse you?)

Anyways, it's no big deal.. people post stuff like this all the time. I just doubt you'll get many enthusiastic responses making posts like "I hate the new camera system".
 
Last edited by a moderator:
N

Never Mind

Guest
I looked at the function list again and noticed a couple things you should look into:
camera_apply(camera) - I'm not sure what the context is where you need this, but it's possible it's required to make things work.
camera_set_update_script(camera,script) - Same thing with this one.. It's possible that you have to use an update script to actually make changes to the camera.

THIS IS ALL GUESS WORK! I still haven't tried GMS2.

I apologize if I'm totally off here o_O

EDIT - Oh yeah I forgot.. This is probably the first thing I should have looked for:
[GUIDE] Meet the New Camera System
https://forum.yoyogames.com/index.php?threads/guide-meet-the-new-camera-system.12269/
Usually when something new comes out there's more than one person having trouble figuring it out
 

Gamerev147

Member
I looked at the function list again and noticed a couple things you should look into:
camera_apply(camera) - I'm not sure what the context is where you need this, but it's possible it's required to make things work.
camera_set_update_script(camera,script) - Same thing with this one.. It's possible that you have to use an update script to actually make changes to the camera.

THIS IS ALL GUESS WORK! I still haven't tried GMS2.

I apologize if I'm totally off here o_O

EDIT - Oh yeah I forgot.. This is probably the first thing I should have looked for:
[GUIDE] Meet the New Camera System
https://forum.yoyogames.com/index.php?threads/guide-meet-the-new-camera-system.12269/
Usually when something new comes out there's more than one person having trouble figuring it out
Awesome. Thank you. I'll try a few things that you suggested and get back to you if you want!
 
K

Kurtyywurtyy

Guest
Nope. I was able to make a basic camera though.
I was able to get it working. I did some copy/paste from the New Camera Guide, plus some manipulation based on the video tutorial you were referencing. But here's a working piece.

obj_camera

Room Start:

Code:
//Added thing for changing stuff
target_view = 0;

//Enable the use of views
view_enabled = true;
rate = 0.2;

//Make view 0 visible
view_set_visible(target_view, true);

//Set the port bounds of view 0 to 640x480
view_set_wport(target_view, 640);
view_set_hport(target_view, 480);

//Resize and center
window_set_rectangle((display_get_width() - view_wport[target_view]) * 0.5, (display_get_height() - view_hport[target_view]) * 0.5, view_wport[target_view], view_hport[target_view]);
surface_resize(application_surface,view_wport[target_view],view_hport[target_view]);


//Camera creation

//Build a camera at (0,0), with size 640x480, 0 degrees of angle, no target instance, instant-jupming hspeed and vspeed, with a 32 pixel border
camera = camera_create_view(32, 32, 240, 160, 0, -1, -1, -1, 32, 32);

//Set view0 to use the camera "camera"
view_set_camera(target_view, camera);
Step:
Code:
/// Move the camera toward your mouse
var xTo,yTo;
move_towards_point(mouse_x,mouse_y,0);
xTo = obj_poacher.x + lengthdir_x(min(90,distance_to_point(mouse_x,mouse_y)),direction);
yTo = obj_poacher.y + lengthdir_y(min(90,distance_to_point(mouse_x,mouse_y)),direction);

x += (xTo-x)/10;
y += (yTo-y)/10;

//Get target view position and size. size is halved so the view will focus around its center
var vpos_x = camera_get_view_x(view_camera[target_view]);
var vpos_y = camera_get_view_y(view_camera[target_view]);
var vpos_w = camera_get_view_width(view_camera[target_view]) * 0.5;
var vpos_h = camera_get_view_height(view_camera[target_view]) * 0.5;

//Interpolate the view position to the new, relative position.
var new_x = lerp(vpos_x, obj_camera.x - vpos_w, rate);
var new_y = lerp(vpos_y, obj_camera.y - vpos_h, rate);

//Update the view position
camera_set_view_pos(view_camera[target_view], new_x, new_y);
 

Gamerev147

Member
I was able to get it working. I did some copy/paste from the New Camera Guide, plus some manipulation based on the video tutorial you were referencing. But here's a working piece.

obj_camera

Room Start:

Code:
//Added thing for changing stuff
target_view = 0;

//Enable the use of views
view_enabled = true;
rate = 0.2;

//Make view 0 visible
view_set_visible(target_view, true);

//Set the port bounds of view 0 to 640x480
view_set_wport(target_view, 640);
view_set_hport(target_view, 480);

//Resize and center
window_set_rectangle((display_get_width() - view_wport[target_view]) * 0.5, (display_get_height() - view_hport[target_view]) * 0.5, view_wport[target_view], view_hport[target_view]);
surface_resize(application_surface,view_wport[target_view],view_hport[target_view]);


//Camera creation

//Build a camera at (0,0), with size 640x480, 0 degrees of angle, no target instance, instant-jupming hspeed and vspeed, with a 32 pixel border
camera = camera_create_view(32, 32, 240, 160, 0, -1, -1, -1, 32, 32);

//Set view0 to use the camera "camera"
view_set_camera(target_view, camera);
Step:
Code:
/// Move the camera toward your mouse
var xTo,yTo;
move_towards_point(mouse_x,mouse_y,0);
xTo = obj_poacher.x + lengthdir_x(min(90,distance_to_point(mouse_x,mouse_y)),direction);
yTo = obj_poacher.y + lengthdir_y(min(90,distance_to_point(mouse_x,mouse_y)),direction);

x += (xTo-x)/10;
y += (yTo-y)/10;

//Get target view position and size. size is halved so the view will focus around its center
var vpos_x = camera_get_view_x(view_camera[target_view]);
var vpos_y = camera_get_view_y(view_camera[target_view]);
var vpos_w = camera_get_view_width(view_camera[target_view]) * 0.5;
var vpos_h = camera_get_view_height(view_camera[target_view]) * 0.5;

//Interpolate the view position to the new, relative position.
var new_x = lerp(vpos_x, obj_camera.x - vpos_w, rate);
var new_y = lerp(vpos_y, obj_camera.y - vpos_h, rate);

//Update the view position
camera_set_view_pos(view_camera[target_view], new_x, new_y);
Awesome man! I'll give it a try! :);)
 
T

Tempus

Guest
I tried the camera setup. The camera is moving like it should. But sometimes i get some flickering black borders around my walls. The walls are objects and my background color is black. I use the depth ordering for the objects used in the dungeon demo of gm2.
Has anyone the same problem?
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
I tried the camera setup. The camera is moving like it should. But sometimes i get some flickering black borders around my walls
This sounds like a sub-pixel error. If you are scaling the camera view and/or moving the camera in increments of less than a pixel then the graphics card can do this as it's not sure which pixel to render where. This can possibly be solved by making sure the camera coordinates are rounded to the nearest whole pixel, or by increasing the texture border width for the texture page the sprites are on.

PS: You should really make a new topic rather than necro bump and hijack someone elses. ;)
 
Top