(Solved) Different between adjustHeight/setHeight and getHeight Terrain coordinates


I use TerrainQuad#adjustHeight, which works fine:

quad.adjustHeight(new Vector2f(terrainX, terrainY), 1.0f);

But I want now use getHeight and setHeight to keep the height between 0.0f and 255.0f.

final Vector2f loc = new Vector2f(terrainX, terrainY);
final float height = quad.getHeight(loc);
if(height < 255.0f) {
    quad.setHeight(loc, Math.min(height * 1.0f, 255.0f));

This doesn’t work, because getHeight returns mostly NaN.
I looked in the source and figure out, that adjustHeight and getHeight calculate the real terrain coordinates different:

// TerrainQuad:1066 (getHeight(Vector2f))
float x = (float)(((xz.x - getWorldTranslation().x) / getWorldScale().x) + (float)(totalSize-1) / 2f);
float z = (float)(((xz.y - getWorldTranslation().z) / getWorldScale().z) + (float)(totalSize-1) / 2f);
// TerrainQuad:1159 (called by adjustHeight(Vector2f, float))
int x = Math.round((xz.get(i).x / getWorldScale().x) + halfSize);
int z = Math.round((xz.get(i).y / getWorldScale().z) + halfSize);

setHeight(Vector2f, float) call the same method as adjustHeight. Can me please somebody explain the different? I have no Idea and don’t found anything about.

Thank you

This has come up a few times. I think the general consensus is to just use adjustHeight and have a convenience method that translates your scale to unit values if you prefer it that way.

1 Like

getHeight() is interpolated… as the javadoc states.

You probably want:

…at least that’s my guess purely from the documentation.

1 Like

Yes, thank you very much. :slight_smile: