9

この質問は以前に何度か聞かれたことを知っており、これに関するさまざまな投稿を読みました。しかし、私はこれを機能させるのに苦労しています。

    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;
    }
4

2 に答える 2

25

ポイントP(x,y)、および長方形A(x1,y1)B(x2,y2)C(x3,y3)、 としD(x4,y4)ます。

  • △APD△DPC△CPB、の面積の合計を計算し△PBAます。

  • この合計が長方形の面積より大きい場合:

    • 次に、ポイントP(x,y)は長方形の外側にあります。
    • それ以外の場合は、長方形の中または上にあります。

各三角形の面積は、次の式で座標のみを使用して計算できます。

3 つのポイントが次のとおりであると仮定します: A(x,y)B(x,y)C(x,y)...

Area = abs( (Bx * Ay - Ax * By) + (Cx * By - Bx * Cy) + (Ax * Cy - Cx * Ay) ) / 2
于 2013-06-17T11:26:15.517 に答える
1

それLocationが長方形の回転中心だと思います。そうでない場合は、適切な図で回答を更新してください。

やりたいことは、四角形のローカル システムでマウスの位置を表現することです。したがって、次のことができます。

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;
}

さらに、マウスが押された場合のチェックをメソッドの先頭に移動することもできます。押されていない場合は、変換などを計算する必要はありません。

于 2013-06-17T10:26:37.547 に答える