GMS 2 Cameras not Working with Multiple Layers

I'm not sure if I'm being a complete idiot, but I'm still learning the new cameras system. I've created a room and the camera works fine, doing everything I want it to, but then I add a new layer to the room and then I can't see any objects. I know they exist, since I can see them drawing information (like a healthbar) to GUI layer, but my camera just can't see them.

I've tried changing the zfrom and zto arguments in the view matrix and the znear and zfar arguments in the orthrographic projection matrix. I've tried moving the camera to its own layer above everything else, but the only thing that works is having only one instance layer.

I don't know how much it will help, but here's my camera object's creation code (I have three different states depending on how many players will be playing):
Code:
window_set_size(global.res_x,global.res_y);
camW = global.res_x;
if (global.players > 1) camW /= 2;
camH = global.res_y;
if (global.players == 3) camH /= 2;

view_enabled = true;

view_visible[0] = true;

camera1 = camera_create();
var viewMat = matrix_build_lookat(
    camW/2,
    camH/2,
    -1000,
    camW/2,
    camH/2,
    1000,
    0,
    0,
    0
);
var projMat = matrix_build_projection_ortho(
    camW,
    camH,
    -1000,
    1000
);
camera_set_view_mat(camera1,viewMat);
camera_set_proj_mat(camera1,projMat);

view_camera[0] = camera1;

view_wport[0] = camW;
view_hport[0] = camH;

view_xport[0] = 0;
view_yport[0] = 0;

if (global.players > 1)
{
    view_visible[1] = true;
    
    camera2 = camera_create();
    var viewMat = matrix_build_lookat(
        camW/2,
        camH/2,
        -1000,
        camW/2,
        camH/2,
        1000,
        0,
        0,
        0
    );
    var projMat = matrix_build_projection_ortho(
        camW,
        camH,
        -1000,
        1000
    );
    camera_set_view_mat(camera2,viewMat);
    camera_set_proj_mat(camera2,projMat);
    
    view_camera[1] = camera2;
    
    view_wport[1] = camW;
    view_hport[1] = camH;
    
    view_xport[1] = camW;
    view_yport[1] = 0;
}

if (global.players == 3)
{
    view_visible[2] = true;
    
    camera3 = camera_create();
    var viewMat = matrix_build_lookat(
        camW/2,
        camH/2,
        -1000,
        camW/2,
        camH/2,
        1000,
        0,
        0,
        0
    );
    var projMat = matrix_build_projection_ortho(
        camW,
        camH,
        -1000,
        1000
    );
    camera_set_view_mat(camera3,viewMat);
    camera_set_proj_mat(camera3,projMat);
    
    view_camera[2] = camera3;
    
    view_xport[2] = 0;
    view_yport[2] = camH;
    
    view_wport[2] = camW;
    view_hport[2] = camH;
}
Here's that object's step event as well:
Code:
if (global.players == 1) //1 Player
{
    cam1_centerX = par_player.x;
    cam1_centerY = par_player.y;

    var viewMat1 = matrix_build_lookat(
        cam1_centerX,
        cam1_centerY,
        -1000,
        cam1_centerX,
        cam1_centerY,
        1000,
        0,
        1,
        0
    );
    camera_set_view_mat(camera1,viewMat1);
}
else if (global.players == 2) //2 Players
{
    for (var i = 0; i < instance_number(par_player); i += 1)
    {
        var P = instance_find(par_player,i);
        switch (P.player_number)
        {
            case 1: var p1 = P;
            case 2: var p2 = P;
        }
    }
    
    cam1_centerX = p1.x;
    cam1_centerY = p1.y;
    var viewMat1 = matrix_build_lookat(
        cam1_centerX,
        cam1_centerY,
        -1000,
        cam1_centerX,
        cam1_centerY,
        1000,
        0,
        1,
        0
    );
    camera_set_view_mat(camera1,viewMat1);
    
    cam2_centerX = p2.x;
    cam2_centerY = p2.y;
    var viewMat2 = matrix_build_lookat(
        cam2_centerX,
        cam2_centerY,
        -1000,
        cam2_centerX,
        cam2_centerY,
        1000,
        0,
        1,
        0
    );
    camera_set_view_mat(camera2,viewMat2);
}
else if (global.players == 3) //3 Players
{
    for (var i = 0; i < instance_number(par_player); i += 1)
    {
        var P = instance_find(par_player,i);
        switch (P.player_number)
        {
            case 1: var p1 = P;
            case 2: var p2 = P;
            case 3: var p3 = P;
        }
    }
    
    cam1_centerX = p1.x;
    cam1_centerY = p1.y;
    var viewMat1 = matrix_build_lookat(
        cam1_centerX,
        cam1_centerY,
        -1000,
        cam1_centerX,
        cam1_centerY,
        1000,
        0,
        1,
        0
    );
    camera_set_view_mat(camera1,viewMat1);
    
    cam2_centerX = p2.x;
    cam2_centerY = p2.y;
    var viewMat2 = matrix_build_lookat(
        cam2_centerX,
        cam2_centerY,
        -1000,
        cam2_centerX,
        cam2_centerY,
        1000,
        0,
        1,
        0
    );
    camera_set_view_mat(camera2,viewMat2);
    
    cam3_centerX = p3.x;
    cam3_centerY = p3.y;
    var viewMat3 = matrix_build_lookat(
        cam3_centerX,
        cam3_centerY,
        -1000,
        cam3_centerX,
        cam3_centerY,
        1000,                       
        0,
        1,
        0
    );
    camera_set_view_mat(camera3,viewMat3);
}
I don't know if resetting the view matrix every step is a good way to move the camera around, but that's what happened to work for me. More information is available upon request.
 

Jonar

Member
I have no idea what your problem is, but it’s probably best to just use a tutorial that is proven to work well and learn from that for whatever you’re doing. There’s no reason to reinvent the wheel.
 

samspade

Member
Agreed. While it seems like a problem with either your layer depth or your visibility settings I'm not sure. Personally, I've moved away from using the matrix options. In most 2D games they don't really provide any additional benefit to the basic camera functions and are more difficult to use and debug. I'd take a look at this system and use it instead.

 
I just switched over to using the abstracted camera_view functions and that somehow fixed it.

I still wonder why it didn't work with matrices though...
 
Top