Dynamic Terrain with dynamic update every frame don't work - please help

Hello!
I would like update an objects possition “swimming” on a fluid terrain. For the surface I use dynamic terrain with an updateVertex on every frame. This works so fine: :wink:
humanGrid | Babylon.js Playground (babylonjs.com)

2023-11-19_14-55-59

But I can’t get the dynamically calculated getHeightFromMap to update “swimming” object. Height remains always the same. I would need recalculated normal map too, to calculate further object movement. :thinking:

Does anyone can help? :smirk_cat:

Looking at the Dynamic Terrain code, I don’t think you can use getHeightFromMap to get new results when you update the vertices, because the function will use the original mapData for the terrain: https://github.com/BabylonJS/Extensions/blob/master/DynamicTerrain/dist/babylon.dynamicTerrain.js#L1031

That’s right :slight_smile:
In my memories, getHeightFromMap() was optimized to compute everything in advance from the mapData.

That said, if you update the terrain with your own custom function, it’s likely that you can know/compute each point elevation, because it’s actually what this custom does (computing each point elevation).

It seems that this function calculates heights and Normals and stores them internally. Strange, that last parameter of getHeightFromMap should be also normal vector - but it’s always zero.
How can i read this internal array? Or do I have actual vertices heights in a custom array and calculate objects heights myself?

terrain.useCustomVertexFunction = true;
terrain.refreshEveryFrame = true;
terrain.computeNormals = true;
terrain.updateVertex = function (vertex, i, j) {  
  vertex.position.y = 2.0 * Math.sin((vertex.position.x + t) / 5.0) * Math.cos((vertex.position.z + t) / 5.0);
};

Thanks for any help! :roll_eyes:

GravWell uses a similar dynamic terrain approach, and I well recall how much I wrestled with the same issues you are! It’s been quite a long time since I’ve looked at the code in depth, but I do think I remember that there was something wonky with updateVertex and useCustomVertexFunction and that I ended up setting computeNormals and useCustomVertexFunction to false but that it still executes (presumably since we’re overwriting the default vertex function with ours?).

HTH!