TulioHenry
Member
I've started a project of a local multiplayer shooter, where the cameras will work like this:
- 1 player : the camera will only focus the player , without zoom
- 2-4 player: the camera will calculate a zoom and it will add to the camera size
but i have a problem, when the camera add the zoom out the sprites stretch, losing the aspect ratio
#macros
GAMEWIDTH = 640;
GAMEHEIGHT = 360;
heres the code:
Game object -> i use to control the game in general
//create
oCAMERA object
//Create
//Step
My inspiration to the camera code was the camera of the duck game.
- 1 player : the camera will only focus the player , without zoom
- 2-4 player: the camera will calculate a zoom and it will add to the camera size
but i have a problem, when the camera add the zoom out the sprites stretch, losing the aspect ratio
#macros
GAMEWIDTH = 640;
GAMEHEIGHT = 360;
heres the code:
Game object -> i use to control the game in general
//create
GML:
windowscale = 2;
window_set_size(GAMEWIDTH*windowscale,GAMEHEIGHT*windowscale);
display_set_gui_size(GAMEWIDTH*windowscale,GAMEHEIGHT*windowscale);
alarm[0] = 1;
surface_resize(application_surface,GAMEWIDTH*windowscale,GAMEHEIGHT*windowscale);
//Create
GML:
following = oparPlayer;
zoom = 2;
spd = 0.1; //the speed of interpolation
GML:
gameW = GAMEWIDTH / 2 * zoom;
gameH = GAMEHEIGHT / 2 * zoom;
camera_set_view_size(CAM, gameW, gameH);
var player_num = instance_number(oparPlayer);
var players = ds_grid_create(1,1);
ds_grid_resize(players,1,player_num);
var i = 0;
with(oparPlayer){
players[# 0,i] = id;
i++
}
switch(player_num){
case 1 : //single player
if(instance_exists(following)){
var xx = clamp(following.x - gameW/2, 0, room_width - gameW);
var yy = clamp(following.y - gameH/2, 0, room_height - gameH);
var cx = camera_get_view_x(CAM);
var cy = camera_get_view_y(CAM);
camera_set_view_pos(CAM,
lerp(cx,xx,spd),
lerp(cy,yy,spd) );
}
break;
case 2: //2 players
var multi_xx = mean(players[# 0,0].x,players[# 0,1].x);
var multi_yy = mean(players[# 0,0].y,players[# 0,1].y);
var xx1 = clamp(multi_xx - gameW/2, 0, room_width - gameW);
var yy1 = clamp(multi_yy - gameH/2, 0, room_height - gameH);
var distance_x = abs(players[# 0,0].x - players[# 0,1].x);
var distance_y = abs(players[# 0,0].y - players[# 0,1].y);
zoom = clamp(lerp(zoom, sqrt(sqr(distance_x) + sqr(distance_y) ) / 100, spd- 0.05), 3, 4)
var cx = camera_get_view_x(CAM);
var cy = camera_get_view_y(CAM);
camera_set_view_pos(CAM,
lerp(cx,xx1,spd),
lerp(cy,yy1,spd) );
break;
case 3:
var multi_xx = mean(players[# 0,0].x,players[# 0,1].x,players[# 0,2].x);
var multi_yy = mean(players[# 0,0].y,players[# 0,1].y,players[# 0,2].y);
var xx1 = clamp(multi_xx - gameW/2, 0, room_width - gameW);
var yy1 = clamp(multi_yy - gameH/2, 0, room_height - gameH);
var distance_x1 = abs(players[# 0,0].x - players[# 0,1].x);
var distance_y1 = abs(players[# 0,0].y - players[# 0,1].y);
var zoom1 = sqrt(sqr(distance_x1) + sqr(distance_y1));
var distance_x2 = abs(players[# 0,1].x - players[# 0,2].x);
var distance_y2 = abs(players[# 0,1].y - players[# 0,2].y);
var zoom2 = sqrt(sqr(distance_x2) + sqr(distance_y2));
var distance_x3 = abs(players[# 0,2].x - players[# 0,0].x);
var distance_y3 = abs(players[# 0,2].y - players[# 0,0].y);
var zoom3 = sqrt(sqr(distance_x3) + sqr(distance_y3));
zoom = clamp(mean(zoom1,zoom2,zoom3)/100,3,4);
var cx = camera_get_view_x(CAM);
var cy = camera_get_view_y(CAM);
camera_set_view_pos(CAM,
lerp(cx,xx1,spd),
lerp(cy,yy1,spd) );
break;
case 4:
var multi_xx = mean(players[# 0,0].x,players[# 0,1].x,players[# 0,2].x,players[# 0,3].x);
var multi_yy = mean(players[# 0,0].y,players[# 0,1].y,players[# 0,2].y,players[# 0,3].y);
var xx1 = clamp(multi_xx - gameW/2, 0, room_width - gameW);
var yy1 = clamp(multi_yy - gameH/2, 0, room_height - gameH);
var distance_x1 = abs(players[# 0,0].x - players[# 0,1].x);
var distance_y1 = abs(players[# 0,0].y - players[# 0,1].y);
var zoom1 = sqrt(sqr(distance_x1) + sqr(distance_y1));
var distance_x2 = abs(players[# 0,1].x - players[# 0,2].x);
var distance_y2 = abs(players[# 0,1].y - players[# 0,2].y);
var zoom2 = sqrt(sqr(distance_x2) + sqr(distance_y2));
var distance_x3 = abs(players[# 0,2].x - players[# 0,3].x);
var distance_y3 = abs(players[# 0,2].y - players[# 0,3].y);
var zoom3 = sqrt(sqr(distance_x3) + sqr(distance_y3));
var distance_x4 = abs(players[# 0,3].x - players[# 0,0].x);
var distance_y4 = abs(players[# 0,3].y - players[# 0,0].y);
var zoom4 = sqrt(sqr(distance_x4) + sqr(distance_y4));
zoom = clamp(mean(zoom1,zoom2,zoom3,zoom4)/100,3,4);
var cx = camera_get_view_x(CAM);
var cy = camera_get_view_y(CAM);
camera_set_view_pos(CAM,
lerp(cx,xx1,spd),
lerp(cy,yy1,spd) );
break;
}
Last edited: