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

Discussion in 'Programming' started by twalkerp, Aug 4, 2019.

  1. twalkerp

    twalkerp Member

    Joined:
    Oct 12, 2018
    Posts:
    29
    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!!
     
  2. twalkerp

    twalkerp Member

    Joined:
    Oct 12, 2018
    Posts:
    29
    Any ideas? Thank you
     
  3. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    965
    There is a clamp() function.
     
    twalkerp likes this.
  4. twalkerp

    twalkerp Member

    Joined:
    Oct 12, 2018
    Posts:
    29
    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: Aug 4, 2019
  5. Japster

    Japster Member

    Joined:
    Aug 5, 2017
    Posts:
    51
    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!....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice