P
paulomtts
Guest
Hello,
I have recently started a small project, and while setting up the basic functionalities I have encountered some issues while trying to combine a click and drag style camera with the ability to zoom in and out.
THE PROBLEM AT HAND
The problem I have been experincing happens whenever I zoom in or out and then try to move the camera. Before I attempt any zooming, the camera will move smoothly via click and drag, but after I have zoomed, the slightest flick will update the camera position a little bit in a somewhat rash manner. It seems as if instead of updating center of the screen gradually as per the click and drag functionality, the zooming somehow displaces it.
Therefore, I'd like to ask for the help of any who might know what causes the issue. I'd be grateful for any ideas.
OBJECT & CODE
My camera consist of an object (objCamera) that has the following events inside it:
As you may expect, startX and startY are initialized in Variables Definitions, both with value equal to 0.
Thanks in advance for any comments!
I have recently started a small project, and while setting up the basic functionalities I have encountered some issues while trying to combine a click and drag style camera with the ability to zoom in and out.
THE PROBLEM AT HAND
The problem I have been experincing happens whenever I zoom in or out and then try to move the camera. Before I attempt any zooming, the camera will move smoothly via click and drag, but after I have zoomed, the slightest flick will update the camera position a little bit in a somewhat rash manner. It seems as if instead of updating center of the screen gradually as per the click and drag functionality, the zooming somehow displaces it.
Therefore, I'd like to ask for the help of any who might know what causes the issue. I'd be grateful for any ideas.
OBJECT & CODE
My camera consist of an object (objCamera) that has the following events inside it:
- Create - Initialize camera
GML:
/// @description Initialize camera
//Resolution
#macro RES_W 1024
#macro RES_H 768
#macro RES_SCALE 1
//Enable views
view_enabled = true;
view_visible[0] = true;
//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);
surface_resize(application_surface, RES_W * RES_SCALE, RES_H * RES_SCALE);
display_set_gui_size(RES_W, RES_H);
//Center window in display
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);
- Step - Zoom in/out
GML:
/// @description Zoom in/out
camW = camera_get_view_width(camera);
camH = camera_get_view_height(camera);
var wheel = mouse_wheel_down() - mouse_wheel_up();
if(wheel != 0){
wheel *= 0.05;
//Add to size
var addW = camW * wheel;
var addH = camH * wheel;
camW += addW;
camH += addH;
//Clamp zoom
camW = clamp(camW, RES_W / 2, room_width);
camH = clamp(camH, RES_H / 2, room_height);
camera_set_view_size(camera, camW, camH);
}//IF
- Global Drag Start - Get start position
GML:
/// @description Get start position
//Get last position in which mouse was pressed
startX = event_data[? "posX"]
startY = event_data[? "posY"]
- Global Dragging - Move camera to point
GML:
/// @description Move camera to point
//Subtract START POSITION by CURRENT POSITION
var currX = startX - event_data[? "rawposX"];
var currY = startY - event_data[? "rawposY"];
//Clamp to room limits
currX = clamp(currX, 0, room_width - camW);
currY = clamp(currY, 0, room_height - camH);
//Set camera to position
camera_set_view_pos(camera, currX, currY);
As you may expect, startX and startY are initialized in Variables Definitions, both with value equal to 0.
Thanks in advance for any comments!