Detect if out of bounds areas are visible in camera or not

I am working on a top down camera setup for a typical RTS like experience. But unlike many existing setups, I want to ensure that the camera doesn’t see anything in the out of bounds area (currently my map is 1009 x 1009). This means that the camera should not move when it’s on the edge of out of bounds being visible. This gets complicated by the fact the camera should be able to be rotated on the yaw and pitch axes. This means that I can’t just create a collision box and call it a day as the visible area depends not only on the position of the camera but also on the rotation.

Right now, I am doing some math to find out the bounds based on all the variables. It’s progressing but I was wondering if there is a better method or if it’s already a solved problem and I am trying to reinvent the wheel.

For eg. I have been thinking about creating an invisible actor on the boundary of my map and restricting movement when any part of the actor comes into frame. But I don’t know if there is a good way to detect when the actor has come into the camera’s frame.

Any suggestions would be appreciated.

Hey @SilentGarud!

Typically the way this would be done is indeed to set a bounding box, with collision on the INSIDE, or 4 collision volumes (planes or boxes) covering the outside. THEN, with the rotation, you clamp it in the rotation logic to prevent it from looking UP too far so you can’t see off into the horizon! :slight_smile:

I’d go for something like DeprojectScreentoWorld or ConvertScreenLocationToWorldSpace, projecting corners of the screen to world, probably with some math based on output of this function if you want to cast trace down to Z=0 plane. Though worth mention that getting viewport size in ue sometimes can be non-trivial task

The problem with the rotation is that, how much I can look UP depends on position of the camera. If the camera is towards the BOTTOM of the map, I can look up more than if the camera was at the TOP of the map. Similarly, the zoom level would also play a role.

I’m not sure what you mean by “How much I can look UP depends on the position of the camera.” Bottom/top meaning closer to/farther from the terrain? Or something like North/South?

(I typed this next part up last night and forgot to send it so I’m sending this now as an exercise.) Okay, so for the rotation and zoom, let’s look at some example cameras for RTS’s that are pretty big names that have a zoomable camera.

There’s almost the entire “Total War” series. What they did was have the camera in the early days be un-rotateable in the X or Y axis, only Z (left/right turning only). Then later they added a small Y rotation as you zoom down, and now with their modern entries they allow you to rotate in the Y a BIT but not much- manually. If I were to guess… it’s probably kept between -90 and -30. But the modern titles also have low-poly landscape going off into the distance of each map, these angles are just to keep the camera focused on the playable landscape and not up into the sky.

Then there’s "Baldur’s gate 3. You can rotate in Z axis only, but if you zoom in, it rotates the Y axis a little more, the closer you get to the ground.

Both BG3 and the middle of the TW series have the zoom depend on math such as “Camera Z pos - ground Z pos * camera Y rot variable adjustment number = Camera Y rot” or something to that effect due to not being able to rotate Y. So the question is what would the closest camera system be that already exists to create and then branch from?

Sorry it was unclear. I mean pitch up and down.

Since you mention some examples, the camera of Cities: Skylines is what I am trying to achieve, with some constraints. In CS, as you zoom out, the pitch becomes vertical and cant be changed but as you zoom in, there is more and more freedom to pitch.

Additionally, unlike CS, I want to pan only until the edge of the map is visible. CS overcame this by having lots of unplayable space in the non modded version and in recent CS2 they don’t care about the edge of the map being visible. In my case, I am planning to impart the illusion of a much larger map by not letting the player see the map edge.

This was what I meant by “is bottom close to the terrain or are you meaning south?”


So this is how you would lay it out to prevent seeing the “Edge” of the world. You’d put a terrain down and raise up the edges to be mountains. That way if the player zooms way in and looks to the horizon the illusion holds. You could also put in a huge water volume so they could see the horizon but it’s so far out they wouldn’t be able to notice, much like a real ocean.

I am already using that idea. The playing area is inside a crater and I have been deliberating if I should clamp the camera height so that it doesn’t go beyond the crater hills.

Sorry again, by bottom of the map, I mean south of the map.

Just as you are clamping X and Y, yes, you should absolutely clamp Z.

So are you saying the more north you are, the less you can rotate Y to look forward/up and the more south you are, the more you can rotate Y to look forward/up? Is this the current state of the game or is this what you want? Because quite honestly that’s very strange to accidentally create.

Yeah, that’s pretty much it. The reason for this is that if the camera is pointed northish and as I move the camera north, I will get closer and closer to the map edge and that will mean that I have to pitch the camera less to see out of bounds. Hence to prevent the camera from seeing out of bounds, I need to limit its pitch the more north it goes.

Ahhhhhhhhh I see.

I think the solution here is not that. I would argue for terrain extension/ raising. Forcing a camera to angle down at the edges feels bad- it gives more of a loss of control feeling than a bounding box, which does do that, this would just make it worse.

Now you CAN do what you’re implying… however the math would get unwieldy pretty fast versus just making the terrain higher. Alternatively, you could set the camera’s maximum Z lower, as well. Cities Skylines is okay with a high Z position because the terrain goes out WAYYYYYY far. But if you look at most other RTS’s they have a definite “Zooming out beyond here takes the player out of the action” point. You need to figure out what that point is, THEN adjust your crater edges to make sure they’re high enough and the X/Y position limitations are enough. It’s gonna take a little tuning, but I can guarantee it’ll be easier than the math you’re wanting.

1 Like

Welp, I have thought about it and you are right. I wanted to give the player a strategic view so as to say. I have decided to instead do this via a second camera that gives a top down view of the whole playing area as a minimap of sorts.

I am trying to implement this now. I feel like the best way to do this would be to attach a collision sphere to the camera so that camera collides with the terrain and a blocking volume over the map at a particular height blocking the camera’s maximum height.

Well, I have been trying to do that and not succeeding. I have created a new thread for that. Thank you so much for taking the time and helping me decide what to do.

Cheers!

1 Like