Wednesday, March 30, 2011

Setting up a 2D Game Camera in XNA

I actually took some of the code from a few websites, but this shit was incredibly hard to find, so i put together what worked for me for you guys :3c


The Camera2D class
-----------------------------
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

    public class Camera2d
    {
        protected float _zoom; // Camera Zoom
        public Matrix _transform; // Matrix Transform
        public Vector2 _pos; // Camera Position
        protected float _rotation; // Camera Rotation

        public Camera2d()
        {
            _zoom = 1.0f;
            _rotation = 0.0f;
            _pos = Vector2.Zero;
        }

        // Sets and gets zoom
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        // Auxiliary function to move the camera
        public void Move(Vector2 amount)
        {
            _pos += amount;
        }
        // Get set position
        public Vector2 Pos
        {
            get { return _pos; }
            set { _pos = value; }
        }

        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            _transform =       // Thanks to o KB o for this solution
              Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
                                         Matrix.CreateRotationZ(Rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                                         Matrix.CreateTranslation(new Vector3(/*Application Window Width*/ * 0.5f,
/*Application Window Height*/ * 0.5f, 0));
            return _transform;
        }
}

-----------------------------


Then you implement it as follows


-----------------------------
Camera2d camera = new Camera2d();

camera.Pos = //some vector2 that represents a position
camera.zoom = //some float that represents a zoom. anything > 1 will be "zoomed in" and anything < 1 will be "zoomed out"

-----------------------------


Then when you go to call the spritebatch.Begin function, you use the following overload


-----------------------------
spriteBatch.Begin(
                            SpriteSortMode.Immediate,
                            BlendState.AlphaBlend,
                            null,
                            null,
                            null,
                            null,
                            camera.get_transformation(this.GraphicsDevice));

-----------------------------


that should be it :D


NOTE:
Some things like the mouse position from Mouse.GetState() is not relative to the camera, so you have to add some logic like


-----------------------------
mouseLoc = new Vector2(curMouse.X, curMouse.Y) + (camera.Pos - new Vector2(ScreenWidth / 2, ScreenHeight / 2));
-----------------------------


to get the mouse position relative to the camera.

6 comments:

  1. Very nice coding. Make sure you post more. Will follow.

    ReplyDelete
  2. some more good posting. I recently got xna because one of my lecturers were talking about it and i wanted to have a look.

    ReplyDelete
  3. Quick question of mutual interest. What did you have to study uni wise to get into 2-d video games? It has interested me for a while but I haven't the first clue where to get started. Or even what resources are readily available. Some pointers would be nice!

    ReplyDelete
  4. @Killer salmon, im not even in a university yet, i graduate in a month from high school =] I made a new post that point out MANY sources to start :)

    ReplyDelete
  5. XNA is awesome, quality tools for smaller games.

    ReplyDelete