Пример #1
0
    // 矩形との当たり判定
    public bool HitTest(ShapeRect other)
    {
        // 矩形の左上頂点
        Vector2 rect_vertex1 =
            new Vector2(other.transform.position.x - (other.transform.localScale.x / 2.0f), other.transform.position.y - (other.transform.localScale.y / 2.0f));

        // 矩形の右下頂点
        Vector2 rect_vertex2 =
            new Vector2(other.transform.position.x + (other.transform.localScale.x / 2.0f), other.transform.position.y + (other.transform.localScale.y / 2.0f));

        // 矩形の横幅 + 円のサイズ分の矩形との当たり判定
        if (rect_vertex1.x - radius <= transform.position.x &&
            transform.position.x <= rect_vertex2.x + radius &&
            rect_vertex1.y <= transform.position.y &&
            transform.position.y <= rect_vertex2.y)
        {
            return(true);
        }
        // 矩形の縦幅 + 円のサイズ分の矩形との当たり判定
        if (rect_vertex1.x <= transform.position.x &&
            transform.position.x <= rect_vertex2.x &&
            rect_vertex1.y - radius <= transform.position.y &&
            transform.position.y <= rect_vertex2.y + radius)
        {
            return(true);
        }

        // 左上頂点
        float distance = Mathf.Sqrt(Mathf.Pow(transform.position.x - rect_vertex1.x, 2) + Mathf.Pow(transform.position.y - rect_vertex1.y, 2));

        if (distance <= radius)
        {
            return(true);
        }
        // 右上頂点
        distance = Mathf.Sqrt(Mathf.Pow(transform.position.x - rect_vertex2.x, 2) + Mathf.Pow(transform.position.y - rect_vertex1.y, 2));
        if (distance <= radius)
        {
            return(true);
        }
        // 左下頂点
        distance = Mathf.Sqrt(Mathf.Pow(transform.position.x - rect_vertex1.x, 2) + Mathf.Pow(transform.position.y - rect_vertex2.y, 2));
        if (distance <= radius)
        {
            return(true);
        }
        // 右下頂点
        distance = Mathf.Sqrt(Mathf.Pow(transform.position.x - rect_vertex2.x, 2) + Mathf.Pow(transform.position.y - rect_vertex2.y, 2));
        if (distance <= radius)
        {
            return(true);
        }

        return(false);
    }
Пример #2
0
    public bool HitTest(ShapeRect other)
    {
        // 2つの矩形のサイズの半分ずつの合計
        float sum_x = (size.x / 2.0f) + (other.size.x / 2.0f);
        float sum_y = (size.y / 2.0f) + (other.size.y / 2.0f);
        // 2つの矩形の距離
        Vector2 distance = new Vector2(Mathf.Abs(other.transform.position.x - transform.position.x), Mathf.Abs(other.transform.position.y - transform.position.y));

        // 2つの矩形の距離がサイズの合計より小さい場合、当たっている
        if (distance.x <= sum_x && distance.y <= sum_y)
        {
            return(true);
        }
        return(false);
    }
Пример #3
0
        public void UpdateCursor()
        {
            if (cursorShape == null || cursorSnapShape == null)
            {
                cursorShape           = new ShapeRect();
                cursorSnapShape       = new ShapeRect();
                cursorShape.width     = LinePropertyUI.instance.GetLineWidth(LinePropertyTarget.Cursor);
                cursorShape.color     = LinePropertyUI.instance.GetColor(LinePropertyTarget.Cursor);
                cursorSnapShape.width = LinePropertyUI.instance.GetLineWidth(LinePropertyTarget.CursorSnap);
                cursorSnapShape.color = LinePropertyUI.instance.GetColor(LinePropertyTarget.CursorSnap);
            }

            if (ModContent.GetInstance <TeraCADConfig>().isDrawCursor)
            {
                cursorShape.pointStart = Main.MouseWorld.ToTileCoordinates().ToVector2() * 16;
                cursorShape.pointEnd   = cursorShape.pointStart.Offset(ModUtils.tileSize, ModUtils.tileSize);
            }

            if (ModContent.GetInstance <TeraCADConfig>().isDrawCursorSnap)
            {
                cursorSnapShape.pointStart = Snap.GetSnapPoint(ToolBox.snapType).Offset(-2, -2);
                cursorSnapShape.pointEnd   = cursorSnapShape.pointStart.Offset(4, 4);
            }
        }
Пример #4
0
 public override bool PointInObject(Point point)
 {
     return(ShapeRect.Contains(point));
 }