How do I confine my camera's view to be only within the room.

J

Jackie Paper

Guest
Haven't quite figured this out yet.

Here's my code:

In the Creation Event of object oCamera:
// Initialize Viewports
view_enabled = true;
view_visible[0] = true;

view_xport[0] = 0;
view_yport[0] = 0;
view_wport[0] = 320;
view_hport[0] = 180;

view_camera[0] = camera_create_view(0, 0, view_wport[0], view_hport[0], 0, noone, -1, -1, -1, -1);

var _dwidth = display_get_width();
var _dheight = display_get_height();
var _xpos = (_dwidth / 2) - (_dwidth / 3);
var _ypos = (_dheight / 2) - (_dheight / 3);
window_set_rectangle(_xpos, _ypos, 1280, 720);
window_set_position((_dwidth / 2) - (_dwidth / 3), (_dheight / 2) - (_dheight / 3));

surface_resize(application_surface, 320, 180);

window_center();

// Create Camera
/* camera = camera_create();

// Define Matrices
var vm = matrix_build_lookat(x, y, -10, x, y, 0, 0, 1, 0); // View Matrix
var pm = matrix_build_projection_ortho(320, 180, 1, 10000); // Projection Matrix

// Apply Matrices
camera_set_view_mat(camera, vm);
camera_set_proj_mat(camera, pm);

view_camera[0] = camera; */

follow = oPlayer;
xTo = x; // x coordinate we're moving to.
yTo = y; // y coordinate we're moving to.

if (instance_exists(follow)) {
x = follow.x;
y = follow.y;
} else {
x = room_width / 2;
y = room_height / 2;
}

sprite_index = noone;



In the Step Event of object oPlayer i have:

// Camera Control
var _hor = kRight - kLeft;
var _ver = kDown - kUp;

var _viewX = camera_get_view_x(view_camera[0]);
var _viewY = camera_get_view_y(view_camera[0]);
var _viewW = camera_get_view_width(view_camera[0]);
var _viewH = camera_get_view_height(view_camera[0]);
var _gotoX = x + (_hor * 15) - (_viewW * 0.5);
var _gotoY = y + (_ver * 15) - (_viewH * 0.5);

var _newX = lerp(_viewX, _gotoX, 0.1);
var _newY = lerp(_viewY, _gotoY, 0.1);
camera_set_view_pos(view_camera[0], _newX, _newY);
 

DukeSoft

Member
Easy!
Code:
var _newX = lerp(_viewX, _gotoX, 0.1);
var _newY = lerp(_viewY, _gotoY, 0.1);
_newX = clamp(_newX, 0, room_width-_viewW);
_newY = clamp(_newY, 0, room_height-_viewH);
This will clamp your camera to the rooms' bounds.
 
J

Jackie Paper

Guest
Easy!
Code:
var _newX = lerp(_viewX, _gotoX, 0.1);
var _newY = lerp(_viewY, _gotoY, 0.1);
_newX = clamp(_newX, 0, room_width-_viewW);
_newY = clamp(_newY, 0, room_height-_viewH);
This will clamp your camera to the rooms' bounds.
Thank you so much! I kept clamping the wrong variables. Still learning. Thanks again.
 
Top