Search Unity

Get resulting bounds from intersection

Discussion in 'Physics' started by RavenTravelStudios, Aug 10, 2017.

  1. RavenTravelStudios

    RavenTravelStudios

    Joined:
    Oct 15, 2015
    Posts:
    100
    Hi everyone,
    lets say i have two boxcollider intersecting each other (including any possible rotation) - how could i retrieve the center of the intersecting area?

    Thank you so much! :)


    upload_2017-8-10_23-42-39.png
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    RavenTravelStudios likes this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    You may try Physics.ComputePenetration. Given two intersecting colliders this function computes the minimal translation required to separate them apart. The result is a distance and a direction vector. Maybe the half-way the distance is an useful point in your case.
     
    RavenTravelStudios likes this.
  4. RavenTravelStudios

    RavenTravelStudios

    Joined:
    Oct 15, 2015
    Posts:
    100
    Both answers are good, unfortunately i cannot use contacts because im not using the OnCollision events, but otherwise that would be useful. I think ComputePenetration is what i need :)
     
  5. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    I would suggest to use raw math to solve, especially for box colliders
     
  6. UrbanNuke

    UrbanNuke

    Joined:
    Jun 11, 2019
    Posts:
    21
    If it will be actual for someone

    Code (CSharp):
    1.  
    2. Bounds bounds1 = new Bounds(new Vector3(0, 0, 0), new Vector3(2, 2, 2));
    3. Bounds bounds2 = new Bounds(new Vector3(1, 1, 1), new Vector3(3, 3, 3));
    4.  
    5. Bounds intersectionBounds = new Bounds();
    6. if (bounds1.Intersects(bounds2))
    7. {
    8.     intersectionBounds.SetMinMax(
    9.         Vector3.Max(bounds1.min, bounds2.min),
    10.         Vector3.Min(bounds1.max, bounds2.max)
    11.     );
    12. }
    13.  
    14. Debug.Log("New Bounding Box Size: " + intersectionBounds.size);
    15.  
     
    a436t4ataf and valentin56610 like this.
  7. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    156
    It is, thank you very much, exactly what I was looking for after chat GPT wrote some S***ty code :)