Help with local multiplayer

W

Warmachine33

Guest
Ok so I'm working on a top down RPG and I want to make it up to four players locally can join. So far I just made a second player and put him in the game, but when I move him the camera does not stay centered on both of them. So I want to try to do 2 things I want the camera to stay center on the players when they are close to each other and if it is possible when they leave the area the camera switches to split screen. I will put the code I have for my camera below and if you need more info let me know, appreciate any help. //obj_camera room start event
GML:
//camera
view_enabled = true;
view_visible[0] = true;


scale = 5;
var cam = camera_create_view(0, 0, RESOLUTION_W, RESOLUTION_H, 0, -1, -1, -1, RESOLUTION_W/2, RESOLUTION_H/2);
view_set_camera(0,cam);

window_set_size(RESOLUTION_W*scale, RESOLUTION_H*scale);
surface_resize(application_surface, RESOLUTION_W*scale, RESOLUTION_H*scale);
//obj_camera end step event
Code:
var CW = camera_get_view_width(view_camera[0]);
var CH = camera_get_view_height(view_camera[0]);

var CX = oplayer.x - CW/2;
var CY = oplayer.y - CH/2;

//clamp
CX = clamp(CX, 0, room_width - CW);
CY = clamp(CY, 0, room_height - CH);

//small rooms
if(room_width<CW || room_height<CH){
    CX = room_width/2 - CW/2
    CY = room_height/2 - CH/2;
}

camera_set_view_pos(view_camera[0], CX, CY);
 
Last edited by a moderator:

davawen

Member
Maybe do this, I'm not sure and I'm on my phone but I think it's what you want:
GML:
var aspect_ratio = room_height/room_width,
    _w = abs(player1.x-player2.x)+20,
    _h = _w*aspect_ratio;

camera_set_view_size(view_camera, _w, _h);
camera_set_view_pos(view_camera, min(player1.x, player2.x)-10, mean(player1.y, player2.y)-_h/2);

//You can clamp everything here
 
W

Warmachine33

Guest
Hey thanks for responding. So I tried the code and it caused the camera to zoom in way too close to the players legs and then when I started walking farther away the camera started zooming out. Maybe I need to play with the code some more but that's what I'm getting right now.
 

davawen

Member
That's what supposed to happen, I'm basically getting the horizontal distance between the players to get the width, then multiply it by room_height/room_width to keep the same aspect ratio.
The reason it zooms on the legs is because I'm using the raw y value, so if your origin is set on the legs, that's where it'll zoom.
I'm on a PC right now, so I'll make a better code.

GML:
//Get player positions relative to their center
var _p1x = obj_player1.x - obj_player1.sprite_xoffset + obj_player1.sprite_width/2,
    _p1y = obj_player1.y - obj_player1.sprite_yoffset + obj_player1.sprite_height/2,
    _p2x = obj_player2.x - obj_player2.sprite_xoffset + obj_player2.sprite_width/2,
    _p2y = obj_player2.y - obj_player2.sprite_yoffset + obj_player2.sprite_height/2;

//Get the aspect ratio, and the size based off the distance between players
var _aspectRatio = RESOLUTION_H/RESOLUTION_W,
    _w = clamp( abs(_p1x - _p2x)+20, WIDTH_MIN, WIDTH_MAX ),
    _h = _w * _aspectRatio;

//Get the positions based on the mean of the player positions, and clamp them in the room
var _cx = clamp( mean(_p1x, _p2x) - _w/2, 0, room_width-_w),
    _cy = clamp( mean(_p1y, _p2y) - _h/2, 0, room_height-_h);

camera_set_view_size(view_camera, _w, _h);
camera_set_view_pos(view_camera, _cx, _cy);
 
W

Warmachine33

Guest
Ok thanks again for your help I'll try it out later about to head to work right now.
 
W

Warmachine33

Guest
Well when I use the code the camera does seem like its centered on the players. It will also let the players wonder off the screen when they move too far from each other, which would be fine if I could get the camera to then switch to split screen mode and then back when they get next to each other. Is there a way to do that ???
 
W

Warmachine33

Guest
So I was able to implement split screen in my game does anyone know how to make a toggle for the split screen??? Basically I'm trying to make it so when the players are within a certain distance of each other it turns split screen off and when the distance of the players are farther away from each other split screen is activated. Any ideas are welcomed and appreciated and I will post the code I have for my split screen below. By the way the if and else statement is my failed attempt at trying to make a toggle lol
GML:
var ex, ey;
ex = instance_nearest(x, y, oplayer).x;
ey = instance_nearest(x, y, oplayer).y;

if point_distance(x, y, ex, ey) < 30
{                   
//camera
view_enabled = true;
view_visible[0] = true;

 scale = 5;
 
 
var cam = camera_create_view(0, 0, RESOLUTION_W, RESOLUTION_H, 0, -1, -1, -1, RESOLUTION_W/2, RESOLUTION_H/2);
view_set_camera(0,cam);

window_set_size(RESOLUTION_W*scale, RESOLUTION_H*scale);
surface_resize(application_surface, RESOLUTION_W*scale, RESOLUTION_H*scale);
}
else if point_distance(x, y, ex, ey) > 30
{
    scale = 5;
    view_enabled = true;
view_visible[0] = true;
view_xport[0] = 0;
view_yport[0] = 0;
view_wport[0] = RESOLUTION_W / 2;
view_hport[0] = RESOLUTION_H;
view_camera[0] = camera_create_view(0, 0, RESOLUTION_W / 2, RESOLUTION_H, 0, oplayer, -1, -1, RESOLUTION_W, RESOLUTION_H);

// Set up camera for view[0] (player 2)
view_visible[1] = true;
view_xport[1] = RESOLUTION_W / 2; // Offset the second view for player two within the game window
view_yport[1] = 0;
view_wport[1] = RESOLUTION_W / 2;
view_hport[1] = RESOLUTION_H;
view_camera[1] = camera_create_view(0, 0, RESOLUTION_W / 2, RESOLUTION_H, 0, oZaden, -1, -1, RESOLUTION_W, RESOLUTION_H);

// Resize the game window and the app surface to accomodate both view ports

surface_resize(application_surface, RESOLUTION_W *scale, RESOLUTION_H*scale);     
}
 
Top