14

I know this question has been asked a few times before, and I have read various posts about this. However I am struggling to get this to work.

    bool isClicked()
    {
        Vector2 origLoc = Location;
        Matrix rotationMatrix = Matrix.CreateRotationZ(-Rotation);
        Location = new Vector2(0 -(Texture.Width/2), 0 - (Texture.Height/2));
        Vector2 rotatedPoint = new Vector2(Game1.mouseState.X, Game1.mouseState.Y);
        rotatedPoint = Vector2.Transform(rotatedPoint, rotationMatrix);

        if (Game1.mouseState.LeftButton == ButtonState.Pressed &&
            rotatedPoint.X > Location.X &&
            rotatedPoint.X < Location.X + Texture.Width &&
            rotatedPoint.Y > Location.Y &&
            rotatedPoint.Y < Location.Y + Texture.Height)
        {
            Location = origLoc;
            return true;
        }
        Location = origLoc;
        return false;
    }
Harvey
  • 319
  • 1
  • 4
  • 8
  • This is unverifiable without knowing the transform that was applied to the texture. Creating a new matrix is a code smell. You should use the matrix that was used originally on the texture. And not rotate the mouse position but rotate back the texture so it is a rectangle again. – Hans Passant Jun 17 '13 at 12:13
  • same as: https://stackoverflow.com/questions/28706989/how-do-i-check-if-a-mouse-click-is-inside-a-rotated-text-on-the-html5-canvas-in/56205468#56205468 – Mahipal May 19 '19 at 07:38

2 Answers2

35

Let point P(x,y), and rectangle A(x1,y1), B(x2,y2), C(x3,y3), D(x4,y4).

  • Calculate the sum of areas of △APD, △DPC, △CPB, △PBA.

  • If this sum is greater than the area of the rectangle:

    • Then point P(x,y) is outside the rectangle.
    • Else it is in or on the rectangle.

The area of each triangle can be calculated using only coordinates with this formula:

Assuming the three points are: A(x,y), B(x,y), C(x,y)...

Area = abs( (Bx * Ay - Ax * By) + (Cx * By - Bx * Cy) + (Ax * Cy - Cx * Ay) ) / 2
meallhour
  • 13,921
  • 21
  • 60
  • 117
Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53
2

I assume that Location is the rectangle's rotation center. If not, please update your answer with an appropriate figure.

What you want to do is expressing the mouse location in the local system of the rectangle. Therfore, you could do the following:

bool isClicked()
{
    Matrix rotationMatrix = Matrix.CreateRotationZ(-Rotation);
    //difference vector from rotation center to mouse
    var localMouse = new Vector2(Game1.mouseState.X, Game1.mouseState.Y) - Location;
    //now rotate the mouse
    localMouse = Vector2.Transform(localMouse, rotationMatrix);

    if (Game1.mouseState.LeftButton == ButtonState.Pressed &&
        rotatedPoint.X > -Texture.Width  / 2 &&
        rotatedPoint.X <  Texture.Width  / 2 &&
        rotatedPoint.Y > -Texture.Height / 2 &&
        rotatedPoint.Y <  Texture.Height / 2)
    {
        return true;
    }
    return false;
}

Additionally, you may want to move the check if the mouse is pressed to the beginning of the method. If it is not pressed, you don't have to calculate the transformation etc.

Nico Schertler
  • 32,049
  • 4
  • 39
  • 70