GMS 2 2 player dynamic zoom - Help

Daveb21st

Member
I am working on a 2 player platformer.
  • I want my two characters to be seen in the camera
  • and to zoom in and out according to their x distance
//Zoom// Adjusts the camera size perfectly.
//Tracking// Centers the camera position perfectly.
But both together mess up the centering...
Everything works fine until I add the //Zoom// section, which messes up the camera position.
But I don't know why. I have tried many things and yet the camera never centers between the players when //Zoom// is added.
...unless I delete //Zoom// and then it centers perfectly again.

help?

GML:
//Get camera width
var cam_w = camera_get_view_width(view_camera[0]);
var cam_h = camera_get_view_height(view_camera[0]);

//Player position shortcut
var p1x = obj_player_1.x;
var p1y = obj_player_1.y;
var p2x = obj_player_2.x;
var p2y = obj_player_2.y;

//Player H-distance
var xptp = abs(p2x - p1x);

//Center camera between players
if instance_exists(obj_player_1) && instance_exists(obj_player_2) {
x = (p1x + p2x) * .5;
y = (p1y + p2y) * .5;
}

//Zoom
camera_set_view_size(view_camera[0],xptp,xptp*.75);

//Tracking
camera_set_view_pos(view_camera[0],x - cam_w*.5,y - cam_h*.5);
 

Ido-f

Member
I wouldn't know, but I did find in similar issues that the solution was resizing the application surface.
So maybe you could try adding
GML:
surface_resize(application_surface, xptp, xptp*.75)
in there, or some variation of that which accounts for screen scaling.
 

Daveb21st

Member
I wouldn't know, but I did find in similar issues that the solution was resizing the application surface.
So maybe you could try adding
GML:
surface_resize(application_surface, xptp, xptp*.75)
in there, or some variation of that which accounts for screen scaling.
The game has pixel art, and that resized them and messes them up a bit. Thanks anyways :)
 

Daveb21st

Member
You're storing cam_w and cam_h based on the current view size, then changing the view size. You need to wait to compute cam_w and cam_h until after you set the size.
OMG that fixed it!!! It boggled my mind... :eek:
I guess camera_get_view prioritizes default room size unless changed?
Thank you so much!
 

Nidoking

Member
I guess camera_get_view prioritizes default room size unless changed?
It's not a matter of defaults. It's that you're getting a value, storing it, then changing the value. It's like counting the number of cans of beans in your pantry, then taking a can of beans with you before closing the door. Now that you've changed the number of cans, your original count was wrong, so you have to count again. Here, you're determining the size of the view, then changing the size of the view. If you needed to know the size before you changed it, then you'd have to get it twice. In this case, you didn't need that information before changing it, so you can wait until after you set the final size to get it.
 
Top