Heightmap and heightfield (cannon) and authoritative server

Hello,
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:

2 Likes

Hey @Cedric! Sorry for leaving this for so long but I’ve got a lot of family stuff going on.
I see now that Havok is the new player in the game and I’m thinking if the best way for my simple multiplayer car game would be using Havok with NullEngine on the server and then render it on the client with BabylonJS? Can you confirm that it’s good idea (as opposed to trying to make it work with cannon-es)

I haven’t checked for a while but I don’t think heightfield support has been merged in Havok yet. I think you can use tri mesh for that. perf should be reasonably the same. Quality and performance will be much better than Ammo anyways.

Okey, can you guide me a little bit with the tri mesh idea?
I’ve seen eg.

where Havok + Heightmap combination is working. I wasn’t getting into the code yet, but is that the example of the tri mesh you’ve mentioned?

This is 1 example of Havok + mesh, yes.

convexPhysicsShape = new BABYLON.PhysicsShapeMesh(
                baseConvexShape,
                scene
            );

Thanks! I’ll try to make it work.

1 Like

@Cedric if I may have one more question. What would you recommend to build the simplest vehicle with Havok?
I’ve seen

But 1100 lines doesn’t seem to be ‘simple’ to me.
Is there another way or is it better to wait until the proper vehicle support will be implemented in Havok plugin?

It will take months before having a production ready vehicle implementation in Babylon.js
So, I recommend for now to use your own implementation.
I think it can be less than 1K LoC but I didn’t try to do 1.

1 Like

Thanks for your reply.
1K LoC isn’t really that much.
I am nowhere close to be able to implement it by myself so I guess I will try to utilize the @Raggar code.
I hope I won’t drift much from the later official API :pray:

It should not and there is a migration guide : Migrate from Physics V1 | Babylon.js Documentation

1 Like

You can try my arcade-style simple vehicle. It uses simple springs for suspension and pretty much nothing else. Low center of mass and forces applied under the vehicle gives it great stability and enables sharp turns without rolling over.
If I have time this weekend, it should be done in a few days

2 Likes