Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Basically, here we try to merge rectangles together to minimize CPU strain for all those little rectangles.
- //It currently only optimizes square regions. I have no idea what this algorithm is called so I'll call it...
- //--- Squarily Controlled Union Maker (SCUM) ---
- long preticks = DateTime.Now.Ticks;
- while (currentRectSize-- > 1)
- {
- for (uint i = 0; i < GridWidth; i++)
- {
- for (uint j = 0; j < GridHeight; j++)
- {
- uint right = i + currentRectSize;
- uint bottom = j + currentRectSize;
- if (right > GridWidth || bottom > GridHeight) continue;
- bool noHoles = true;
- for (uint ii = 0; ii < currentRectSize; ii++)
- {
- for (uint jj = 0; jj < currentRectSize; jj++)
- {
- uint x = i + ii;
- uint y = j + jj;
- if (gridListCopy[x, y] == null)
- {
- noHoles = false;
- break;
- }
- }
- if (!noHoles) break;
- }
- if (noHoles)
- {
- float size = PhysicsManager.ToSimUnits(currentRectSize * GridSize);
- Body body = BodyFactory.CreateRectangle(PhysicsManager.World, size, size, 1);
- body.Friction = 0;
- body.CollidesWith = Category.None;
- body.Position = MathStuff.VectorXNA(PhysicsManager.ToSimUnits(new Vector2((i - 0.5f + currentRectSize / 2f) * GridSize, (j - 0.5f + currentRectSize / 2f) * GridSize)));
- MainBodies.Add(body);
- for (uint ii = 0; ii < currentRectSize; ii++)
- {
- for (uint jj = 0; jj < currentRectSize; jj++)
- {
- uint x = i + ii;
- uint y = j + jj;
- gridListCopy[x, y] = null; //Mark the space the current rect uses as occupied so that no other rects will try to use this space
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment