Guest User

Untitled

a guest
Dec 9th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1.             //Basically, here we try to merge rectangles together to minimize CPU strain for all those little rectangles.
  2.             //It currently only optimizes square regions. I have no idea what this algorithm is called so I'll call it...
  3.             //--- Squarily Controlled Union Maker (SCUM) ---
  4.  
  5.             long preticks = DateTime.Now.Ticks;
  6.             while (currentRectSize-- > 1)
  7.             {
  8.                 for (uint i = 0; i < GridWidth; i++)
  9.                 {
  10.                     for (uint j = 0; j < GridHeight; j++)
  11.                     {
  12.                         uint right = i + currentRectSize;
  13.                         uint bottom = j + currentRectSize;
  14.                         if (right > GridWidth || bottom > GridHeight) continue;
  15.  
  16.                         bool noHoles = true;
  17.  
  18.                         for (uint ii = 0; ii < currentRectSize; ii++)
  19.                         {
  20.                             for (uint jj = 0; jj < currentRectSize; jj++)
  21.                             {
  22.                                 uint x = i + ii;
  23.                                 uint y = j + jj;
  24.                                 if (gridListCopy[x, y] == null)
  25.                                 {
  26.                                     noHoles = false;
  27.                                     break;
  28.                                 }
  29.                             }
  30.  
  31.                             if (!noHoles) break;
  32.                         }
  33.  
  34.                         if (noHoles)
  35.                         {
  36.                             float size = PhysicsManager.ToSimUnits(currentRectSize * GridSize);
  37.  
  38.                             Body body = BodyFactory.CreateRectangle(PhysicsManager.World, size, size, 1);
  39.                             body.Friction = 0;
  40.                             body.CollidesWith = Category.None;
  41.                             body.Position = MathStuff.VectorXNA(PhysicsManager.ToSimUnits(new Vector2((i - 0.5f + currentRectSize / 2f) * GridSize, (j - 0.5f + currentRectSize / 2f) * GridSize)));
  42.  
  43.                             MainBodies.Add(body);
  44.                             for (uint ii = 0; ii < currentRectSize; ii++)
  45.                             {
  46.                                 for (uint jj = 0; jj < currentRectSize; jj++)
  47.                                 {
  48.                                     uint x = i + ii;
  49.                                     uint y = j + jj;
  50.                                     gridListCopy[x, y] = null; //Mark the space the current rect uses as occupied so that no other rects will try to use this space
  51.  
  52.                                 }
  53.                             }
  54.                         }
  55.                     }
  56.                 }
  57.             }
Add Comment
Please, Sign In to add comment