diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index 6e75c6c..0a9bb31 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -199,10 +199,14 @@ namespace DotRecast.Detour * Function returning a random number [0..1). * @return Random location */ - public Result FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius, - IDtQueryFilter filter, FRand frand) + public DtStatus FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius, + IDtQueryFilter filter, FRand frand, out long randomRef, out RcVec3f randomPt) { - return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, NoOpPolygonByCircleConstraint.Noop); + var result = FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, NoOpPolygonByCircleConstraint.Noop); + randomRef = result.result.GetRandomRef(); + randomPt = result.result.GetRandomPt(); + + return result.status; } /** @@ -220,14 +224,19 @@ namespace DotRecast.Detour * Function returning a random number [0..1). * @return Random location */ - public Result FindRandomPointWithinCircle(long startRef, RcVec3f centerPos, float maxRadius, - IDtQueryFilter filter, FRand frand) + public DtStatus FindRandomPointWithinCircle(long startRef, RcVec3f centerPos, float maxRadius, + IDtQueryFilter filter, FRand frand, out long randomRef, out RcVec3f randomPt) { - return FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, StrictPolygonByCircleConstraint.Strict); + var result = FindRandomPointAroundCircle(startRef, centerPos, maxRadius, filter, frand, StrictPolygonByCircleConstraint.Strict); + randomRef = result.result.GetRandomRef(); + randomPt = result.result.GetRandomPt(); + + return result.status; } public Result FindRandomPointAroundCircle(long startRef, RcVec3f centerPos, float maxRadius, - IDtQueryFilter filter, FRand frand, IPolygonByCircleConstraint constraint) + IDtQueryFilter filter, FRand frand, + IPolygonByCircleConstraint constraint) { // Validate input if (!m_nav.IsValidPolyRef(startRef) || !RcVec3f.IsFinite(centerPos) || maxRadius < 0 diff --git a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs index 2f07e0b..4c53b0d 100644 --- a/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/CrowdProfilingTool.cs @@ -161,11 +161,11 @@ public class CrowdProfilingTool if (0 < zones.Count) { int zone = (int)(rnd.Next() * zones.Count); - Result result = navquery.FindRandomPointWithinCircle(zones[zone].GetRandomRef(), - zones[zone].GetRandomPt(), zoneRadius, filter, rnd); - if (result.Succeeded()) + var status = navquery.FindRandomPointWithinCircle(zones[zone].GetRandomRef(), zones[zone].GetRandomPt(), zoneRadius, filter, rnd, + out var randomRef, out var randomPt); + if (status.Succeeded()) { - return result.result.GetRandomPt(); + return randomPt; } } @@ -282,11 +282,11 @@ public class CrowdProfilingTool Result nearestPoly = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter); if (nearestPoly.Succeeded()) { - Result result = navquery.FindRandomPointAroundCircle(nearestPoly.result.GetNearestRef(), - crowAgentData.home, zoneRadius * 2f, filter, rnd); - if (result.Succeeded()) + var status = navquery.FindRandomPointAroundCircle(nearestPoly.result.GetNearestRef(), crowAgentData.home, zoneRadius * 2f, filter, rnd, + out var randomRef, out var randomPt); + if (status.Succeeded()) { - crowd.RequestMoveTarget(ag, result.result.GetRandomRef(), result.result.GetRandomPt()); + crowd.RequestMoveTarget(ag, randomRef, randomPt); } } } @@ -297,11 +297,11 @@ public class CrowdProfilingTool Result nearestPoly = navquery.FindNearestPoly(ag.npos, crowd.GetQueryExtents(), filter); if (nearestPoly.Succeeded()) { - Result result = navquery.FindRandomPointAroundCircle(nearestPoly.result.GetNearestRef(), - crowAgentData.home, zoneRadius * 0.2f, filter, rnd); - if (result.Succeeded()) + var status = navquery.FindRandomPointAroundCircle(nearestPoly.result.GetNearestRef(), crowAgentData.home, zoneRadius * 0.2f, filter, rnd, + out var randomRef, out var randomPt); + if (status.Succeeded()) { - crowd.RequestMoveTarget(ag, result.result.GetRandomRef(), result.result.GetRandomPt()); + crowd.RequestMoveTarget(ag, randomRef, randomPt); } } } diff --git a/test/DotRecast.Detour.Test/RandomPointTest.cs b/test/DotRecast.Detour.Test/RandomPointTest.cs index 14b0386..8cf546a 100644 --- a/test/DotRecast.Detour.Test/RandomPointTest.cs +++ b/test/DotRecast.Detour.Test/RandomPointTest.cs @@ -64,12 +64,14 @@ public class RandomPointTest : AbstractDetourTest query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); for (int i = 0; i < 1000; i++) { - Result result = query.FindRandomPointAroundCircle(randomRef, randomPt, 5f, filter, f); - Assert.That(result.Failed(), Is.False); + var status = query.FindRandomPointAroundCircle(randomRef, randomPt, 5f, filter, f, out var nextRandomRef, out var nextRandomPt); + Assert.That(status.Failed(), Is.False); - randomRef = result.result.GetRandomRef(); - randomPt = result.result.GetRandomPt(); + randomRef = nextRandomRef; + randomPt = nextRandomPt; + Tuple tileAndPoly = navmesh.GetTileAndPolyByRef(randomRef).result; + float[] bmin = new float[2]; float[] bmax = new float[2]; for (int j = 0; j < tileAndPoly.Item2.vertCount; j++) @@ -97,14 +99,14 @@ public class RandomPointTest : AbstractDetourTest float radius = 5f; for (int i = 0; i < 1000; i++) { - Result result = query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); - Assert.That(result.Failed(), Is.False); + var status = query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var nextRandomRef, out var nextRandomPt); + Assert.That(status.Failed(), Is.False); - float distance = RcVec3f.Dist2D(randomPt, result.result.GetRandomPt()); + float distance = RcVec3f.Dist2D(randomPt, nextRandomPt); Assert.That(distance <= radius, Is.True); - randomRef = result.result.GetRandomRef(); - randomPt = result.result.GetRandomPt(); + randomRef = nextRandomRef; + randomPt = nextRandomPt; } } @@ -114,29 +116,29 @@ public class RandomPointTest : AbstractDetourTest FRand f = new FRand(1); IDtQueryFilter filter = new DtQueryDefaultFilter(); query.FindRandomPoint(filter, f, out var randomRef, out var randomPt); - + float radius = 5f; // jvm warmup for (int i = 0; i < 1000; i++) { - query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f); + query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f, out var _, out var _); } for (int i = 0; i < 1000; i++) { - query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); + query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var _, out var _); } long t1 = RcFrequency.Ticks; for (int i = 0; i < 10000; i++) { - query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f); + query.FindRandomPointAroundCircle(randomRef, randomPt, radius, filter, f, out var _, out var _); } long t2 = RcFrequency.Ticks; for (int i = 0; i < 10000; i++) { - query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f); + query.FindRandomPointWithinCircle(randomRef, randomPt, radius, filter, f, out var _, out var _); } long t3 = RcFrequency.Ticks;