I think this is what’s going on, this method,

```
/**
* Find the closest cell on the mesh to the given point AVOID CALLING! not a
* fast routine!
* @param point
* @return
*/
public Cell findClosestCell(Vector3f point) {
float closestDistance = 3.4E+38f;
float closestHeight = 3.4E+38f;
boolean foundHomeCell = false;
float thisDistance;
Cell closestCell = null;
// oh dear this is not fast
for (Cell cell : cellList) {
if (cell.contains(point)) {
thisDistance = Math.abs(cell.getHeightOnCell(point) - point.y);
if (foundHomeCell) {
if (thisDistance < closestHeight) {
closestCell = cell;
closestHeight = thisDistance;
}
} else {
closestCell = cell;
closestHeight = thisDistance;
foundHomeCell = true;
}
}
if (!foundHomeCell) {
Vector2f start = new Vector2f(cell.getCenter().x, cell.getCenter().z);
Vector2f end = new Vector2f(point.x, point.z);
Line2D motionPath = new Line2D(start, end);
ClassifyResult Result = cell.classifyPathToCell(motionPath);
if (Result.result == Cell.PathResult.ExitingCell) {
Vector3f ClosestPoint3D = new Vector3f(
Result.intersection.x, 0.0f, Result.intersection.y);
cell.computeHeightOnCell(ClosestPoint3D);
ClosestPoint3D = ClosestPoint3D.subtract(point);
thisDistance = ClosestPoint3D.length();
if (thisDistance < closestDistance) {
closestDistance = thisDistance;
closestCell = cell;
}
}
}
}
return closestCell;
}
```

loops through all cells and returns the closest cell only after checking all cells.

When it first starts this loop,

```
boolean foundHomeCell = false;
```

so when there are two or more cells with the same x and z but different y, this loop first sees foundHomeCell is false so sets found to true here on the first iteration,

```
if (foundHomeCell) {
if (thisDistance < closestHeight) {
closestCell = cell;
closestHeight = thisDistance;
}
} else {
closestCell = cell;
closestHeight = thisDistance;
foundHomeCell = true; //first iteration sets to true
}
```

The loop hits the next cell that also contains point x and z and sets closestCell and closestHeight again if its

```
thisDistance < closestHeight
```

where closestHeight is from the last cell thisDistance, then finally returns that last cell it found to the calling method.

So how to differentiate which cell to return is the problem. This current way is just a race to the bottom.