Confused about tileAttached(), tileDettached()

Hello,

I’ve taken a liking to the TerrainGrid and it’s features, and trying to use it. I’m wondering about the tileAttached() listener, from what I can tell, each time the center moves all 4 of the present quads gets detached, even the one I’m standing on, and then re-attaches all.

My impression was that it would be called when a “new” tile is brought in, and wanted to be adding objects during that (i have vectors for thoose saved with the heightdata).

The suggested gridMoved() for adding/removing objects feels awkward to me, maybe I just don’t have much faith in my ability atm :3



My question would be, what’s the reason for the reattachment of all 4 tiles(or maybe it actually isn’t reattaching, just calling the tileAttached())?

(tileDetached() seems to do the same thing).

Thanks, I agree it should behave like you expect it to. We’ll look into this, could you make a small test case that allows checking the issue?

The TerrainGridListener interface is a little wonky right now. It will be changing shortly to respond to the following events:

-gridMove (called on tiles that are attached and visible, when the grid moves)

-tileLoaded (tile is loaded into the scene, but not necessarily visible)

-tileUnloaded (tile is removed from cache)

-tileAttached (a tile is attached to the visible tree)

-tileDetached (a tile is no longer visible)

3 Likes

Wasn’t expecting that quick a response :D, went and had dinner.



As far as a test case goes, I was used simple System.out.prints in the listener methods, to see the effects. moving around on the grid caused one call on gridMoved, 4 on tileattached, 4 on detach.

I thought I had in place a way of getting the objects off aswell, but realizing now as I was looking through it that the “cell” doessn’t seem to be the same in tileattach / tiledetach.



[java]terrain.addListener(new TerrainGridListener(){



public void gridMoved(Vector3f newCenter) {

System.out.println(“new center - x: " + newCenter.x +” y: " + newCenter.y +" z: “+ newCenter.z);//TODO: remove

}



public Material tileLoaded(Material material, Vector3f cell) {

return material;

}





public void tileAttached(Vector3f cell, TerrainQuad quad) {

System.out.println(“added Tile - x: " + cell.x +” y: " + cell.y +” z: "+ cell.z); //TODO: remove)

quad.addControl(new RigidBodyControl(new HeightfieldCollisionShape(quad.getHeightMap(), terrain.getLocalScale()), 0));

bullet.getPhysicsSpace().add(quad);





Node cellWorldNode = new Node(“Cell_” + cell.x + “"+ cell.z);



MapPatch quadMap = mapArea.getMapPatch(cell);

//Vegitation Node:

//**

Node plant = (Node) assetManager.loadModel(“Models/Doodads/Rock/Rock_1.j3o”);

Vector3f[] vegVect = quadMap.getVegitation();



for(int i =1; i< vegVect.length;i++){

Node pi = (Node) plant.clone();

pi.setLocalTranslation(quad.getWorldTranslation().add(vegVect.multLocal(2f, 1f, 2f)));

cellWorldNode.attachChild(pi);

}

rootNode.attachChild(cellWorldNode);



}



public void tileDetached(Vector3f cell, TerrainQuad quad) {

System.out.println(“detached Tile - x: " + cell.x +” y: " + cell.y +" z: "+ cell.z); //TODO: remove)



bullet.getPhysicsSpace().remove(quad);

quad.removeControl(RigidBodyControl.class);

rootNode.detachChildNamed("Cell
” + cell.x + “_” + cell.z);



}



});[/java]



vegVect[] is basically vectors based of the heightmap, with x-y coords and a heightvalue. the attached model ends up at what I think is the correct place (not sure on x-z tbh, but its height isnt above or underground).

I realize now tho that the objects shouldn’t have been multiplying themselves anyways - since even if it did 4 tiles attached, it was doing 4 detatches aswell :). But I since I check the name of the node by the Vector3f Cell , its getting it wrong it seems. as in the print of the cell x-y-z, the Cell vector in the detach is offset alittle it seems. tileattach has round figures wheras detach has decimals etc.

I’m guessing it has a self-correcting thing in the grid to make it snap to some grid. it’s probably why I noticed the doublecopies of the added Rocks in the first place, they weren’t exatly on top of eachother, they were offset by small amounts, so i could notice them slightly sticking out of one another.