Inconsistant getHeight results for terrain

I have a terrain, and I am trying to get a height on every certain amount. To do this, i am using a method getHeight, which is just

    public float getHeight(float worldX, float worldZ) {
        return terrain.getHeight(new com.jme3.math.Vector2f(worldX, worldZ));
    }

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.

1 Like

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)

But those are where they display the correct height value

I am using a localTranslaftion of 0,0,0

I made it wait a second before trying to map the height of the terrain, and that did solve the problems

1 Like