Multiple 2D camera's in XNA

Started by
-1 comments, last by omniscient232 12 years, 2 months ago
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,
null,
null,
null,
null,
getTransformation());

level.draw(spriteBatch, this);
spriteBatch.End();
}
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)) *
Matrix.CreateTranslation(lorigin);
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?

This topic is closed to new replies.

Advertisement