Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();
share|improve this question
    
Add the code you're using to get the cursor position, and the code you're using to draw the cursor. –  Andrew Russell Apr 23 at 14:59
    
I added the code. Is something wrong with it? –  Homer_Simpson Apr 23 at 20:34
add comment

1 Answer 1

up vote 0 down vote accepted

You most likely need to convert your screen coordinates into your game world coordinates.

Vector2 pos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); 
pos = Vector2.Transform(pos, Matrix.Invert(camera.GetMatrix()));

Line 1 gets the current mouse position

Line 2 converts the current mouse position, into your world coordinates, based on the camera transformation matrix you are using to draw.

share|improve this answer
    
I tried your code, but it's not working. The cursor is still drawn on the wrong position. –  Homer_Simpson Apr 23 at 20:33
    
@Homer_Simpson Try Invert(scaleMatrix * camera.GetMatrix()). The Invert method is what you want to use - you just need to get the maths right. –  Andrew Russell Apr 24 at 6:56
    
It's not working with Matrix.Invert(scaleMatrix * camera.GetMatrix())). –  Homer_Simpson Apr 24 at 16:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.