• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

problem with zoom and limiting the viewport inside the room

Didjynn

Member
Hello everyone,

I have a viewport zooming in and out.

The thing is (and I don't find why that's why I'm here), zooming in or out teleports the center of my view to the bottom right corner of the room. Always. And I have absolutly no idea why.

Tell me if I mistake but the position of the view is the middle of it (I checked) and not the top left corner.
To avoid the view to show what's outside of the room, I check the position
Code:
room_width - camera_get_view_width(view_camera[0])
(for the right side) when to divide camera_get_view_width(view_camera[0]) by 2 seemed much more logical to me.

For the zoom itself here is my code, there is some lines but nothing complicated (or maybe it is since I can't debug it o_O )


Now is the code for my zooming system, as I said I don't get why the center of the viewport when I zoom in and out go in the bottom right corner of the room. If somebody can help me ?

Code:
// ZOOM
if mouse_wheel_down() {
    if zoom_factor < 1 {
        zoom_factor += 0.1
        camera_set_view_size(view_camera[0], room_width * zoom_factor, room_height * zoom_factor)
        cam_width = camera_get_view_width(view_camera[0]) / 2
        cam_height = camera_get_view_height(view_camera[0]) / 2
        show_debug_message("mx = " + string(mouse_x) + "  my = " + string(mouse_y))
        camera_set_view_pos(view_camera[0], mouse_x, mouse_y);
       show_debug_message("viewport x = " + string(camera_get_view_x(view_camera[0])) + "  y = " + string(camera_get_view_y(view_camera[0])))
        scrCamPos()
    }
}else if mouse_wheel_up(){
    if zoom_factor > 0.1 {
        zoom_factor -= 0.1
        camera_set_view_size(view_camera[0], room_width * zoom_factor, room_height * zoom_factor)
        cam_width = camera_get_view_width(view_camera[0]) / 2
        cam_height = camera_get_view_height(view_camera[0]) / 2
        camera_set_view_pos(view_camera[0], mouse_x, mouse_y);
        scrCamPos()
    }
}
This is the script I have in the 2 conditions to clean the code :

Code:
//scrCamPos
if camera_get_view_x(view_camera[0]) + cam_width > room_width {
    camera_set_view_pos(view_camera[0], room_width - cam_width, camera_get_view_y(view_camera[0]))
    show_debug_message("post zoom viewport x = " + string(camera_get_view_x(view_camera[0])) + "  y = " + string(camera_get_view_y(view_camera[0])))
    window_mouse_set(window_get_width() / 2, window_get_height() / 2)
}

if camera_get_view_x(view_camera[0]) - cam_width < 0 {
    camera_set_view_pos(view_camera[0], cam_width, camera_get_view_y(view_camera[0]))
    show_debug_message("post zoom viewport x = " + string(camera_get_view_x(view_camera[0])) + "  y = " + string(camera_get_view_y(view_camera[0])))
    window_mouse_set(window_get_width() / 2, window_get_height() / 2)
}

if camera_get_view_y(view_camera[0]) - cam_height < 0 {
    camera_set_view_pos(view_camera[0], camera_get_view_x(view_camera[0]), cam_height)
    show_debug_message("post zoom viewport x = " + string(camera_get_view_x(view_camera[0])) + "  y = " + string(camera_get_view_y(view_camera[0])))
    window_mouse_set(window_get_width() / 2, window_get_height() / 2)
}

if camera_get_view_y(view_camera[0]) + cam_height > room_height {
    camera_set_view_pos(view_camera[0], camera_get_view_x(view_camera[0]), room_height - cam_height)
    show_debug_message("post zoom viewport x = " + string(camera_get_view_x(view_camera[0])) + "  y = " + string(camera_get_view_y(view_camera[0])))
    window_mouse_set(window_get_width() / 2, window_get_height() / 2)
}
 

Rob

Member
Views are drawn from the top left corner, like rooms. I don't have time to properly check your code atm but thinking that views are drawn from the center might be part of the problem
 

Didjynn

Member
pretty sure it's that... it's stupid but I was really convinced it was from the center. Thanks !
 
Top