This is how i draw that Map
for (int y = 0; y < MapHeight; y++){for (int x = 0; x < MapWidth; x++){Rectangle DrawRect = new Rectangle();DrawRect.X = (x * TileWidth) -(int)camera.X;DrawRect.Y = (y * TileHeight) -(int)camera.Y;DrawRect.Width = TileWidth;DrawRect.Height = TileHeight;Batch.Draw(d_textureDict[Index], DrawRect, Color.White);
So, I am essentially moving the map by subtracting the CameraPosition. It works properly if I use the Keyboard to move the map left right up and down etc.
This is all of my Input Method
float acceleration = 0.1f; #region MOVEMENT if(Input.Keyboard.IsKeyDown(Keys.Up, false)) TestPlayer.ChangeSpeed(acceleration); else if(Input.Keyboard.IsKeyDown(Keys.Down, false)) TestPlayer.ChangeSpeed(-acceleration); TestPlayer.MoveToMouse(Input.Mouse.Position);// See Below #endregion #region Camera Control CameraPosition.X = TestPlayer.Center.X; CameraPosition.Y = TestPlayer.Center.Y; #endregion CameraPosition.X = MathHelper.Clamp(CameraPosition.X, 0, currentGameMap.PixelWidth - WindowRez.X); CameraPosition.Y = MathHelper.Clamp(CameraPosition.Y, 0, currentGameMap.PixelHeight - WindowRez.Y);
public void MoveToMouse(Point MousePosition) { /*Todo: * 1. Find out how much we need to rotate * 2. Find out what the Vector is and Normalise it */ //1. Vector2 StartDir = new Vector2(0, -1); float rot = (float)Math.Atan2(MousePosition.Y - Center.Y, Center.X - MousePosition.X); rot = -(float)(rot + MathHelper.ToRadians(90.0f)); PlayerStatistics.Rotation = rot; //2. Matrix rotMat = Matrix.CreateRotationZ(rot); Vector2 MoveDir = Vector2.Transform(StartDir, rotMat); MoveDirection = MoveDir; }
In this function I set the Movement direction. Then to get the velocity i multiply the speed by the Movement Direction