Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. /// <summary>
  2. /// Checks - and resolves - collisions with worldlines.
  3. /// Returns a list of vectors it has resolved upon.
  4. /// </summary>
  5. public virtual List<Vector2> CheckResolveCollision(BaseWorld world)
  6. {
  7.     List<Vector2> resolveNormal = new List<Vector2>();
  8.  
  9.     foreach (Line worldLine in world.lines)
  10.     {
  11.         int numOverlaps = 0;
  12.  
  13.         float currentOverlapMagnitude = 0;
  14.         Vector2 currentOverlapAxis = Vector2.Zero;
  15.  
  16.         bool minSet = false;
  17.  
  18.         Vector2 wLNormal = worldLine.normal;
  19.  
  20.         Tuple<float, float> minmaxPolyUseCheck = ProjectToAxis(wLNormal);
  21.         float minPolyUC = minmaxPolyUseCheck.Item1;
  22.         float maxPolyUC = minmaxPolyUseCheck.Item2;
  23.  
  24.         float wLinePoint = worldLine.ProjectToAxis(wLNormal).Item1;    //these are always going to be the same.
  25.  
  26.         float checkMagnitude = 0;
  27.  
  28.         if ((wLinePoint > minPolyUC && wLinePoint < maxPolyUC))
  29.         {
  30.             checkMagnitude = MathHelper.Min(Math.Abs(Math.Abs(minPolyUC) - Math.Abs(wLinePoint)), Math.Abs(Math.Abs(maxPolyUC) - Math.Abs(wLinePoint)));
  31.         }
  32.         else continue;
  33.  
  34.         foreach (Line polyLine in lines)
  35.         {
  36.             Vector2 currentNormal = polyLine.normal;
  37.  
  38.             Tuple<float, float> minmaxPoly = ProjectToAxis(currentNormal);
  39.             float minPoly = minmaxPoly.Item1;
  40.             float maxPoly = minmaxPoly.Item2;
  41.  
  42.             Tuple<float, float> minmaxWLine = worldLine.ProjectToAxis(currentNormal);
  43.             float minWLine = minmaxWLine.Item1;
  44.             float maxWLine = minmaxWLine.Item2;
  45.  
  46.             if (((maxWLine > minPoly && maxWLine >= maxPoly) && (minWLine <= minPoly && minWLine < maxPoly)) || ((minWLine > minPoly && minWLine < maxPoly) || (maxWLine >= minPoly && maxWLine <= maxPoly)))
  47.             {   //I have absolutely no idea why the above line works. Changing one little thing breaks it
  48.                 if (checkMagnitude < currentOverlapMagnitude || !minSet)
  49.                 {
  50.                     currentOverlapMagnitude = checkMagnitude;
  51.                     currentOverlapAxis = worldLine.normal;
  52.  
  53.                     minSet = true;
  54.                 }
  55.  
  56.                 numOverlaps++;
  57.             }
  58.             else break;
  59.         }
  60.  
  61.         if (numOverlaps >= lines.Count()) //Lines.count should also be = to number of distinct vertices.
  62.         {
  63.             Move(worldLine.normal * currentOverlapMagnitude);
  64.             resolveNormal.Add(worldLine.normal);
  65.         }
  66.     }
  67.  
  68.     return resolveNormal;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement