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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

What is wrong with this simple logic

Discussion in 'Scripting' started by RoughSpaghetti3211, Mar 19, 2018.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    I just want to sort angA, angB and angC from small to large. I cant see it please help:(

    Code (csharp):
    1.  
    2.             if (angA > angC) {
    3.                 int temp = _hexA;
    4.                 _hexA = _hexC;
    5.                 _hexC = temp;
    6.                 float tempAng = angA;
    7.                 angA = angC;
    8.                 angC = tempAng;
    9.             }
    10.             if (angA > angB) {
    11.                 int temp = _hexA;
    12.                 _hexA = _hexB;
    13.                 _hexB = temp;
    14.                 float tempAng = angA;
    15.                 angA = angB;
    16.                 angB = tempAng;
    17.             }
    18.             if (angB > angC) {
    19.                 int temp = _hexB;
    20.                 _hexB = _hexC;
    21.                 _hexC = temp;
    22.                 float tempAng = angB;
    23.                 angB = angC;
    24.                 angC = tempAng;
    25.             }
    26.             // If not true fail
    27.             Assert.IsTrue( angC < angB,  (" A " + angA + " B " +angB + " C " + angC));
    28.             Assert.IsTrue( angB < angA,  (" A " + angA + " B " +angB + " C " + angC));
    29.  
    30.  
    Assert fails (seem correct to me unless some precision error )and I can see my triangle has incorrect order
    Assertion failed: A 0 B 2.081964 C 4.158172
    Assertion failure. Value was False
    Expected: True
     

    Attached Files:

  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Did you write your own sort or while not use a sort that you can do on a collection?
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    For now I just need to verify the winding order is the issues but I cant get past the asset, why is it failing ? what am I missing. The print out is correct but the assert fails and the triangle is wound anti-clockwise.
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Here is the angle code , it rough but if anyone see anything please let me know

    Code (csharp):
    1.  
    2.             Vector3 first = hexes [_hexA].VertPos;
    3.             Vector3 second = hexes [_hexB].VertPos;
    4.             Vector3 third = hexes [_hexC].VertPos;
    5.             Vector3 origin = GetCenter (ref hexes);
    6.  
    7.             Vector3 vBase = origin - first;
    8.             Vector3 vNorm = origin;
    9.  
    10.             Vector3 vA = vNorm - first;    
    11.             vNorm = vNorm.normalized;
    12.             vBase = vBase.normalized;
    13.             vA = vA.normalized;
    14.             float angA = Mathf.Acos (Vector3.Dot (vBase, vA));
    15.             if (Vector3.Dot (vBase, vA) > 1)
    16.                 angA = -1;
    17.             if (Vector3.Dot (vBase, vA) < -1)
    18.                 angA = 0;
    19.             float dir = Vector3.Dot (vNorm, (Vector3.Cross (vBase, vA)));
    20.             if (dir < 0.0f)
    21.                 angA = Mathf.PI + (Mathf.PI - angA);
    22.  
    23.  
    24.             Vector3 vB = vNorm - second;    
    25.             vNorm = vNorm.normalized;
    26.             vBase = vBase.normalized;
    27.             vB = vB.normalized;
    28.             float angB = Mathf.Acos (Vector3.Dot (vBase, vB));
    29.             if (Vector3.Dot (vBase, vB) > 1)
    30.                 angB = -1;
    31.             if (Vector3.Dot (vBase, vB) < -1)
    32.                 angB = 0;
    33.             dir = Vector3.Dot (vNorm, (Vector3.Cross (vBase, vB)));
    34.             if (dir < 0.0f)
    35.                 angB = Mathf.PI + (Mathf.PI - angB);
    36.  
    37.  
    38.  
    39.             Vector3 vC = vNorm - third;    
    40.             vNorm = vNorm.normalized;
    41.             vBase = vBase.normalized;
    42.             vC = vC.normalized;
    43.             float angC = Mathf.Acos (Vector3.Dot (vBase, vC));
    44.             if (Vector3.Dot (vBase, vC) > 1)
    45.                 angC = -1;
    46.             if (Vector3.Dot (vBase, vC) < -1)
    47.                 angC = 0;
    48.             dir = Vector3.Dot (vNorm, (Vector3.Cross (vBase, vC)));
    49.             if (dir < 0.0f)
    50.                 angC = Mathf.PI + (Mathf.PI - angC);
    51.  
    52.  
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Ended up being a dump error in calculation
     
    Last edited: Mar 19, 2018
  6. nygren

    nygren

    Joined:
    Feb 13, 2014
    Posts:
    33
    Aren't the assert checks inverted?

    It seems the sorting is done so that angA is smallest, then angB, and angC the largest. The asserts however, check that angC is smaller than angB, and that angB is smaller than angA, i.e. it expects angA to be the largest, then angB and angC the smallest.