I am pretty new to XNA framework For now I can do drawing, animations and so, But I have encountered a problem with Camera displaying cursor.
I want to achieve a 2D game where you use cursor as main controller (selecting, moving on map and so). I want the camera to be controlled by cursor Right-click and drag.
I have created very simple logic for moving it, but it's not working. Anytime I press RButton and drag it, camera is working well.
The problem pop up when do it again. Whole View resets to initial position. I guess the problem is with mouse position relative to world.
I am adding my camera Class
class TCCamera : IDrawable
{
public Matrix transformation;
Viewport viewport;
Vector2 centre;
public TCCamera(Viewport vport)
{
viewport = vport;
this.centre = new Vector2();
}
public void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content)
{
}
public void Draw(SpriteBatch spriteBatch)
{
throw new NotImplementedException();
}
public Matrix Translate
{
get
{
return this.transformation;
}
}
public void Update(GameTime gameTime)
{
MouseState mstat = Mouse.GetState();
if (mstat.RightButton == ButtonState.Pressed)
{
this.centre.X = mstat.X;
this.centre.Y = mstat.Y;
transformation = Matrix.CreateTranslation(new Vector3(-centre.X, -centre.Y, 0));
}
}
}
}
And also the Cursor Class
class TCCursor : IDrawable
{
Texture2D tex;
public Vector2 pos;
Rectangle bBox;
public TCCursor()
{
this.pos = new Vector2();
this.bBox = new Rectangle(0, 0, 50, 50);
}
public void LoadContent(ContentManager Content)
{
tex = Content.Load<Texture2D>("cursor");
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(tex, bBox, Color.White);
}
public void Update(GameTime gameTime)
{
MouseState value = Mouse.GetState();
this.pos.X = value.X;
this.pos.Y = value.Y;
this.bBox.X = value.X;
this.bBox.Y = value.Y;
}
}
And also I am drawing as
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.AlphaBlend,
null, null, null, null, camera.Translate);
I still cannot completely understand the concept of World and View Matrices and their Math.
After a while I have found the solution using inverse matrices. I have found this extremly useful: Tutorial XNA Camera