Switch back from Ammo.js to Cannon.js

Hi everyone,

I’m currently working on a small arcade sky game and i’m struggling a bit with the physics engines.

First, I need to switch the engine back to Cannon JS in order to be able to use HeightmapImpostor which seems to work only with this engine. Am I right on this point ? What I want to do is import a mesh from blender and use it as the ground in my physics engine. Don’t know if it’s the best way to do that.

Second, as I want to switch back I tried to just replace the script import in the head of my html file and removed AmmoJSPlugin + the import of the plugin in scene.enablePhysics. I thought that doing so was ok but now, it’s like I don’t have any gravity. Do you know what’s wrong in this case?

Here is a link to my demo with Ammo JS and with Cannon I would have prefer to put it in the playground but I don’t know how to import my glb file in it.


Ammo.js is the default physics engine.
Use this inplace of line 152:

scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), new BABYLON.CannonJSPlugin());

And for your second question, the above code should fix it.

1 Like

Thanks for your answer Givo but I already tried that solution and it did not work unfortunately ( I modified my code to add your modification Babylon - Getting Started ).

Could it come from a difference in a way of using Babylon JS (declaring mesh and impostor, manipulating physics ) when using one plugin or the other ?

pinging @Cedric

1 Like

I know some properties don’t transfer from one engine to another. Also, make note that the mesh impostors for cannon only collide with spheres and planes. :slight_smile:

1 Like

Thanks for your answer Givo,
I will try yo rewrite it step by step (refactoring along also) and find the missing / faulty properties.
Just to make shure that I’m not trying to do something impossible or undesirable with Babylon : If you wanted to have a custom ground with bump and hole with low poly flavor, would you use HeightmapImpostor or something else ? Knowing that the physics impostor on my “skier” mesh are sphere ?

I would use height map impostor. It’s a lot of trial-and-error with this kind of thing (for me at least)

If you want to stick with ammo It’s possible to use a mesh imposter for adding physics to a heightmap.

var ground = new BABYLON.MeshBuilder.CreateGroundFromHeightMap('ground', '.height.png', {width: 500, height: 500, subdivisions: 100, minHeight: 0, maxHeight: 10, onReady: () => {
    ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, restitution: 0.1, friction:0.5}, scene)
}}, scene);

I’m doing something quite similar using blender to generate a heightmap and using babylon to create a mesh from the heightmap and then using the mesh imposter in ammo for the physics. So I’d say your on the right track, so dont give up yet! :slight_smile:


Hello @Mr_K,
Thanks for your answer :slight_smile: Don’t worry i’m not giving up :mechanical_arm:
Currently I’m working on another mini game in Babylon.js (less ski more basketball) and I’m planning to comeback to this one once I’ll complete the other !

Thank you for the advice on using an heightmap then the MeshImpostor in order to generate a physics impostor, I’ll surely try that ! Also, I’m really interested on how you are generating an height map from Blender. I found some tutorials online about that but they were old or seemed hacky to me (but maybe they are not and I don’t understand it as a blender newbie).

Thanks again for your support and advices !

1 Like

No worries!

There are a few ways to get heightmaps out of blender but its become tricky since 2.8 removed the option to bake displacement in cycles.

I find the easiest way is to use the displacement modifier and add a texture to it, then use texture paint to create the height map.

You can always download legacy versions of blender if you want to make it easy :slight_smile: Index of /release/