I’m new to this forum and just started to play with jmonkeyengine for a week or two now. I really like it, and it makes much easier to create things I have in my mind.
Right now I’m about to write some codes to create endless terrains based on pluggable noise and fractal functions. Everything seems to work fine, but I have some bugs, and do not know how to solve them.
I’m creating a 3x3 grid of neighbouring terrain tiles, where the user is always on the center. I’ve read on the forums that there are similar solutions already done, so this proves that the concept itself is viable. When I’m moving around I’m shifting the grid when neccessary, and to save memory I’m trying to disconnect tiles far behind the user. I can see on the console, that the nodes were detached, but I can soon get a “java.lang.OutOfMemoryError: Direct buffer memory” exception. I know that I can always raise this value, but I cannot raise it infinitely as the camera is moving along. As I’m adding physics to the scene, I found out one bug being I didn’t detach the node in question from the bulletAppState, and after fixing that I can get farther, but the error still persists. The code is:
Where ProceduralTerrain is just a descendant of TerrainQuad that is filled from a fractal function, and maintains a reference to its physicsControl data. Is there anything I’m doing wrong?
The slowest part of switching to the next center location is when I’m adding the new terrain tiles. Calculating the heightmap is fast enough, but creating the TerrainQuads and adding them to the bulletAppState is stopping the application for a while. Can I create them in a separate thread, or is there any other way to speed it up?
As far as I know, there’s no built in way to do that. But you can load them on another thread if you are careful and do not add them to the scene graph on that thread. You can create all kinds of geometry on another thread but you have to add it to the scene graph on the lwgl thread… the same one update is called on.
I have found some hints on how to do this, but the results were still questionable… Tried out to create my own mesh out of the generated heightmap data, and though it was much faster, I’m facing another problem: to add physics to a mesh i seem to have to have it created as Mode.Triangles, as the converter wants to fill TriangleCount * 3 indexes. And of course TriangleStrips have much less (TriangleCount + 2). Do I have to create a list version out of the strips, or can I feed the collisionmesh with stripdata as well? (I could not find any docs yet on thisd particular question)