getRandomPointAround not constrained to maxRadius

I’m having another issue with getRandomPointAround (thanks for the seed fix btw @Cedric ). I have this code:

console.log('centerPoint', centerPoint, 'radius', radius);
const nextPosition = window.navigationPlugin.getRandomPointAround(centerPoint, radius);
console.log('nextPosition', nextPosition);

And here is what it logs:

wanderer.system.ts:70 centerPoint Vector3 {_isDirty: true, _x: 15, _y: 1, _z: -15} radius 1
wanderer.system.ts:72 nextPosition Vector3 {_isDirty: true, _x: -24.392005920410156, _y: 0.5, _z: -295.0232238769531}

Unless I’m misunderstanding the point of this method, this position it’s generating does not seem right.

Here are my nav mesh parameters in case they’re relevant:

navMeshParams: {
      cs: 5,
      ch: 0.5,
      walkableSlopeAngle: 0,
      walkableHeight: 4,
      walkableClimb: 0,
      walkableRadius: 1,
      maxEdgeLen: 12,
      maxSimplificationError: 0.5,
      minRegionArea: 3,
      mergeRegionArea: 5,
      maxVertsPerPoly: 6,
      detailSampleDist: 6,
      detailSampleMaxError: 1,

It’s dependent on your topology. is the centerPoint close or on the navmesh? if not, recast may find the ‘closest’ on the navmesh, it might be a bit distant.
Also, query extent has an influence too. Creating A Navigation Mesh | Babylon.js Documentation
if you can share a PG, I’ll be able to check what happens.

That makes sense, because it does seem to work on some maps I have. Unfortunately it’s too complex to repro in a PG but I can show a video that might help? Basically it just shows that the entire map is part of the navmesh.

The centerPoint is on the navmesh as far as I can tell. Can you explain the query extent some more?

I’m not quite sure how findNearestPoly works, but is it possible the issue derives from the fact that this is all one flat plane?

getClosestPoint returns Vector3 {_isDirty: true, _x: 15.000028610229492, _y: 2.057157039642334, _z: -15.000028610229492}

Alright I think ChatGPT may have solved it:

Radius Check: The function checks if the circle intersects with neighboring polygons (if (distSqr > radiusSqr)). However, this doesn’t guarantee that the final random point within the selected polygon will be within the specified radius from the center. If a large polygon is selected and the random point falls on the far side of it, the point could be outside the desired radius.

Random Point Generation Within Polygon: Once a polygon is selected, a point is randomly generated within it (dtRandomPointInConvexPoly). Again, there’s no guarantee this point will be within the specified radius from the center, especially if the polygon is large.

If your mesh consists of a few large polygons, the function will select a polygon (which in your case might be the entire plane) and then choose a random point within that polygon. Because the polygon is large, the random point could be far from the intended circle’s center, exceeding the specified radius.

I think since my map is all one giant plane, it’s likely just a couple polys. I wish once it found the poly, it would choose a point that was still within the radius, but that’s a recast issue, not a plugin one as far as I understand.

1 Like