Make view not go past a point

N

NoFontNL

Guest
This code should follow the player object, stored in variable follow, but when it reaches a specific x coordinate or y coordinate, it should prevent the view from going past that point. However, when moving the view to the inner side of the room and moving the view back, it doesn't go past the y coordinate with an offset of 64 inwards. (Picture with explanation below)

Camera object:
Code:
if(instance_exists(follow)){
   
            x+=(follow.x-x)/10;
            y+=(follow.y-y)/5;
     
}
if(instance_exists(follow)){
        x=follow.x;
    }
    if(rectangle_in_rectangle(x-view_wview/2,room_height/2,x+view_wview/2,room_height/2,global.cameraOffset,global.cameraOffset,room_width-global.cameraOffset,room_height-global.cameraOffset) != 1){
        while(view_xview<global.cameraOffset){
            view_xview++;
        }
        while(view_xview+view_wview>room_width-global.cameraOffset){
            view_xview--;
        }
        x=view_xview+view_wview/2;
    }
    if(instance_exists(follow)){
        y=(follow.bbox_top+follow.bbox_bottom)/2;
    }
    if(rectangle_in_rectangle(room_width/2,y-view_hview/2,room_width/2,y+view_hview/2,global.cameraOffset,global.cameraOffset,room_width-global.cameraOffset,room_height-global.cameraOffset) != 1){
        while(view_yview<global.cameraOffset){
            view_yview++;
        }
        while(view_yview+view_hview>room_height-global.cameraOffset){
            view_yview--;
        }
        y=view_yview+view_hview/2;
    }
upload_2020-1-14_11-3-23.png
Left picture: Running this code when the actual edge of the view is past the specific point.
Middle picture: Running te code when you have moved the view more inside the room.
Right picture: Moving the view back to the coordinates in the first picture. (But the view doesn't go to the same position)
 
Last edited by a moderator:

NightFrost

Member
While I don't have time to debug your code, I'll just point out an inefficiency in your while loops. If they had some additional logic and/or early exits inside it would be a different matter but right now they just increment a variable. For example the first is equivalent to:
Code:
if(view_xview < global.cameraOffset){
    view_xview = global.cameraOffset;
}
A thought occurs to me however. Since the while-loops are not made mutually exclusive, they can both run if the values overlap. That is, the first while-loop increases the variable so much that after it is done the second loop also catches it and decreases it back down below the given threshold. Depends on what the actual values are, so you might want to show_debug_message them inbetween to see what's happening. Also, one common strategy to make view stay within bounds is to just use clamp to force it, ie something like view_x = clamp(0, room_width - view_width, view_x).
 
N

NoFontNL

Guest
While I don't have time to debug your code, I'll just point out an inefficiency in your while loops. If they had some additional logic and/or early exits inside it would be a different matter but right now they just increment a variable. For example the first is equivalent to:
Code:
if(view_xview < global.cameraOffset){
    view_xview = global.cameraOffset;
}
A thought occurs to me however. Since the while-loops are not made mutually exclusive, they can both run if the values overlap. That is, the first while-loop increases the variable so much that after it is done the second loop also catches it and decreases it back down below the given threshold. Depends on what the actual values are, so you might want to show_debug_message them inbetween to see what's happening. Also, one common strategy to make view stay within bounds is to just use clamp to force it, ie something like view_x = clamp(0, room_width - view_width, view_x).
I'm using a camera object. When I just clamp the view, then a screen shake will happen. The view moves to the camera object and then gets clamped. Then it moves to the camera object again and gets clamped again.

Edit: even when using clamp, it still offsets the view by 64 pixels. Not immediately, but when moving the view the room inwards and then moving back.
 
Last edited by a moderator:
M

maxdax5

Guest
Code:
view_xview = x-(view_wview/2);
view_yview = y-(view_hview/2);
view_xview = clamp(view_xview,0,room_width-view_wview);
view_yview = clamp(view_yview,0,room_height-view_hview);
Use this in your object_camera and this will move the view to the position of the object_camera. It then clamp the view to your needed values if those are exeeded.
 
N

NoFontNL

Guest
The problem was that 'object following' in the room settings was set to obCamera. Can someone clarify why this causes the strange behaviour?
The problem is fixed, but I still would like to know why this problem occurred.
 

TheouAegis

Member
The problem was that 'object following' in the room settings was set to obCamera. Can someone clarify why this causes the strange behaviour?
The problem is fixed, but I still would like to know why this problem occurred.
Setting the view_object or camera target forces GM to use its own view handling and you lose [most] control of the view.
 
Top