Physics update after modifying height data of TerrainQuad / TerrainGrid

Hello!



I’m trying to modify terrain generated with TerrainGrid, but I have some issues with the physics data after the height data of the map is updated. The ‘character’ (1st person view) walks through the terrain if the height goes up (i didnt try yet to lower it but im sure it would be the same).



So far , i figured the problem comes from physics, as TerrainGrid is using the events addTile, removeTile to recompute physics for each new tile.



The problem im having right now is I dont know how to call the removeTile, addTile event from outside the class.



Of course, if there is another solution, I’m listening…



Thanks in advance.

Adding and removing the tile is not needed to recalculate the physics mesh. You will need to do that whenever you modify the height.

Sorry i didnt fully understand your answer, Sploreg.

I’l try to explain more clearly. The process is like this:



If player clicks mouse , i intersect a ray with the terrain, then i modify the height around the point of intersection in the targeted tile. Now is the time (i think) to recalculate the physics mesh. And that is what i quite can’t figure out properly.



Meanwhile, i figured out something to work with the adding/removing of the tile :



Im getting the TerrainPatch that’s being ‘hit’, then the parent TerrainQuad, and using this TerrainQuad with the add/remove process. But I think its still not working properly , also from time to time it screws up the terrain tiles.



Im open to other ideeas, since this one , so far, doesn’t look like a brilliant one :slight_smile:

I have only played with TerrainQuad (not TerrainGrid). Don’t know if this is the best way to do it but I use:



[java]

public void adjustHeight(Vector2f xz, float delta) {

terrain.adjustHeight(xz, delta);

bulletAppState.getPhysicsSpace().remove(terrain);

terrain.removeControl(terrainCollisionControl);

CollisionShape terrainShape =

CollisionShapeFactory.createMeshShape((Node) terrain);

terrainCollisionControl = new RigidBodyControl(terrainShape, 0f);

terrain.addControl(terrainCollisionControl);

bulletAppState.getPhysicsSpace().addAll(terrain);

}

[/java]

1 Like

Yes do what @jmaasing suggests.

@jmaasing , @Sploreg Thanks both, I will try this and report back with the result .

I think what i was doing, is about the same (one way or another):



[java]while(tq.getControl(RigidBodyControl.class)!=null){

tq.removeControl(RigidBodyControl.class);

}

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

bulletAppState.getPhysicsSpace().add(tq);[/java]



The code above is actually the code from the TerrainGrid listener addTile , a little bit modified to work with my quad.

P.S. I did found something that i was doing wrong , all the adding/removing from physics was done before modifying the mesh. I corrected this, but no improvement…same result

@eemerge said:
I think what i was doing, is about the same (one way or another):

[java]while(tq.getControl(RigidBodyControl.class)!=null){
tq.removeControl(RigidBodyControl.class);
}
tq.addControl(new RigidBodyControl(new HeightfieldCollisionShape(tq.getHeightMap(), terrain.getLocalScale()), 0));
bulletAppState.getPhysicsSpace().add(tq);[/java]

The code above is actually the code from the TerrainGrid listener addTile , a little bit modified to work with my quad.

I'm just guessing (can't test your code right now) but you do not remove the "tq" from the bullet physics space. Maybe that is the reason?

Yes that might have been an issue also, I update my code but still no result.



Im wondering…could it be because tq.getHeightMap() returns the initial heightMap and not the updated one since im only updating the bufferdata?



This is the code i use to modify the terrain (its basic for now, as i’m just testing and learning):



[java]BufferUtils.setInBuffer(new Vector3f(v1.x, v1.y + 1.0f, v1.z), (FloatBuffer) vb.getData(), triIndices[0]);

BufferUtils.setInBuffer(new Vector3f(v2.x, v2.y + 1.0f, v2.z), (FloatBuffer) vb.getData(), triIndices[1]);

BufferUtils.setInBuffer(new Vector3f(v3.x, v3.y + 1.0f, v3.z), (FloatBuffer) vb.getData(), triIndices[2]); [/java]

@eemerge said:
Im wondering.....could it be because tq.getHeightMap() returns the initial heightMap and not the updated one since im only updating the bufferdata?

nope

woa woa wait, why are you hitting the buffer directly to modify the terrain??? Use the terrain API to set the heights and everything will work out fine.

Yey! Progress! :))



For now it seems i ‘hit’ the bumps on the map… Thanks guys for the help so far !

There is one more bizzare issue , one of the patches (better said, a HALF of it) kinda dissapears when i ‘click’ on it …but im not falling through it, its still there…

ya cause you are modifying the buffers directly instead of going through the terrain API.

I had commented the buffer modification code, but there was a



[java]vb.setUpdateNeeded();[/java]



left, seems that was causing the problem.



Thanks!

Since its related, I would like to ask another question : I’d like to view the targeted area (targeted with the mouse) , but not the whole tile, just the “triangle” thats being targeted… any ideeas on how to do this?

what do you mean by “view” it? Highlight the triangle you are mousing-over?

Yes, sorry. Highlight it…for example, view it like wireframe

You will have to modify the shader for that.

I would just project an image onto that area. http://hub.jmonkeyengine.org/groups/contribution-depot-jme3/forum/topic/projective-texture-mapping/

Thanks Sploreg, i’ll look into that .