[ad_1]
I’ve applied marching sqaures some time in the past and now I’m in want of turning it right into a polygon for collision..
My problem is with the borders, I am unable to work out tips on how to add vertices alongside the borders of the grid so the vertices may be related into one polygon.
so that is how one chunk appears to be like like in my sport presently:
And right here is how I need them to seem like:
Right here is my code:
public void Replace() {
if (!IsEmpty) {
verts.Clear();
for (int i = 0; i < Information.Width; i++) {
for (int j = 0; j < Information.Peak; j++) {
byte sort = (byte)LineType(world.GetSquare(i + PositionX, j + PositionY, grid), world.GetSquare(i + PositionX + 1, j + PositionY, grid), world.GetSquare(i + PositionX + 1, j + PositionY + 1, grid), world.GetSquare(i + PositionX, j + PositionY + 1, grid));
varieties[World.GetSquareIndex(i, j)] = sort;
}
}
for (int i = 0; i < Information.Width; i++) {
for (int j = 0; j < Information.Peak; j++) {
int x = (i + PositionX) * Information.SquareSize;
int y = (j + PositionY) * Information.SquareSize;
Vector2 a = new Vector2(x + Information.SquareSize * 0.5f, y);
Vector2 b = new Vector2(x + Information.SquareSize, y + Information.SquareSize * 0.5f);
Vector2 c = new Vector2(x + Information.SquareSize * 0.5f, y + Information.SquareSize);
Vector2 d = new Vector2(x, y + Information.SquareSize * 0.5f);
byte sort = varieties[World.GetSquareIndex(i, j)];
change (sort) {
case 1:
verts.Add(c);
verts.Add(d);
break;
case 2:
verts.Add(b);
verts.Add(c);
break;
case 3:
verts.Add(b);
verts.Add(d);
break;
case 4:
verts.Add(a);
verts.Add(b);
break;
case 5:
verts.Add(a);
verts.Add(d);
verts.Add(b);
verts.Add(c);
break;
case 6:
verts.Add(a);
verts.Add(c);
break;
case 7:
verts.Add(a);
verts.Add(d);
break;
case 8:
verts.Add(a);
verts.Add(d);
break;
case 9:
verts.Add(a);
verts.Add(c);
break;
case 10:
verts.Add(a);
verts.Add(b);
verts.Add(c);
verts.Add(d);
break;
case 11:
verts.Add(a);
verts.Add(b);
break;
case 12:
verts.Add(b);
verts.Add(d);
break;
case 13:
verts.Add(b);
verts.Add(c);
break;
case 14:
verts.Add(c);
verts.Add(d);
break;
}
}
}
}
}
[ad_2]