Heightmap and heightfield (cannon) and authoritative server

I want to use heightmaps with the stack of BabylonJS on the client and cannon-es running on the server and being responsible for the physics.
My ideal workflow would be:

  1. create heightmaps images (eg. with some editor)
  2. use it with the BabylonJS on the client (which is simple with CreateGroundFromHeightMap)
  3. create heightfield for the cannon to take care about the physics

I’m having trouble with the latter.
I haven’t found any example of using the image for heightfield creation.

I’m thinking maybe I should take the Uint8ClampedArray of the heightmap image (which can be done with loading the image to the canvas and getting it from the canvas context) and somehow convert it into the heightfield that cannon understands?
The Uint8ClampedArray is one-dimentional array where every 4 entries defines one pixel, like
[0, 0, 0, 255, 0, 0, 0, 255, ... ] defines two pixels.

  1. The bonus question :smiley: On the other way around, if I would like to create the heightmap programatically then should I use DynamicTerrain?

I’ll be thankful :pray: for any idea and pushing me in the right direction.

cc @RaananW and @Cedric :slight_smile:

1 Like

Hi @neu5

I guess you’ve seen this PG already : https://playground.babylonjs.com/#EXL6K#9

Did you try to use a static mesh impostor instead? It may use a bit more memory but performance should be the same…and you could use another Physics Engine.

Hey @Cedric,

Youp, I think I’ve seen all the examples existing in the nearest Internet :slight_smile:

I don’t think I’m following you with the static mesh impostor idea. Could you explain in more details?
I use BabylonJS to render heightmap only on the client - not for the physics.

I’ve been going through the BabylonJS implementation and the CreateGroundFromHeightMap is using CreateGroundFromHeightMapVertexData so maybe I can use it or at least some parts of its implementation :thinking:

dumb question maybe but can’t you use babylon on the server too?

That’s not a dumb question at all! I’ve got babylon on the server before :slight_smile:
But I would like to avoid this, I take it as the last resort.

I will try my best with the CreateGroundFromHeightMapVertexData but I’m also curious if @RaananW has something to say in this subject.

If you are asking how to pass the data to cannon, you will need to pass the mesh’s positions (from the vertex data) to the cannon heightmap. We are doing it here - Babylon.js/cannonJSPlugin.ts at master · BabylonJS/Babylon.js (github.com)
It is a grid (plane) with height definition on each defined point.

Hey @RaananW :slight_smile:
Sorry for not responding for a while but a lot is going on in my private life.
That’s definitely very interesting but I’m not 100% sure if that’s what I need.
I will try to experiment and let you know :slight_smile: