Jump to content
  • Advertisement
Sign in to follow this  

Multiple 2D camera's in XNA

This topic is 2812 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've succesfully implemented a 2D camera class in XNA , and now I want to implement the possibility to have 2, 3 or 4 camera's, that I can resize and place on the screen at will.

These are the relevant functions of my current camera:

internal void Draw(SpriteBatch spriteBatch, Level level)
if (!activated) { return true; }
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend,

level.draw(spriteBatch, this);
public Matrix getTransformation(GraphicsDevice graphicsDevice = null)
Vector3 lpos = new Vector3(-position.X, -position.Y, 0);
Vector3 lorigin = -(new Vector3(-screenSize, 0) * 0.5f);
transform =
Matrix.CreateTranslation(lpos) *
Matrix.CreateRotationZ(rotation * (float)(Math.PI / 180)) *
Matrix.CreateScale(new Vector3(zoom, zoom, 1)) *
return transform;

the getTransformation function returns a matrix, based on the position, zoom and rotation of the camera. in level.Draw, all the Texture2D's are drawn on their world positions.
This completely works with one camera, but now I'm trying to implement multiple camera's

I want to be able to set the size of the image of the camera that it takes on screen, and it's position on screen. So that I can do things like: have a small zoomed out camera inj the corner of the screen, or a zoomed in camera in the middle.

I already just tried draw a second camera over it, but there are some problems
I have two questions about this:
1. How do I make sure that the small camera in the corner does not overdraw the main camera?
2. Is it a good idea to just have each camera do : spriteBatch begin and end, so that spritebatch will begin and end 4 times a frame if there are 4 camera's?
3. How do I determine the position on screen the camera is drawn on?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!