[SOLVED] Using TerrainQuad.getHeight() properly

I am trying to generate a bunch of plant models at random spots on the surface of the terrain but terrainQuad.getHeight(); isn’t returning the correct value; at least it’s not returning the value that my character’s local translation is returning.

This is the code I am using:

 gameLevel = app.getAssetManager().loadModel("Scenes/FirstScene.j3o");
        gameLevel.addControl(new RigidBodyControl(0));
       terrainGeo = gameLevel;
       terrainQuad = ((TerrainQuad)((Node)terrainGeo).getChild("terrain-FirstScene"));
       Node plantcube = (Node) app.getAssetManager().loadModel("Models/plantcube.j3o");
       List<Node> lilys = new ArrayList<>();
       for(int i=0;i<2000;i++){
            lilys .add((Node) plantcube.clone());
       for (Node lily: lilys) {
            float a = (float) Math.random()*2000;
            float c = (float) Math.random()*2000;
         lily.setLocalTranslation(-1000 + a,  terrainQuad.getHeight(new Vector2f((-1000 + a),(-1000+c))),-1000 +c);
         lily.setLocalRotation(new Quaternion(0.0f, 0.93807447f + (float)Math.random(), 0.0f, -0.34643376f+ (float)Math.random()));

This is what it looks like:
What’s especially vexing to me is that the difference between the models local translation.y and the terrainquad.getHeight isn’t constant.

I ran into a similar issue in the past, and I recall getting things to work when I switched to use the .getHeightmapHeight() method instead of .getHeight()

Unless I’m mistaken, it looks like you’re inputting world coordinates to the .getHeight() method, and I believe that method actually takes in the local coordinates for the TerrainQuad.

And then the .getHeightMapHeight() method takes in world coordinates.

1 Like

Ok so, you are definitely correct that I should be using getHeightmapHeight(). I just created another Scene and Terrain within it using one of the JME random hill terrain generators and now everything is working properly…unfortunately I don’t want to just give up the Scene i’ve been working on for a while. Any idea what properties of a Terrain would cause it to behave like that?

I altered the mesh of the “Original” Terrain that was having heightmap problems using the JME3 Terrain Editor.

Hmm I’m not entirely certain, but did you alter the mesh by using the terrainQuad.setHeight(Vector2f coord, float height); method, or by altering the mesh’s vertex buffer. I think altering the vertex buffer could cause the problem, since that would not alter the height map data being stored in the TerrainQuad class, only the mesh.

Edit: my bad, I just saw you did mention that you altered the terrain using the terrain editor. I’m not quite sure what could have been the cause then, I use the terrain editor a lot as well but haven’t noticed that problem. Did you possibly scale the terrain when it was having issues? I think one of the javadocs mentioned that the getHeight() methods don’t take scaling into account.

Another potential factor could be if your’e attaching more than one LOD control - the terrain editor will create an LOD control for you by default when you make a new Terrain, and then I’ve also had times where I accidentaly added a second LOD Control at run-time and I got some weird results.

1 Like

I’m an absolute buffoon; there was a minuscule rotation quaternion being applied to the scene. Thanks for your help!

1 Like

Also, as a final note to anyone looking at this in the future, if you use TerrainQuad.getHeight(), it properly takes into account both Scene scaling as well as Terrain scaling! TerrainQuad.getHeightmapHeight() does not.

1 Like

Glad to hear you got things working :slightly_smiling_face:

1 Like