Skip to content

Commit

Permalink
Find point on floor api.
Browse files Browse the repository at this point in the history
  • Loading branch information
awgil committed Mar 2, 2024
1 parent b736033 commit 071fe42
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions vnavmesh/IPCProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public IPCProvider(NavmeshManager navmeshManager, FollowPath followPath, MainWin
Register("Nav.SetAutoLoad", (bool v) => navmeshManager.AutoLoad = v);

Register("Query.Mesh.NearestPoint", (Vector3 p, float halfExtentXZ, float halfExtentY) => followPath.Query?.FindNearestPointOnMesh(p, halfExtentXZ, halfExtentY));
Register("Query.Mesh.PointOnFloor", (Vector3 p, float halfExtentXZ) => followPath.Query?.FindPointOnFloor(p, halfExtentXZ));

Register<Vector3>("Path.MoveTo", followPath.MoveTo);
Register<Vector3>("Path.FlyTo", followPath.FlyTo);
Expand Down
7 changes: 7 additions & 0 deletions vnavmesh/NavmeshQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public List<long> FindIntersectingMeshPolys(Vector3 p, Vector3 halfExtent)

public Vector3? FindNearestPointOnMesh(Vector3 p, float halfExtentXZ = 5, float halfExtentY = 5) => FindNearestPointOnMeshPoly(p, FindNearestMeshPoly(p, halfExtentXZ, halfExtentY));

// finds the point on the mesh within specified x/z tolerance and with largest Y that is still smaller than p.Y
public Vector3? FindPointOnFloor(Vector3 p, float halfExtentXZ = 5)
{
var polys = FindIntersectingMeshPolys(p, new(halfExtentXZ, 2048, halfExtentXZ));
return polys.Select(poly => FindNearestPointOnMeshPoly(p, poly)).Where(pt => pt != null && pt.Value.Y <= p.Y).MaxBy(pt => pt!.Value.Y);
}

// returns -1 if not found, otherwise voxel index
public int FindNearestVolumeVoxel(Vector3 p, int halfSize = 2) => VoxelSearch.FindNearestEmptyVoxel(VolumeQuery.Volume, p, halfSize);
}

0 comments on commit 071fe42

Please sign in to comment.