GameMaker how do i make the players not go outside camera view??

This is my problem ..
when i have 2 players playing the camera follows the center between the 2 players but when one player gets to far away he goes outside the camera view.. I want the player to stay within the view boundaries.

Code:
 create event
// Enable views
view_enabled = true;
view_visible[0] = true;
zoom = 2;

intpolW = 2;
intpolH = 1;

// Create camera
camera = camera_create_view(0, 0, RES_W, RES_H);

view_set_camera(0, camera);

// Resize window & application surface
window_set_size(RES_W * RES_SCALE, RES_H * RES_SCALE);

alarm[0] = 1; //window_center();

surface_resize(application_surface, RES_W * RES_SCALE, RES_H * RES_SCALE);

display_set_gui_size(RES_W, RES_H);

#region center not active

// Center window
var display_width = display_get_width();
var display_height = display_get_height();

var window_width = RES_W * RES_SCALE;
var window_height = RES_H * RES_SCALE;

window_set_position(display_width/2 - window_width/2, display_height/2 - window_height/2);

#endregion

follow = o_ship_parent;
step event:
Code:
 step event.
// Get current camera position
var camX = camera_get_view_x(camera);
var camY = camera_get_view_y(camera);
var camW = camera_get_view_width(camera);
var camH = camera_get_view_height(camera);

//check player count put in list
var player_num = instance_number(o_ship_parent);
var players = ds_grid_create(1,1);
ds_grid_resize(players,1,player_num);

var i = 0;
with(o_ship_parent){
    players[# 0,i] = id;
    i++
}

if player_num > 3 player_num = 2;

switch(player_num){
#region //single player
   case 1 : 
   if instance_exists(follow){

    // Set target camera position
    var targetX = follow.x  - (camW/2);
    var targetY = follow.y  - (camH/2);
        
    // Clamp the target to room bounds
    targetX = clamp(targetX, 0, room_width - camW);
    targetY = clamp(targetY, 0, room_height - camH);

    // Smoothly move the camera to the target position
    camX = lerp(camX, targetX, CAM_SMOOTH);
    camY = lerp(camY, targetY, CAM_SMOOTH);        
    }
    
    
    // Apply camera position
    camera_set_view_pos(camera, camX, camY);
    camera_set_view_size(camera, camW, camH);

    break;
#endregion

#region //2 player
    case 2: 
    
    // Set target camera position between player 1 and 2
    var multi_xx = mean(players[# 0,0].x,players[# 0,1].x);
    var multi_yy = mean(players[# 0,0].y,players[# 0,1].y);
    
    // Clamp the target to room bounds
    var xx1 = clamp(multi_xx - camW/2, 0, room_width - camW);
    var yy1 = clamp(multi_yy - camH/2, 0, room_height - camH);
    
    //distance_between player 1 and 2
    var distance_x = abs(players[# 0,0].x - players[# 0,1].x);
    var distance_y = abs(players[# 0,0].y - players[# 0,1].y);
        
         
    //zooming
    if distance_x > 896/2 and distance_x < 896 and distance_y > 504/3 and distance_y < 504{
        if camW <= 1280 or camH < 720{
        camW = Approach(camW,1280,intpolW);
        camH = Approach(camH,720,intpolH);
        }
    }
    if distance_x < 896/2 and distance_x > 0 {
        {
        camW = Approach(camW,896,intpolW);
        camH = Approach(camH,504,intpolH);
        }
    }

    // Smoothly move the camera to the target position
    var cx = camera_get_view_x(camera);
    var cy = camera_get_view_y(camera);

    camX = lerp(cx,xx1,CAM_SMOOTH);
    camY = lerp(cy,yy1,CAM_SMOOTH);
    
    //update camera position
    camera_set_view_pos(camera,camX,camY);
    camera_set_view_size(camera,camW,camH);
 
    break;
}
 
GML:
var xx1 = clamp(multi_xx - camW/2, 0, room_width - camW);
    var yy1 = clamp(multi_yy - camH/2, 0, room_height - camH);
multi_xx - camW/2 - to my understanding you've found the mean between player 1 and player 2 x.positions and are now deducting that value by
half of the camera width.

Eg.

P1.x = 10
P2.x = 100

Mean.x = 55

Mean.x - camW/2 = off screen...

Try taking camW/2 out of both the X and Y and see if there's an improvement.
 
Yes but I mean... that I want a border so the players cannot move beyond the screen if they are to far away from each other.. so there will be an invisible wall there...

Im bad at explaining but i hope you all understand..
 

HayManMarc

Member
There would probably be a maximum distance that the players can be from each other. You would just need to clamp that distance and not allow movement in any direction that would make that distance greater.

Hope that helps.
 
There would probably be a maximum distance that the players can be from each other. You would just need to clamp that distance and not allow movement in any direction that would make that distance greater.

Hope that helps.
yes that would be the way to go .. but how do i do that.. in the player object or? and codewise how would that be most efficient to implement?
 
yes that would be the way to go .. but how do i do that.. in the player object or? and codewise how would that be most efficient to implement?
One way that jumps out to me is utilising your multi_x and multi_y variables as the variable inside each X and Y clamp then set your min and max distance values for them
 
Top