in the part of code that goes over all the terrain to get height measurements,
for (int x = -half; x < half; x += step) {
for (int z = -half; z < half; z += step) {
float y = analyzer.getHeight(x, z);
if (Float.isNaN(y)) {
System.out.println("Skipping area for float is not a number (" + x+" "+z + "returned " + y +")");
continue;
}
It works from the ranges of -128, -128, to -66, -66, (as long as both numbers are smaller than -66) but after that it does not get a result, saying “Skipping area for float is not a number (68 22 returned NaN)”
However, in the main thread i put the code
System.out.println("Player is at Coordinates: " + player.getPhysicsLocation().getX()+" "+player.getPhysicsLocation().getZ());
System.out.println("Terrain Height is: " + terrainAnalyzer.getHeight(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getZ()) + "Is NaN? " + Float.isNaN(terrainAnalyzer.getHeight(player.getPhysicsLocation().getX(), player.getPhysicsLocation().getZ())));
It says “Player is at Coordinates: 68.09893 22.536545
Terrain Height is: 0.0Is NaN? false”
Its the same input, but why I am getting a different result?
Depending on whether you are working in local or world space, you may have better results using the getHeightMapheight(Vector2f xz) method.
The getHeight() method you are using expects the coordinates to be in world space. And if you input a world position value that does not match a terrain vertex, then the method will return Float.NaN for the height.
Anytime you get a NaN return from that method, it means you are searching for a terrain vertex at a point where a terrain vertex does not exist for that terrain.
So if you are working with the getHeight() method that expects world space, and if you are working with a terrain that has a localTranslation of something other than 0,0,0 then you also need to account for that.
But I’d suggest trying with the getHeightMapheight(Vector2f xz) if you are having trouble with the other method that expects world space.
Also, another important note I see in the javadoc (that is another reason you may be getting NaN return values):
The getHeight() method states:
Get the real-world height of the terrain at the specified X-Z coorindate.
Wheras the getHeightmapHeight() method states:
Get the heightmap height at the specified X-Z coordinate. This does not count scaling and snaps the XZ coordinate to the nearest (rounded) heightmap grid point.
It sounds like the getHeight() method may not like that you are using non rouded values like:
And it may work if you round those off to be 68.0 and 22.0 (or use the getHeightMapHeight() method that handles non rounded values)