Fixed infinite loop when querying CellsForLine.

WorldChunkRefactoring
Martin Felis 2023-08-30 08:53:49 +02:00
parent 83265f5838
commit 1e7e07f349
3 changed files with 17 additions and 15 deletions

View File

@ -294,10 +294,12 @@ public class HexGrid : Resource
List<HexCell> result = new List<HexCell>(); List<HexCell> result = new List<HexCell>();
HexCell toCell = GetHexAt(toPlane); HexCell toCell = GetHexAt(toPlane);
Vector2 direction = (toPlane - fromPlane).Normalized(); float distance = (toPlane - fromPlane).Length();
Vector2 direction = (toPlane - fromPlane) / distance;
Vector2 currentPointPlane = fromPlane; Vector2 currentPointPlane = fromPlane;
HexCell currentCell = GetHexAt(currentPointPlane); HexCell currentCell = GetHexAt(currentPointPlane);
float currentDistance = 0;
do do
{ {
@ -311,8 +313,9 @@ public class HexGrid : Resource
out boundaryPlaneDistance); out boundaryPlaneDistance);
currentCell = currentCell.GetAdjacent(HexCell.NeighborDirections[neighbourIndex]); currentCell = currentCell.GetAdjacent(HexCell.NeighborDirections[neighbourIndex]);
currentPointPlane += direction * boundaryPlaneDistance * 1.001f; currentDistance += boundaryPlaneDistance * 1.001f;
} while (currentCell != toCell); currentPointPlane = fromPlane + direction * boundaryPlaneDistance;
} while (currentDistance < distance);
result.Add(currentCell); result.Add(currentCell);

View File

@ -88,4 +88,15 @@ public class HexGridTests : TestClass
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, -1)), lineCells[1]); Assert.Equal(HexCell.FromOffsetCoords(new Vector2(1, -1)), lineCells[1]);
Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 0)), lineCells[2]); Assert.Equal(HexCell.FromOffsetCoords(new Vector2(2, 0)), lineCells[2]);
} }
[Test]
public void GetTestsInfiniteLoop()
{
HexGrid grid = new HexGrid();
Vector2 fromPlane = new Vector2(-2.31678f, -5.024752f);
Vector2 toPlane = new Vector2(-2.599937f, -6.134028f);
List<HexCell> cellList = grid.GetCellsForLine(fromPlane, toPlane);
}
} }

View File

@ -28,16 +28,4 @@ public class NavigationComponentTests : TestClass
_navigationComponent = new NavigationComponent(); _navigationComponent = new NavigationComponent();
_navigationComponent.TileWorld = _tileWorld; _navigationComponent.TileWorld = _tileWorld;
} }
[Test]
public void SimpleTest()
{
_tileWorld.HexGrid.AddObstacle(HexCell.FromOffsetCoords(new Vector2 (-4, 7)));
Vector3 startPoint = new Vector3(-2.25f, 1.2f, -4.76314f);
Vector3 endPoint = new Vector3(-2.533157f, 1.2f, -5.872417f);
bool isBlocked = _navigationComponent.CheckSweptTriangleCellCollision(startPoint, endPoint, 0.27f);
}
} }