Search Unity

[SOLVED] Rect.Overlaps() weird behaviour

Discussion in 'UGUI & TextMesh Pro' started by NVriezen, May 18, 2019.

  1. NVriezen

    NVriezen

    Joined:
    Oct 5, 2015
    Posts:
    46
    Hello everyone,

    Currently I am working on an AR game.
    I ran into a problem with my UI.
    I have written a Drag N Drop system by using the EventTrigger class.
    OnEndDrag() I want to check if a rectangular image is overlapping with a DropArea.

    I got the system set up and it seemed to work great. As I had one image, the berry, which is 200 x 200 drop onto a DropArea which is 300 x 300.

    But using this system now for a bigger DropArea of 800 x 1250, it does not work as it should.
    In a radius of the size of the berry around the DropArea's anchor, the overlapping works as normal. But outside that radius, the overlapping only returns true above and on the right side of the DropArea's anchor point.
    But on the left and bottom side, it does not return true for overlap, even though it is clearly inside the rectangle.
    See below for the code

    WhatsApp Image 2019-05-18 at 16.35.25.jpeg

    Code (csharp):
    1.  
    2. public override void OnEndDrag(PointerEventData data)
    3.     {
    4.         rectTransform = GetComponent<RectTransform>();
    5.         Rect berryRect = new Rect(transform.position, rectTransform.sizeDelta);
    6.         for (int i = 0; i < dropAreas.Length; i++)
    7.         {
    8.             Debug.Log("Checking dropAreas");
    9.             Debug.Log(berryRect + " - overlap with " + dropRects[i]);
    10.             if (berryRect.Overlaps(dropRects[i]))
    11.             {
    12.                 Debug.Log("Is dropped on berry slot");
    13.                 if (dropAreas[i].OnDrop(this, typeObject))
    14.                 {
    15.                     return;
    16.                 }
    17.             }
    18.         }
    19.  
    20.         transform.SetParent(originalParent);
    21.         transform.localPosition = originalLocalPosition;
    22.     }
    23.  
     
  2. NVriezen

    NVriezen

    Joined:
    Oct 5, 2015
    Posts:
    46
    I fixed this in the mean time by using my own overlap function. I asked my teacher from school, he suggested this solution.
    So I suspect the function is broken somehow or one of the two Rects is upside down which causes weird behaviour.

    Code (csharp):
    1.  
    2. private bool RectOverlap(Rect firstRect, Rect secondRect)
    3.     {
    4.         if (firstRect.x + firstRect.width*0.5f < secondRect.x - secondRect.width*0.5f)
    5.         {
    6.             return false;
    7.         }
    8.         if (secondRect.x + secondRect.width * 0.5f < firstRect.x - firstRect.width * 0.5f)
    9.         {
    10.             return false;
    11.         }
    12.         if (firstRect.y + firstRect.height * 0.5f < secondRect.y - secondRect.height * 0.5f)
    13.         {
    14.             return false;
    15.         }
    16.         if (secondRect.y + secondRect.height * 0.5f < firstRect.y - firstRect.height * 0.5f)
    17.         {
    18.             return false;
    19.         }
    20.         return true;
    21.     }
    22.  
     
    JamesTheMains and FaiyazOfficial like this.
  3. Playsetler

    Playsetler

    Joined:
    Dec 2, 2020
    Posts:
    1
    Dolbaeb, chto ne vylozhil funkciyu?
     
  4. KarMa_PlaYzz

    KarMa_PlaYzz

    Joined:
    Nov 13, 2016
    Posts:
    3
    Code (CSharp):
    1. private bool RectOverlap(Rect firstRect, Rect secondRect)
    2.     {
    3.         if (firstRect.x + firstRect.width*0.5f < secondRect.x - secondRect.width*0.5f)
    4.         {
    5.             return false;
    6.         }
    7.         if (secondRect.x + secondRect.width * 0.5f < firstRect.x - firstRect.width * 0.5f)
    8.         {
    9.             return false;
    10.         }
    11.         if (firstRect.y + firstRect.height * 0.5f < secondRect.y - secondRect.height * 0.5f)
    12.         {
    13.             return false;
    14.         }
    15.         if (secondRect.y + secondRect.height * 0.5f < firstRect.y - firstRect.height * 0.5f)
    16.         {
    17.             return false;
    18.         }
    19.         return true;
    20.     }
    21.  
     
  5. FaiyazOfficial

    FaiyazOfficial

    Joined:
    Mar 29, 2018
    Posts:
    5
    :D
    Thanks alot.
     
  6. furtin7

    furtin7

    Joined:
    May 17, 2020
    Posts:
    1
    If the solution above doesn't work try this.
    Code (CSharp):
    1. private bool RectOverlap(RectTransform firstRect, RectTransform secondRect)
    2.     {
    3.         if (firstRect.position.x + firstRect.rect.width * 0.5f < secondRect.position.x - secondRect.rect.width * 0.5f)
    4.         {
    5.             return false;
    6.         }
    7.         if (secondRect.position.x + secondRect.rect.width * 0.5f < firstRect.position.x - firstRect.rect.width * 0.5f)
    8.         {
    9.             return false;
    10.         }
    11.         if (firstRect.position.y + firstRect.rect.height * 0.5f < secondRect.position.y - secondRect.rect.height * 0.5f)
    12.         {
    13.             return false;
    14.         }
    15.         if (secondRect.position.y + secondRect.rect.height * 0.5f < firstRect.position.y - firstRect.rect.height * 0.5f)
    16.         {
    17.             return false;
    18.         }
    19.         return true;
    20.     }
     
    Nizanth7 and JamesTheMains like this.