Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Would love to have int3 == int3 return a single bool

Discussion in 'Entity Component System' started by 8bitgoose, Apr 16, 2020.

  1. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    449
    As of now, if we compare two int3s with the implicit operator it returns a bool3, rather than a bool. I'd like to be able to easily compare two integers to make sure they are exactly the same without writing an extension method.

    Any reason not to have this in the math library?
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    if not mistaken you can do something like: math.bool3(true) == (int3 == int3)
     
  3. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    449
    @brunocoimbra I tried that and it didn't work. Even if it did, doesn't make writing it any easier. :p

    I just wrote my own function

    Code (CSharp):
    1.  
    2.        public static bool equal(int3 p1, int3 p2)
    3.        {
    4.            if(p1.x == p2.x && p1.y == p2.y && p1.z == p2.z)
    5.                return true;
    6.  
    7.            return false;
    8.        }
    But writing mathf.equals(p1, p2) is way more annoying than p1 == p2.
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    math.all(p1 == p2)
     
  5. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    449
    Thanks @DreamingImLatios, I had no idea that function existed.

    Would still love bool newThing = p1 == p2 so I won't have to add a random function in there :).
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    I know why you want that, but doing that would break math.select functionality using simd.
     
  7. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    449
    Ah I see, I did not know why that wasn't in there. Excuse my ignorance, but what does math.select do?
     
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,993
    math.select lets you pick one of two values per component based on the boolean, also per component.
    Code (CSharp):
    1. public static GjkSupportPoint GetSupportPoint(BoxCollider box, float3 direction)
    2.         {
    3.             bool3 signs = direction > 0f;
    4.             return new GjkSupportPoint
    5.             {
    6.                 pos = box.center + math.select(-box.halfSize, box.halfSize, signs),
    7.                 id  = (uint)math.bitmask(new bool4(signs, false)),
    8.             };
    9.         }
     
  9. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Guys, simply use
    a.Equals(b)

    This resolves into a direct call of the following method:
    Code (CSharp):
    1. /// <summary>Returns true if the float3 is equal to a given float3, false otherwise.</summary>
    2. [MethodImpl(MethodImplOptions.AggressiveInlining)]
    3. public bool Equals(float3 rhs) { return x == rhs.x && y == rhs.y && z == rhs.z; }
    Code (CSharp):
    1. IL_00c6: ldloca.s     a
    2. IL_00c8: ldloc.s      b
    3. IL_00ca: call         instance bool [Unity.Mathematics]Unity.Mathematics.float3::Equals(valuetype [Unity.Mathematics]Unity.Mathematics.float3)
    4. IL_00cf: stloc.s      result
     
  10. Cynicat

    Cynicat

    Joined:
    Jun 12, 2013
    Posts:
    290
    It's designed to work with boolean combiners, you can use

    if (math.all(myInt3 == myOtherInt3)) { ... }
    fun fact there is also an any() function:
    if (math.any(myInt3 == myOtherInt3)) { ... }

    all() and any() also have overloads for checking if various vectors are non-zero. =3
     
    Blueprinter likes this.
  11. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    should be one instruction after burst ;)