# Random determined coordinates on the NavMesh

I have created a NavMesh for my terrain and let a enemy move with computePathe to the selected coordinate.
Next, I wanted to try, if the enemy can choose itself a coordinate for himself.
For that I wanted to ask if there is a method that can determine a random X & Z-coordinate, which are located on the NavMesh?

You could get a random triangle from the mesh (I think the method is getTriangle(int index)… I think) And just use the coord as upper/lower bounds of the random coords you choose. You can get the total number of traingle from the mesh, etc, etc. So everything you need to do this is readily available for ya.

1 Like

I can’t understand it completely.
I couldn’t use getTriangle on navMesh so I used getCell on navMesh and than getTriangle on cell.
But what I have to do exactly with the Vector3f[]?

And if I print out cell I get 82.9,-125.0. Are these the upper/lower bounds?
And also what I have to do with the total number of triangle from the mesh (in total 549 triangles)?

private int index;
private Vector3f[] c;

``````navMesh = new NavMesh(navMeshGeometry.getMesh());

Cell cell = navMesh.getCell(index);
c = cell.getTriangle();

int numCells = navMesh.getNumCells();
``````

But how can I get a random value for xCooord and zCoord?
navMeshPathfinder.computePath(new Vector3f(xCoord, yCoord, zCoord));

I really feel like an idiot.
But still I’m grateful for the advice An example would help me very much Could someone explain the reply of t0neg0d more precisely to me?

I’ll try and explain a little further… it would look something along the lines of:

[java]
Mesh m;
Triangle t;
Vector3f p1, p1, p3, a, b, result;

m = someNavMesh;
t = m.getTriangle(FastMath.rand.nextInt(m.getTraingleCount());
p1 = t.get1();
p2 = t.get2();
p3 = t.get3();

a.interpolate(p1, p2, 1f-FastMath.rand.nextFloat());
b.interpolate(p1, p3, 1f-FastMath.rand.nextFloat());
result.interpolate(a,b,FastMath.rand.nextFloat());
[/java]

result will now be a Vector3f within the bounds of the triangle of the mesh you randomly selected. You may need to initialize the Vector3fs a, b and result /shrug

2 Likes

Solution very much depends on how you want distribution to look like. Should each x/z coordinate be same probable? Should each unit area of surface be same probable? Or you don’t care at all as long as it is slightly better than return (0,0,0)?

@t0neg0d
Thanks a lot! I tried to use your code but I get an error in the 10.line.

``````Node terrainNode = (Node) terrain;
Geometry navMeshGeometry = (Geometry) terrainNode.getChild("NavMesh");
navMesh = new NavMesh(navMeshGeometry.getMesh());

Mesh m;
Triangle t;
Vector3f p1, p2, p3, a, b, result;

m = navMeshGeometry.getMesh();
t = m.getTriangle(FastMath.rand.nextInt(m.getTriangleCount()));
p1 = t.get1();
p2 = t.get2();
p3 = t.get3();

a.interpolate(p1, p2, 1f-FastMath.rand.nextFloat());
b.interpolate(p1, p3, 1f-FastMath.rand.nextFloat());
result.interpolate(a, b, FastMath.rand.nextFloat());``````

no suitable method found for getTriangle(int)
method Mesh.getTriangle(int,int[]) is not applicable
(actual and formal argument liests differ in lenght)
method Mesh.getTriangle(int,Triangle) is not applicable
(actual and formal argument liests differ in lenght)
method Mesh.getTriangle(int,Vector3f,Vector3f,Vector3f) is not applicable
(actual and formal argument liests differ in lenght)

I am happy, as long as the vector3f defines an available spot on the NavMesh with the x/z coordinates and the y coordinate is 0.

3 Likes

Oooops… sorry I meant: m.getTriangle(int, t); not t =

1 Like

@t0neg0d

Thank you very much!
You’ve helped me a lot 1 Like