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

Android How Can I Clamp Global Dragging Gesture to Room Height and Width?

T

twalkerp

Guest
I have a zoom in and out function, and am adding a global gesture "Dragging" to allow the user to pan the camera around while zoomed in.

Global Drag Start
Code:
drag_room_x = event_data[? "posX"];
drag_room_y = event_data[? "posY"];
Global Dragging
Code:
var _x = drag_room_x - event_data[? "rawposX"];
var _y = drag_room_y - event_data[? "rawposY"];

camera_set_view_pos(cam,_x,_y);
I'm trying to clamp the dragging of the camera_set_view_pos to be within the room only, so they don't see outside the room, how do I accomplish this?

P.S (Low priority) - the dragging is a bit choppy, it will "jump back" a bit to where the last touch position was when dragging starts.

Thank you!!
 
T

twalkerp

Guest
There is a clamp() function.
Yes, I've managed to lock the camera to the dimensions I want to zoom in/zoom out on pinch with clamp(), but I can't figure out the math behind clamping based on the event_data and dragging,

I figured it would be something like
Code:
camera_set_view_pos(cam,clamp(_x,0,room_width/2),clamp(_y,0,room_height/2));
That somewhat works, but that leaves 3/4 of the room with outside the room, and the top left 1/4 is the game

I've gotten the drag to only trigger when zoomed in
Code:
camwidth = camera_get_view_width(view_camera[0]);
camheight = camera_get_view_height(view_camera[0]);

if(camheight < room_height && camwidth < room_width)
camera_set_view_pos(cam,_x,_y);
But still dont know how to only drag within confines of room
 
Last edited by a moderator:

Japster

Member
I had to do something similar for Tetralogical's pieces grabbing (needed to drag pieces around and have them stop before they went off screen, although no zooming of screen, which I'm aware will require more logic/maths, in order to keep zoomed in/out content in the level boundaries!).

I found I couldn't get the results I wanted with clamp (probably my fault) - so I just used MIN and MAX functions to restrict/limit my final values, to ensure it was either the position somewhere in the screen, or max/min screen width/height? (I'm aware that clamp is probably just feeding a value through these 2 functions though)

I might be getting the wrong end of the stick though, regarding your issue!....
 
Top