SOLVED Trying to "Stop" a Camera From Going to Certain Parts of a Room

Hi all,

I'm currently working on a 2D game with a smooth moving camera that follows the player. The camera itself works fine and using clamp() I've managed to keep it only moving within the confines of the room it's placed in. I want to be able to control where the camera goes more precisely, though. I thought that in theory, I could have an object act as a "wall" that stops the camera from moving when it presses up against it, so for example:
  • If the top border of the camera hits this object while the player jumps, the camera should not go any higher.
  • If the bottom border hits it while the player falls, it should go no lower.
  • And repeat this logic for the left and right borders, but on the X-axis.
But I can't figure out how I would register a collision between the camera's border(s) and this object, so that I can then get the camera to stop moving. Does anyone have any ideas?
 

D_W

Member
I have had success with doing this in two ways:

1) Have an object that your camera follows that basically is controlled the same way as your player and has it's own actual wall objects that only it can collide with. It can be very flexible and you can have your rooms be any shape depending on how you're doing collisions.
The downsides to this are:
  • It can be a lot of extra work as you basically have to design a level on top of your existing map to account for how your camera is going to move through the world.
  • It opens up a lot of weird edge-cases (no pun intended) where the player and camera get desync'd.
2) Have a "sub-room" object that your camera clamps into. Basically you would make an object with a sprite that is, at minimum, the same size as your view, then you can scale to different sizes in the room editor. Then you have some collision code (ie collision_point or instance_place, any collision function that returns an instance ID) to figure out which sub-room your currently in and clamp the camera to it's bounding box. This works surprisingly well and isn't too hard to implement compared to the previous option.
However, it has a major downside to this is that you could only have rectangular rooms.
 
I have had success with doing this in two ways:

1) Have an object that your camera follows that basically is controlled the same way as your player and has it's own actual wall objects that only it can collide with. It can be very flexible and you can have your rooms be any shape depending on how you're doing collisions.
The downsides to this are:
  • It can be a lot of extra work as you basically have to design a level on top of your existing map to account for how your camera is going to move through the world.
  • It opens up a lot of weird edge-cases (no pun intended) where the player and camera get desync'd.
2) Have a "sub-room" object that your camera clamps into. Basically you would make an object with a sprite that is, at minimum, the same size as your view, then you can scale to different sizes in the room editor. Then you have some collision code (ie collision_point or instance_place, any collision function that returns an instance ID) to figure out which sub-room your currently in and clamp the camera to it's bounding box. This works surprisingly well and isn't too hard to implement compared to the previous option.
However, it has a major downside to this is that you could only have rectangular rooms.
I did briefly think about that first suggestion, having basically an invisible "wall sensor object" which followed the player and controlled the camera that way, but until you described how it worked there, I wasn't quite sure how I'd achieve it. That second idea as well sounds neat. It reminds me of how the old NES Mega Man games used to handle rooms in their levels, if you're at all familiar with that. I'll give both a shot now, thank you very much for your suggestions!
 

D_W

Member
I did briefly think about that first suggestion, having basically an invisible "wall sensor object" which followed the player and controlled the camera that way, but until you described how it worked there, I wasn't quite sure how I'd achieve it. That second idea as well sounds neat. It reminds me of how the old NES Mega Man games used to handle rooms in their levels, if you're at all familiar with that. I'll give both a shot now, thank you very much for your suggestions!
It's funny you should say that, because NES Mega Man (and other NES games) is what I used it to emulate.
 
It's funny you should say that, because NES Mega Man (and other NES games) is what I used it to emulate.
I just managed to figure out the logic of it by playing around with setting a global variable that stores the "sub-room" instance ID and running some maths on the camera's position relative to said sub-room's bounding boxes. It works flawlessly! Now all I need to do is add screen transitions to make the experience a little smoother.
 
  • Like
Reactions: D_W
Top