Help with TerrainGrid Not Loading (solved)

In my game I’m using TerrainGrid + FractalTileLoader to create a large game world… Do I need to update something special to trigger the TerrainGrid after it is constructed? Maybe something to trigger one of the TerrainGridListener functions tokeep it from messing up? See end of post for code and more info.

When I start the game it just spams this in the debug console repeatedly and nothing is visible:

gridMoved → newCenter = (0.0, 0.0, 0.0)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
fixed normals (0.0, 0.0, 0.0)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
gridMoved → newCenter = (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
— OVERWORLD CHUNK LOADED: 0,0
— OVERWORLD CHUNK UNLOADED: 0,0
fixed normals (NaN, 0.0, NaN)

    /*
     * Add a listener that loads and unloads chunks.
     */
    overworld.addListener(new TerrainGridListener() {

        /*
         * notifies the system about a tile being loaded
         * @param newCenter 
         */
        @Override
        public void gridMoved(Vector3f newCenter) {
            System.out.println("gridMoved -> newCenter = " + newCenter.toString());
        }

        @Override
        public void tileAttached(Vector3f cell, TerrainQuad quad) {
            final int oldChunkX = currentChunkX;
            final int oldChunkZ = currentChunkZ;
            int newChunkX = (int) cell.x;
            int newChunkZ = (int) cell.z;

            /*if (newChunkX == oldChunkX && newChunkZ == oldChunkZ) {
                return; // commented to see if it fixes the problem. it doesnt
            }*/
            currentChunkX = newChunkX;
            currentChunkZ = newChunkZ;
            System.out.println("--- OVERWORLD CHUNK LOADED: " + newChunkX + "," + newChunkZ);
            try {
                Texture texture = client.assets().loadTexture("terrain/overworld/alpha_" + (int) cell.x + "_" + (int) cell.z + ".png");
                quad.getMaterial().setTexture("AlphaMap", texture);
            } catch (Exception e) {
                quad.getMaterial().setTexture("AlphaMap", client.assets().loadTexture("terrain/overworld/alpha_default.png"));
            }
            overworld.attachChild(quad);
            //quad.setCullHint(CullHint.Inherit); // messed with this a lot to see if it fixes the problem. it doesnt
        }

        @Override
        public void tileDetached(Vector3f cell, TerrainQuad quad) {
            System.out.println("--- OVERWORLD CHUNK UNLOADED: " + (int) cell.x + "," + (int) cell.y);
            //overworld.detachChild(quad); // commented to see if it fixes the problem. it doesnt
        }
    });

When I use the spellbook to teleport to a village, sometimes it will stop spamming and the terrain will be visible. The spell does nothing special, except for set the character’s local translation to x/z (The character’s gets teleported to a village at startup also, as a test to see if it fixes the problem, and it doesn’t)

I have no clue where to start other than what you provided. There are floats in your log that are deemed NaN. I would start there.

Edited first post, it always prints 0,0,0 before it starts printing the NaN,0,NaN
I know this is a tough help req because it could be a number of things from non-jme code so its hard to pinpoint the cause. Maybe if I understood the method I’d be able to narrow things down

Is there any extended info I can get about this? Like what would a JME user be doing that triggers the execution of gridMoved … obviously something camera related, but what? What is the newCenter read from?
“Called whenever the camera has moved full grid cells.”
gridMoved(Vector3f newCenter)

If you shift click a method it will tell you all the other methods including line number that calls it. When you debug a line as a stop point, the debugger will give you a stacktrace of the chain that got you there. I think this is maybe a time to learn how to take advantage of it.

1 Like

I don’t have experience with the Terrain Grid, however I’ve had a similar issue using a single Terrain. It was caused by calling the .getHeight(Vector2f xzLoc) or .getHeightMapHeight(Vector2f xzLoc). If you call this method on an xzLocation that is outside of the terrain, then the value will return NaN.

I’m not sure if this is the same cause of your NaN values from the terrain grid, but I thought it may be relevent to mention a situation where I’ve found a method from the TerrainQuad to return NaN values.

2 Likes

I didnt suspect the getHeight(xz) function, I’ll check that out

You’re right!! I didnt think a help thread would legitimately resolve the error because it’s not really a “jme problem”… Seriously, thank you for sharing your experience :grin:

To anyone who runs into this problem in the future:
Test your game using a hardcoded height value instead of getHeight, and do something to trigger an LOD update. The terrain should load after doing those 2 thing :wink:

4 Likes

Awesome! I’m glad I was able to help :slightly_smiling_face: