Added NavigationComponent::CheckSweptTriangleCellCollision().

WorldChunkRefactoring
Martin Felis 2023-08-13 22:03:10 +02:00
parent 0cbff2a298
commit 232caf2e8b
1 changed files with 39 additions and 1 deletions

View File

@ -252,6 +252,44 @@ public class NavigationComponent : Spatial
return false; return false;
} }
bool CheckSweptTriangleCellCollision(Vector3 startWorld, Vector3 endWorld, float radius)
{
Vector2 startPlane = new Vector2(startWorld.x, startWorld.z);
Vector2 endPlane = new Vector2(endWorld.x, endWorld.z);
Vector2 directionPlane = (endPlane - startPlane).Normalized();
Vector2 sidePlane = directionPlane.Rotated(Mathf.Pi * 0.5f);
List<HexCell> cells = TileWorld.HexGrid.GetCellsForLine(startPlane + directionPlane * radius, endPlane + directionPlane * radius);
foreach (HexCell cell in cells)
{
if (TileWorld.HexGrid.GetHexCost(cell) == 0)
{
return true;
}
}
cells = TileWorld.HexGrid.GetCellsForLine(startPlane + sidePlane * radius, endPlane + sidePlane * radius);
foreach (HexCell cell in cells)
{
if (TileWorld.HexGrid.GetHexCost(cell) == 0)
{
return true;
}
}
cells = TileWorld.HexGrid.GetCellsForLine(startPlane - sidePlane * radius, endPlane - sidePlane * radius);
foreach (HexCell cell in cells)
{
if (TileWorld.HexGrid.GetHexCost(cell) == 0)
{
return true;
}
}
return false;
}
public List<NavigationPoint> SmoothPath(KinematicBody body, List<NavigationPoint> navigationPoints) public List<NavigationPoint> SmoothPath(KinematicBody body, List<NavigationPoint> navigationPoints)
{ {
if (navigationPoints.Count <= 2) if (navigationPoints.Count <= 2)
@ -271,7 +309,7 @@ public class NavigationComponent : Spatial
{ {
Vector3 endPoint = navigationPoints[endIndex].WorldPosition; Vector3 endPoint = navigationPoints[endIndex].WorldPosition;
if (HasPathCollision(body, startPoint, endPoint)) if (CheckSweptTriangleCellCollision(startPoint, endPoint, 0.27f))
{ {
if (endIndex - startIndex == 1) if (endIndex - startIndex == 1)
{ {