Cannon convexHullImposter

Hi, I’ve been digging into the CannonJSPlugin to try to add support for convex hull impostors. I wanted to get feedback on what the requirements or complications are for supporting convexHull in the Babylon plugin. I know convexHull has some collision issues, but I was able to get the plugin working with some simple meshes. I’m assuming the object being passed to the plugin is a Mesh. Here is what I’ve tried so far
invoking the convex hull impostor
mesh.physicsImpostor = new PhysicsImpostor(mesh, PhysicsImpostor.ConvexHullImpostor, { mass: 0 }, scene);

CannonJSPlugin.js ~line 354

    CannonJSPlugin.prototype._createShape = function (impostor) {
        ...
        switch (impostor.type) {
            ...
            case PhysicsImpostor.ConvexHullImpostor:
                  const rawVert = object.getVerticesData("position")
                  const rawNorm = object.getVerticesData("normal");
                  const rawIndices = object.getIndices();

                  const vertices = [];
                  const normals = [];
                  const faces = [];

                  for ( let i=0 ; i < rawVert.length ; i+=3 ) {
                    vertices.push(
                      new this.BJSCANNON.Vec3(
                        rawVert[ i ],
                        rawVert[ i + 1 ],
                        rawVert[ i + 2 ]
                      )
                    );
                    normals.push(
                      new this.BJSCANNON.Vec3(
                        rawNorm[ i ],
                        rawNorm[ i + 1 ],
                        rawNorm[ i + 2 ]
                      )
                    );
                  }

                  for ( let i=0 ; i < rawIndices.length ; i+=3 ) {
                    faces.push([
                      rawIndices[ i ],
                      rawIndices[ i + 1 ],
                      rawIndices[ i + 2 ]
                    ]);
                  }
        
                  // Construct polyhedron
                  returnValue = new this.BJSCANNON.ConvexPolyhedron({ vertices, faces })
                    break;

I noticed in the PhysicsImpostor.MeshImpostor there’s some logic to transform the world coordinates and convert the rawVerts. I don’t know exactly why this is done or if it would need to be done on the convex hull as well.
Again, I’ve only tried this with my simple use case. I’m curious what other use cases might be or if there are specific meshes I should use to test with.

pinging @Cedric

Hi @Frankie_Ali and welcome to the forum

Vertex positions need to be transformed when using a mesh hierarchy as an impostor instead of just one mesh.
Imagine parented 2 boxes. Box1 is child of Box0. Box1 vertex positions need to be transformed into Box0 local space.So when Box0+Box1 vertices are used for a convec hull, every position is correct.
Basically, transform Box1 into world space and tranform back into Box0 local space.

Check this PR as a reference : MeshImpostor local position transform by CedricGuillemet · Pull Request #10031 · BabylonJS/Babylon.js · GitHub

Hi, Here is my attempt at supporting Cannon’s ConvexHullImpostor:

https://github.com/antoineMoPa/Babylon.js/commit/0bbcffc634141e7ed3f42ea5d6d4453b4e792867

It’s largely based on the exisiting code for MeshImpostor. For my use case, it works well! One thing to note is that the mesh must already be Convex, else CannonJS will send a bunch of warnings. Also, I let CannonJS calculate the normals, but it could be good to use object.getVerticesData(“normal”) as @Frankie_Ali did.

2 Likes