Collisions on Flipped Normals

Firstly, the gltf/glb has right handed coordinate system and BabylonJS coordinate system is left handed. So you need to set scene.useRightHandedSystem = true before importing. Else you have to consider negative z-scaling, which can cause unpredictable collision problems.

The main cause of the collision failure seems to be that the flipped mesh has inverted normals and also has a right handed coordinate system. Usually the triangles of gltf are counter-clockwise, while Babylons (left handed) are clockwise. So if you enable right handed conversion by scene, then the side orientation is and needs to be set clockwise and vice versa.

Accordingly the workaround that covers both cases would be:

mesh.overrideMaterialSideOrientation = scene.useRightHandedSystem ? Material.ClockWiseSideOrientation : Material.CounterClockWiseSideOrientation

To invert normals just get, invert and set vertices normal data:

var normals = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
for(let i=0; i<normals.length; i++)
     normals[i] *= -1;
mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);

Another thing is that you need to set measures of “player” collision body, that is defined by ellipsoid-property of mesh:

player.ellipsoid = new BABYLON.Vector3(1, 1, 1) // radius of 1

From my knowledge checkCollisions of meshA only takes effect if another meshB moveWithCollisions(). Therefore it would only make sense if another mesh moves with collision.

The PG with workaround:

But @RaananW may confirm I didn’t miss anything.

3 Likes