Hey, I was wondering if anyone here who really understands Linear Algebra or game Cameras can help me with figuring out how to modify the "Transform Matrix" I pass to spriteBatch to zoom in or zoom out, or in other words make all things on the screen larger or smaller. This is for a 2D game, so I don't have any advanced 3D code going on. Here is my code sample, so you can get a better understanding of what I'm trying to say:
| public class Viewport |
| { |
| public static Matrix TMatrix { |
| get { |
| return Matrix.CreateTranslation( |
640 - position.X, 400 - position.Y, 0); // 640 and 400 are half of the game screen's width and height.
|
| } |
| private set { |
| } |
| } |
| |
| private static Vector2 position = Vector2.Zero; |
| |
| public static Vector2 Get() |
| { |
| return position; |
| } |
| public static void Set(Vector2 pos) |
| { |
| position = pos; |
| } |
| public static void Add(Vector2 pos) |
| { |
| position += pos; |
| } |
| } |
That is a static class I pass in Vector2's so that it will give me back a Matrix when I request it.
| spriteBatch.Begin( |
| SpriteBlendMode.AlphaBlend, |
| SpriteSortMode.Deferred, |
| SaveStateMode.None, |
| Viewport.TMatrix); |
| |
| spriteBatch.Draw(origin, Vector2.Zero, Color.White); |
| |
| spriteBatch.End(); |
Having that "Viewport" class then allows me to draw a sprite with position (0, 0) at the center of the screen instead of me having to change its position of that sprite. When I have a lot of sprites in all sorts of locations, this method has allowed me to scroll the screen without having to mess with every sprite object's positions. All I needed to do is Viewport.Set or Viewport.Add a Vector2 with a new position I want to set as the center position. Its very convenient.
Now I'd like to know if there is a way that I can modify the TMatrix from my "Viewport" class so that it will give me the effect of zooming in or out the screen. The thing is, I've already tried setting the third value in Matrix.CreateTranslation() into something other than zero or one, but then all my sprites seemingly dissapear. The real issue here is that I have not understood what this Matrix really is. If someone here can show me how to modify some Z value or add or multiply another Matrix onto that matrix for making zoom in/out, then I would greatly appreciate it.
Thank you, in advance.