Detinator10
Detinator10

Reputation: 123

Why does my game interpret the mouse pointers coordinates wrong

I have an monogame (2D) game I'm making and when I try to get the mouse coordinates they are wrong. I have no idea what the issue is but here is my code where I get the coordinates:

MouseState mouseState;
mouseState = Mouse.GetState();
test = new Tower(TowerTexture, new Vector2(mouseState.X, mouseState.Y));
//test is drawn where mouse pointer is thought to be and it is drawn off

Here is the tower drawing code:

    foreach (Tower tower in towers)
    {
        tower.Draw(spriteBatch);
    }

And here is the draw function for the tower:

public virtual void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(texture, center, null, Color.White, rotation,
    origin, 1.0f, SpriteEffects.None, 0);
}

One more thing is that as the mouse pointer, the real one is closer, to the upper left corner the offset of the supposed mouse coordinates is less but as you go closer and closer to the lower right corner of the screen the supposed mouse coordinates are farther off. I honestly have no idea what's wrong but any thoughts on what might be wrong would be appreciated. Thank you!

Upvotes: 0

Views: 473

Answers (2)

davidsbro
davidsbro

Reputation: 2758

This is in response to both this question and your question on hold. To fix this problem, you can scale down your image when you draw it. I'm not quite sure what the value of center is, but my guess is that it is a rectangle with its center at the mouse pointer. To scale down the image, try something like this:

Rectangle center;

public Tower(Texture2D TowerTexture, Vector2 location)
{
    float scaledown = 10;
    float XOffset = TowerTexture.Width / (2 * scaledown); //get an X and Y offset to center the image in the rectangle
    float YOffset = TowerTexture.Height / (2 * scaledown);
    this.center = new Rectangle(location.X + XOffset, location.Y + YOffset, 
        XOffset * 2, YOffset * 2);
}

Then Draw this image like you did previously, using center as the destination rectangle. I wrote this code without a compiler or debugging, but I think it should give you a basic idea. HTH

Upvotes: 1

Detinator10
Detinator10

Reputation: 123

What is wrong is that the resolution of my image is bigger than the resolution of my screen.

Upvotes: 0

Related Questions