What Are The X and Z Locations For Save and Load

I found some post that helped me with saving the terrain quad to file on TerrainGridListener.tileDetached(…) and loading with TerrainGridTileLoader.getTerrainQuadAt().



I have a problem with the loading and saving locations of tiles moving around. It appears to be based on what location I enter a quad at.



For the save I use Math.round(…) of cell.x and cell.z. Where cell is the Vector3f from the first parameter in tileDetached(…).



For load I use Math.round(…) of location.x and location.z. Where location is the Vector3f from first parameter in TerrainGridTileLoader.getTerrainQuadAt(…).



Is this the proper way to determine the location of the TerrainQuad?



I serialize the quad to j3o with the importer and deserialize with the exporter.

There is some commented-out code in one of the image grid terrain tests that created the tiles that are used in the test example for j3o streaming.

I found the save j3o lines in TerrainGridTileLoaderTest. I save and load TerrainQuads. But, the x and z values I use do not match up.



I see more calls to TerrainGridTileLoader.getTerrainQuadAt(…) than I do to TerrainGridListener.tileDetached(…). This leads me to believe that one is working at a higher level.



Here is my code for the two methods.



TerrainGridTileLoader

[java]

@Override

public TerrainQuad getTerrainQuadAt(Vector3f location) {

try {

BinaryImporter importer = BinaryImporter.getInstance();

importer.setAssetManager(EditorMain.getTheater().getAssetManager());

TerrainQuad loadedQuad = (TerrainQuad) importer.load(new File("/tmp/" + "TerrainGrid_" + Math.round(location.x) + "" + Math.round(location.z) + ".j3o"));

if (loadedQuad != null) {

return loadedQuad;

}

} catch (Exception e) {

e.printStackTrace();

}

System.out.println("getTerrainQuadAt: " + Math.round(location.x) + ", " + Math.round(location.z));



return createNewQuad(location);

}

[/java]



TerrainGridListener

[java]

public void tileDetached(Vector3f cell, TerrainQuad quad) {

try {

BinaryExporter.getInstance().save(quad, new File("/tmp/" + "TerrainGrid
" + Math.round(cell.x) + "_" + Math.round(cell.z) + ".j3o"));

} catch (IOException ex) {

ex.printStackTrace();

}



System.out.println("detached: " + Math.round(cell.x) + ", " + Math.round(cell.z));

}

[/java]

Here is the output from the print out statements





getTerrainQuadAt: -2, -1

getTerrainQuadAt: 1, -1

getTerrainQuadAt: -2, 0

getTerrainQuadAt: 1, 0

getTerrainQuadAt: -2, 1

getTerrainQuadAt: 1, 1

getTerrainQuadAt: -2, 2

getTerrainQuadAt: 1, 2

detached: 0, 0

detached: 0, 0

detached: 0, 0

detached: 0, 0

getTerrainQuadAt: -3, -1

getTerrainQuadAt: 0, -1

getTerrainQuadAt: -3, 0

detached: -1, 0

detached: 0, 0

getTerrainQuadAt: -3, 1

getTerrainQuadAt: 0, 1

getTerrainQuadAt: -3, 2

detached: -1, 0

getTerrainQuadAt: 0, 2

detached: 0, 0

getTerrainQuadAt: 1, 0

getTerrainQuadAt: 1, 1

detached: -2, 0

getTerrainQuadAt: 1, 2

detached: -2, 0

Looking at the detach method. The values I think I want are in TerrainQuad.name. The value for one instance is “Quad(-1.0, 0.0, 3.0)”. But, I cannot find out where the vector is containing the (-1, 0, 3). The Vector3f cell passed in, does not provide me with the right data, or I don’t know how to convert it to what I need.

Why are you doing all that? Its already implemented. Alse never use BinaryImporter to load files, use the AssetManager.

Is this the commented out code you were talking about?



TerrainGridTileLoaderTest.java



// try {

// BinaryExporter.getInstance().save(terrain, new File("/Users/normenhansen/Documents/Code/jme3/engine/src/test-data/TerrainGrid/"

// + “TerrainGrid.j3o”));

// } catch (IOException ex) {

// Logger.getLogger(TerrainFractalGridTest.class.getName()).log(Level.SEVERE, null, ex);

// }

This was the post I was following an example of:



http://hub.jmonkeyengine.org/groups/terramonkey/forum/topic/saving-terraingrid-tiles/





I need to save tiles as the are detached and reload the same file when the camera moves back over the tile.

The thing you want to do is done in TerrainGridTileLoaderTest, the code to create the terrain is removed from the current code but can be accessed in this svn revision: http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/terrain/TerrainFractalGridTest.java?spec=svn8678&r=8678

Thanks,



Where at in this code is the setup to save terrain to a file and load it?



I also wanted to avoid the Fractal loader.



I have a Swing application that allows you to raise, lower currently. Import and place objects from blender on the terrain. As the camera moves I need to save the terrain to a file and load it back out later.



If the Fractal loader keeps up with what was removed from the camera view and places the same quad back. I can look at how it is handling it.

As I said, TerrainGridTileLoaderTest loads j3o tiles and the commented out code in the class I linked saved it before. Basically just copy that code.

I see the save code. I have the same thing in my project.



I found the code to load the quads in FractalHeightMapGrid



I’ll see if i can find out what the difference is. The save code uses cell.x and cell.z just as I do. It appears the load code users location x and z as well.





I did notice that the load code is loading from a png while the save is, saving to a j3o.



You said you did not suggest I load using the BinaryImporter ? It seemed to work just fine.

As I said twice now, TerrainGridTileLoaderTest loads the j3o tiles, any no you are not supposed to use BinaryImporter.

Sorry, not trying to be any trouble for you.



So, you are saying use AssetTileLoader for loading thh j3o files? This i guess works like the ImageLoader where it plugs x and z into the filename.

It only needs the “base” folder and name of the tiles. In line 97 that info is given.

Thanks, I have it working now.



I had to change my save filename to order Z, Y, X









thanks again.

Thanks, I have it working now.



I had to save with the filename location ordered z, y, x. Everything shows up in the right place afterwards.







[java]

int tileX = Math.round(cell.x);

int tileY = Math.round(cell.y);

int tileZ = Math.round(cell.z);

try {

BinaryExporter.getInstance().save(quad, new File("/home/…/assets/Scenes/Terrain/"

  • "TerrainGrid_"
  • tileZ
  • "_"
  • tileY
  • "_"
  • tileX
  • ".j3o"));

    } catch (IOException ex) {

    ex.printStackTrace();

    }

    [/java]

I still seem to be having issues with the load / save using referencing the same X / Z coord.



When I start the application and move forward, then backwards, the loader and detach (save) seem to be referencing different x and z coord.



Any thoughts on this?



Move forward:

attaches (x, z)

3, -1

3, 0

3, 1

3, 2



saves (x, z)

0, 0



Move Backward:

attaches (x, z)

-1, -1

2, -1

-1, 0

-2, 0

-1, 1

2, 1

-1, 2

2, 2



saves (x, z)

1, 0

Like I said, the existing code works.

I think I have everything the way the test were. TerrainFractalGridTest had the save in the attach method; which i did try and still no luck.



Anything wrong with this code?



For loading i have this, and it seems to work



[java]

final AssetTileLoader loader = new AssetTileLoader(assetManager, "TerrainGrid", "Scenes/Terrain");

[/java]



For saving I added a TerrainGridListener and implemented the save in detach



[java]

public void tileDetached(Vector3f cell, TerrainQuad quad) {

int tileX = Math.round(cell.x);

int tileY = Math.round(cell.y);

int tileZ = Math.round(cell.z);

try {

BinaryExporter.getInstance().save(quad, new File("/home/worleyc/projects/game/awakening/AwakeningEditor/assets/Scenes/Terrain/"

  • "TerrainGrid_"
  • tileX
  • "_"
  • tileY
  • "_"
  • tileZ
  • ".j3o"));



    } catch (IOException ex) {

    // Logger.getLogger(TerrainFractalGridTest.class.getName()).log(Level.SEVERE, null, ex);

    ex.printStackTrace();

    }



    System.out.println("detached: " + tileX + ", " + tileZ);

    }



    [/java]