Obtain rectangle indicating 2D world space camera can see

Posted by Gareth on Game Development See other posts from Game Development or by Gareth
Published on 2013-10-17T17:49:58Z Indexed on 2013/10/17 22:22 UTC
Read the original article Hit count: 269

Filed under:
|
|
|
|

I have a 2D tile based game in XNA, with a moveable camera that can scroll around and zoom.

I'm trying to obtain a rectangle which indicates the area, in world space, that my camera is looking at, so I can render anything this rectangle intersects with (currently, everything is rendered).

So, I'm drawing the world like this:

  _SpriteBatch.Begin(
                SpriteSortMode.FrontToBack,
                null,
                SamplerState.PointClamp,        // Don't smooth            
                null, null, null,
                _Camera.GetTransformation());   

The GetTransformation() method on my Camera object does this:

    public Matrix GetTransformation()
    {
        _transform =
           Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
           Matrix.CreateRotationZ(Rotation) *
           Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
           Matrix.CreateTranslation(new Vector3(_viewportWidth * 0.5f,
               _viewportHeight * 0.5f, 0));
        return _transform;
    }

The camera properties in the method above should be self explanatory.

How can I get a rectangle indicating what the camera is looking at in world space?

© Game Development or respective owner

Related posts about XNA

Related posts about c#