Sign in to follow this  
BoxyCraft

Camera Bounds [SOLVED]

Recommended Posts

BoxyCraft    127
I have a 3D camera which focuses on a 2D world, platformer style. I would like to prevent the camera from seeing outside the bounds of the world, at any distance. How can I do this? I am able to translate between world and screen coordinates, and get a Rect of the area of the world that is visible. My first idea was to translate that rect to the plane that the camera is on, but I'm not sure how to do that and still respect the aspect ratio. Thanks for any ideas and help! [Edited by - BoxyCraft on May 4, 2010 2:22:33 AM]

Share this post


Link to post
Share on other sites
jyk    2094
If you already know how to compute the area of the world currently visible to the camera, it seems you could do the following:

1. Compute the half-width of the 'visible area' rect (call it w).

2. Compute the left and right 'camera bounds' for the world as left_most_x + w, right_most_x - w.

3. Restrict the camera x value so that it always lies between these two values.

Note that if the camera 'backs up' while near a world boundary, its x position might need to be adjusted accordingly.

You would also need to impose a maximum camera distance to prevent the camera from backing up so far that areas outside the world bounds became visible. If you want to compute the exact distance at which this will occur in both the horizontal and vertical directions, it can easily be done with a little trig (you can then simply enforce that the camera never move farther than this distance away from the world).

Share this post


Link to post
Share on other sites
BoxyCraft    127
Thanks, it works perfectly!

Now, about this part, can you please point me in the right direction to calculating the max distance from the plane?

Quote:
You would also need to impose a maximum camera distance to prevent the camera from backing up so far that areas outside the world bounds became visible. If you want to compute the exact distance at which this will occur in both the horizontal and vertical directions, it can easily be done with a little trig (you can then simply enforce that the camera never move farther than this distance away from the world).


I found this article, and tried the following, but it didn't work:


Dim tempfov As Single = CSng(2 * Math.Atan(Math.Tan(fov / 2) * aspectRatio))
_maxz = (screenwidth * 0.5f) / CSng(Math.Tan(tempfov / 2))





Any help would be greatly appreciated, thanks!

[Edited by - BoxyCraft on May 4, 2010 2:43:35 AM]

Share this post


Link to post
Share on other sites
BoxyCraft    127
Sorry, I was being stupid, I used the screenwidth instead of the width of the world!

So, the max distance from the camera where the width of the visible area is equal to "maxwidth" is:


Dim tempfov As Single = CSng(2 * Math.Atan(Math.Tan(fov / 2) * aspectRatio))
_maxz = (maxwidth * 0.5f) / CSng(Math.Tan(tempfov / 2))



If you want to use the height instead of the width (depending on which is smaller) you would use your fov instead of tempfov which I put in above. The linked article explains it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this