I am building a 2 player game and at the moment the scroll position is determined by the midpoint of the 2 characters positions. But i have a problem. When the characters goes to far apart the scroll position of the screen is in the middle of these characters and no one is visible because they moved out of view.
So I am looking for an algorithm to determine the scale that the world should be at for the characters to be in view.
Your time is valued.
Assuming that your screen (or window) is fixed size, (Screen_Width, Screen_Height)
.
Now you compute the X and Y distances between the two players.
xdist = abs(Player2.x - Player1.x);
ydist = abs(Player2.y - Player1.y);
Compute the x and y scaling factors:
xscale = Screen_Width / xdist;
yscale = Screen_Height / ydist;
And take the smaller value.
scale = min(xscale, yscale);
Now, if scale >= 1
, then the players will fit on the screen. You don't have to do any scaling. You can just find the midpoint and draw.
If scale < 1
, then you have to scale all of your coordinates by scale
.