• Advertisement
Sign in to follow this  

Camera Bounds [SOLVED]

This topic is 2817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement