• 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!

GameMaker help with: Camera panning with mouse

C

CanRuf

Guest
Hello!
I am using the new camera system in GMS2 and i couldn't figure out how to pan the camera using the mouse.
I want the same or similar panning to the GMS2 Workspace, where you hold down mb_middle and move the mouse to pan the view.

My camera objects create event code:
Code:
camera = camera_create();

var viewmat = matrix_build_lookat(x,y,-100,x,y,0,0,1,0);
var projmat = matrix_build_projection_ortho(320,240,1.0,32000.0);
camera_set_view_mat(camera,viewmat);
camera_set_proj_mat(camera,projmat);
view_camera[0] = camera;
Thanks in advance.
 

NazGhuL

NazTaiL
Hi! What I once did: When mouse_button is pressed, each step set the mouse_x and mouse_y point as the Anchor point(Like x_previous and y_previous). Then the next step look from how much pixel it moves on the X and Y axis. Then move the camera using: camera_set_view_pos.
 
C

CanRuf

Guest
Can you please provide how you would do this in code?
I understand the logic of how it can be done but i can't think up how to do this in code. I tried many things that made sense on paper,but in the end it just sent the camera flying to x -infinity.
Also i use a camera object to control the cameras x and y positions. Thanks.
 

samspade

Member
Hello!
I am using the new camera system in GMS2 and i couldn't figure out how to pan the camera using the mouse.
I want the same or similar panning to the GMS2 Workspace, where you hold down mb_middle and move the mouse to pan the view.

My camera objects create event code:
Code:
camera = camera_create();

var viewmat = matrix_build_lookat(x,y,-100,x,y,0,0,1,0);
var projmat = matrix_build_projection_ortho(320,240,1.0,32000.0);
camera_set_view_mat(camera,viewmat);
camera_set_proj_mat(camera,projmat);
view_camera[0] = camera;
Thanks in advance.
I don't have GMS2 so I apologize if this leads you in the wrong direction, I did a brief look up and it seems that GMS2 has a lot of new camera specific features that GMS doesn't. However, I do exactly what you want, I think, in my current game by creating a camera object which tracks the mouse_x and mouse_y and center the view on the camera object. You could easily set it up so this tracking only occurs if you hold down a button and you can also set it up so that the object snaps to the mouse pointer or trails behind it.
 

NazGhuL

NazTaiL
Let say global.camera is your actual camera.
I made a view that is 200x200:

add to the create event:
Code:
PX = mouse_x;
PY = mouse_y;
then on step event:
Code:
if(mouse_check_button_pressed(mb_left))
{
PX = mouse_x;
PY = mouse_y;
}

if(mouse_check_button(mb_left))
{
var mx = mouse_x-PX;//difference on the x axis
var my = mouse_y-PY;//difference on the y axis

var vx = camera_get_view_x(global.camera);//view_x
var vy = camera_get_view_y(global.camera);//view_y
var nx = vx-mx;//new view_x
var ny = vy-my;//new view_y
nx = clamp(nx, 0, room_width-200);//clamp in the boundaries of the room.
ny = clamp(ny, 0,room_height-200);
camera_set_view_pos(global.camera, nx, ny);//Set position
}
 
Top