-1
\$\begingroup\$

I made a sort of "floating camera" for a 2-D platforming game set to follow the player by default but can be made to snap to a stationary point in a room for boss battles and the like, but I've encountered a bit of a problem. Namely that this camera has a tendency to show the outside of the room as the player gets closer to its boundaries, which ultimately reveals my levels to be surrounded by an unsightly void beyond the reach of my room tile assets. In short, I need a way to make a camera object only show what's within the boundaries of a room like a Viewport does, without actually resorting to a pure viewport camera as that does not seem capable of changing which object it follows like I need it to. Any advice?

For reference, here is the basic script I am using for this "floating" camera:

Create event:

camera = camera_create();

var vm = matrix_build_lookat(x,y,-10,x,y,0,0,1,0);
var pm = matrix_build_projection_ortho(512,384,1,500);

camera_set_view_mat(camera,vm);
camera_set_proj_mat(camera,pm);

view_camera[0] = camera;

follow = obj_player;
xTo = x;
yTo = y;

boss_level = true;

Step event:

x += (xTo - x)/25;
y += (yTo - y)/25;

if (follow != noone)
{
    xTo = follow.x;
    yTo = follow.y;

}


var vm = matrix_build_lookat(x,y,-9,x,y,0,0,1,0);
camera_set_view_mat(camera,vm);

if(boss_level && distance_to_object(obj_boss_marker) <= 256)
    follow = obj_boss_marker;
else
    follow = obj_player;
\$\endgroup\$
2
\$\begingroup\$

Try something like this:

var viewWidth = 512;
var viewHeight = 384;

var posx = clamp(x, viewWidth div 2, room_width - viewWidth div 2 - 1);
var posy = clamp(y, viewHeight div 2, room_height - viewHeight div 2 - 1);

var vm = matrix_build_lookat(posx,posy,-9,posx,posy,0,0,1,0);
camera_set_view_mat(camera,vm);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.