3
\$\begingroup\$

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();
\$\endgroup\$
2
  • \$\begingroup\$ Add the code you're using to get the cursor position, and the code you're using to draw the cursor. \$\endgroup\$ Apr 23, 2014 at 14:59
  • \$\begingroup\$ I added the code. Is something wrong with it? \$\endgroup\$ Apr 23, 2014 at 20:34

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ I tried your code, but it's not working. The cursor is still drawn on the wrong position. \$\endgroup\$ Apr 23, 2014 at 20:33
  • \$\begingroup\$ @Homer_Simpson Try Invert(scaleMatrix * camera.GetMatrix()). The Invert method is what you want to use - you just need to get the maths right. \$\endgroup\$ Apr 24, 2014 at 6:56
  • \$\begingroup\$ It's not working with Matrix.Invert(scaleMatrix * camera.GetMatrix())). \$\endgroup\$ Apr 24, 2014 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .