- Home /
Calculating the viewing area corners in world space of a perspective camera that is NOT perpendicular to the viewing surface
Hey all!
So I have a game that is 2.5d, so its basically a top down game map, in which I want to keep the camera's frustum/viewing area from ever going outside of the edge of the map image, which is working right now for me, but now i want to add in slight amounts of rotation so that the camera isnt perpendicular to the map.
This is currently working for me and here's how I'm doing it:
1) With the height and width of the camera frustum, I am currently find the maximum distance for the camera to be positioned so that the viewing area never goes out of the map using the formulas given by Unity:
https://docs.unity3d.com/Manual/FrustumSizeAtDistance.html
2) Given the Height and Width of my screen I scaled down the target frustrum which i determined from finding the largest ratio of map.sizeDelta.x/screen.x and same with y, and then found the max distance from that
3) Then I have a target position at which the camera is going to be centered at, and with the center position and the target frustum width and height you can know how far off the camera that is going to be centered on this target object will be outside of the world space corners of the map (basically checking if centerPos.x > mapMinX + (frustrumWidth/2) and so on for each side)
4) If the position is too far in one direction, I change the target position to so that it doesn't spill out over the map, basically:
newPosition.x = mapMinX + (frustumWidth / 2);
3) Then I pass that target to my camera controller and it lerps its way to that position
Here's my question:
This is all just being calculated from a top down, exactly perpendicular to the map, like every example on google has discussed so far..
I want to be able to calculate these world corners of the viewing area from an angled camera (aka a camera not perpendicular to the map)
In Review:
Given a Vector3 position of a target on the map I would like to look at, and potentially given a distance, I want to calculate ahead of time what angle and position of the camera would be best so that i could get that target in view of the camera, but also but not have the camera see off of the map.
I guess my real problem with my current way is how do I take the top down rect that I have:
(xPos: targetPosition.x, yPos: targetPosition.y, Width: frustumWidth, Height: frustumHeight)
and skew the corners to get the four world point corners of the transformed "quadrilateral" because its not a rectangle anymore, as it would appear in rotated version of the camera
I am willing to concede potentially keeping all rotation to a maximum of 5 or 10 degrees about the x or z axes but I am still lacking a calculation that can even give me the skewed rect of what the camera will see if its rotated
So whether that is using a projection matrix perhaps to skew the top down view by an amount, or perhaps there is a formula for a perspective camera to determine this, or even if the way you might suggest has me redo the entire system I've created so far, I would very much appreciate any help, and I'm sure there will be other people down the road that would appreciate it as well! (:
What I Don't Want To Do (but will if there is a compelling reason to)
I don't want to set my camera and then make calculations and set it again and so on and so on, I want to calculate it ahead of time so I can just have my camera lerp there!
Here are a few links that are very close to what I've been looking for but I haven't been able to find anything about how to calculate ahead of time:
http://answers.unity3d.com/questions/486035/how-to-find-world-coordinates-of-screen-corners-wi.html
http://mathforum.org/library/drmath/view/55344.html
http://answers.unity3d.com/questions/564276/adjusting-returned-worldtoscreenpoint-for-perspect.html
Thanks again all!