Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on a Camera class and I have a rectangle field named Bounds that determines the bounds of the camera. I have it working for zooming and moving the camera so that the camera cannot exit its bounds.

However, I'm a bit confused on how to do the same for rotation. Currently I allow rotating of the camera's Z-axis. However, if sufficiently zoomed out, upon rotating the camera, areas of the screen outside the camera's bounds can be shown.

I'd like to deny the rotation assuming it meant that the newly rotated camera would expose areas outside the camera's bounds, but I'm not quite sure how. I'm still new to Matrix and Vector math and I'm not quite sure how to model if the newly rotated camera sees outside of its bounds, undo the rotation. Here's an image showing the problem:

http://i.stack.imgur.com/NqprC.png

The red is out of bounds and as a result, the camera should never be allowed to rotate itself like this. This seems like it would be a problem with all rotated values, but this is not the case when the camera is zoomed in enough.

Here are the current member variables for the Camera class:

private Vector2 _position = Vector2.Zero;
private Vector2 _origin = Vector2.Zero;
private Rectangle? _bounds = Rectangle.Empty;

private float _rotation = 0.0f;
private float _zoom = 1.0f;

Is this possible to do? If so, could someone give me some guidance on how to accomplish this? Thanks.

EDIT: I forgot to mention I am using a transformation matrix style camera that I input in to SpriteBatch.Begin. I am using the same transformation matrix from this tutorial.

share|improve this question

1 Answer 1

I've written an article before called Limiting 2D Camera Movement with Zoom which shows you how to translate and zoom the camera freely without never letting it show beyond some predefined bounds. Whenever you move or zoom the camera, its position and zoom are automatically validated. Unfortunately, I did not cover rotation in that article because the math required would be a lot trickier.

However, that's only because instead of simply undoing the movement to the latest known valid orientation, I was actually taking care of clamping it back to the closest valid orientation instead, and I haven't figured out how to do that for rotation.

But in your case, if you only care about undoing the rotation whenever the camera sees outside its bounds, then it's actually pretty easy to do! Here's a step by step guide on how to implement that.


Step 1

Assign the new camera rotation but keep track of the old rotation value too. If you cache your view matrix, be sure to update it because it will be needed for the next step. Example:

float previousRotation = _rotation;
_rotation = value;

Step 2

Calculate the positions of the four corners of the camera in world space. In order to do that you only need to transform each of the viewport's corners (i.e. in view space) by the inverse view matrix. Example:

// Calculate the inverse of the view matrix
Matrix inverseView = Matrix.Invert(ViewMatrix);

// Storing viewport size just to make code below smaller
float vh = viewport.Height;
float vw = viewport.Width;

// Calculate four corners in world space
Vector2 tl = Vector2.Transform(new Vector2(0, 0), inverseView);
Vector2 tr = Vector2.Transform(new Vector2(vw, 0), inverseView);
Vector2 bl = Vector2.Transform(new Vector2(0, vh), inverseView);
Vector2 br = Vector2.Transform(new Vector2(vw, vh), inverseView);

Step 3

Revert back to the old rotation if any of the corners lies outside of the camera's bounds. Example:

if(!bounds.Contains(tl) || !bounds.Contains(tr) || !bounds.Contains(bl) || !bounds.Contains(br)) 
{
    _rotation = previousRotation;
}

Note: XNA's Rectangle stores ints, not floats, so you'll have to convert between Vector2 and Point (and cast the vector components to ints) to use Contains or use a RectangleF structure such as this instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.