After moving the camera to any direction, my cursor isn't getting drawn on the correct position. For example: I move my camera to the right, after that I click on the screen, but the cursor isn't getting drawn on the place I clicked. It's drawn somewhere else on the screen. Why is the cursor not getting drawn on the place I clicked?
I need to scale the screen because I want to support different resolutions in a MonoGame project(see my other question): How can I use a camera matrix with different resolutions?
I scale like this:
UPDATE: I added the code that I'm using to get the cursor position, and the code that I'm using to draw the cursor. In addition, I use now Matrix.Invert(scaleMatrix * camera.GetMatrix())), but it doesn't work. The cursor is still drawn on the wrong position.
//In my Player class, I get the position of the click(Tap)
//The Playerposition is the center of the camera
public void Update(GameTime gameTime)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gs = TouchPanel.ReadGesture();
switch (gs.GestureType)
{
case GestureType.HorizontalDrag:
Playerposition += new Vector2(gs.Delta.X / game1.scaleX, gs.Delta.Y / game1.scaleY);
break;
case GestureType.VerticalDrag:
Playerposition += new Vector2(gs.Delta.X / game1.scaleX, gs.Delta.Y / game1.scaleY);
break;
case GestureType.Tap:
game1.CursorPosition = new Rectangle((int)(gs.Position.X / game1.scaleX), (int)(gs.Position.Y / game1.scaleY), 10, 10);
game1.Clicked = true;
break;
}
}
}
//In Game1:
public const int VirtualScreenWidth = 800;
public const int VirtualScreenHeight = 480;
public float scaleX, scaleY;
private Vector3 _screenScale;
protected override void LoadContent()
{
scaleX = (float)GraphicsDevice.Viewport.Width / (float)VirtualScreenWidth;
scaleY = (float)GraphicsDevice.Viewport.Height / (float)VirtualScreenHeight;
_screenScale = new Vector3(scaleX, scaleY, 1.0f);
}
protected override void Update(GameTime gameTime)
{
player.Update(gameTime);
//Updating the camera position:
Newcameraposition = new Vector2(player.Playerposition.X - (float)VirtualScreenWidth / 2, player.Playerposition.Y - (float)VirtualScreenHeight / 2);
camera.Update(gameTime, Newcameraposition);
InvertCursorPos = Vector2.Transform(new Vector2(CursorPosition.X, CursorPosition.Y), Matrix.Invert(scaleMatrix * camera.GetMatrix()));
//Calculate the cursor position
if (Clicked == true)
{
CursorPosition = new Rectangle((int)(InvertCursorPos.X + Newcameraposition.X), (int)(InvertCursorPos.Y + Newcameraposition.Y), 10, 10);
Clicked = false;
}
base.Update(gameTime);
}
// Drawing
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
var scaleMatrix = Matrix.CreateScale(_screenScale);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, null, scaleMatrix * camera.GetMatrix());
//Drawing the cursor
spriteBatch.Draw(CursorSprite, CursorPosition, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
spriteBatch.End();