Collisions not detected for imported meshes

I’m importing meshes from Blender (via a glb export). They come in as a set of one or more child meshes to a root mesh.

Collision detection (checkCollisions) does not work with them.
The import function:

 BABYLON.SceneLoader.ImportMesh('', 'assets/', config.file, scene,
             (meshes, particleSystems, skeletons, animationGroup) => {
                 meshes.forEach(m => {
                     if(m.material != null)
                         m.material.sideOrientation = BABYLON.Material.CounterClockWiseSideOrientation;
                     
                     shadowGenerator.addShadowCaster(m);
                 });
 
                 this.mesh = meshes[0];
                 this.mesh.scaling.set(.2, .2, .2);
                 this.mesh.position = new BABYLON.Vector3(0, 3, 0);
 
                 const boundingInfo = getBoundingInfo(meshes);
                 this.mesh.setBoundingInfo(boundingInfo);
                 this.mesh.checkCollisions = true;
                 this.mesh.showBoundingBox = config.IS_DEBUG;
         });

The call to ‘move’ the player:

move() : void {
        if(this.mesh && !this.isDead) {
            this.mesh.moveWithCollisions(this.velocity);
        }
}

And in the scene creation:

public static CreateScene(engine: BABYLON.Engine, canvas: HTMLCanvasElement): SpaceCheetahScene {
        var scene = new BABYLON.Scene(engine);
        scene.useRightHandedSystem = true;

        const cfg = {
            IS_DEBUG,
            gravity
        };

        scene.collisionsEnabled = true;
        ....

Are you using BABYLON.Mesh.checkCollisions = true in conjunction with BABYLON.Mesh.moveWithCollisions(BABYLON.Vector3) for collisions? If so, this guide on Mesh Collisions might help.

If you want to check if meshes are intersecting, this guide on Intersect Collisions - Mesh might help.

1 Like

If you could reproduce it on the playground we could see if it is a bug or something you can solve in your code.

As i don’t know what you are importing, it is hard for me to say whether meshes[0] is enough, but seeing what you do i can only assume it is the root of the others.

Just a random question - Why are you setting the root mesh’s bounding info?

@gbz I’ve added some more code from the move() function, and the scene setup. I’ve followed both those guides with little luck. The intersect works, but it doesn’t detect ‘collisions’ it detects if they’re overlapping after the move - so the player will ‘sink’ into the ground when they impact it.

1 Like

@RaananW It may be something with the Blender model, but I’m not sure what might cause that from a modeling standpoint.

meshes[0] is the ‘root’ mesh that the gltf exporter creates as a parent mesh for the 4 meshes that are in the model (head, torso, arms/legs, eyes). If I turn on the collision detection for all the children it gets weird - presumably because the submeshes actual ‘collide’ all the time.

I’m setting the bounding info on the root mesh b/c it doesn’t appear to ‘get’ any bounding info in the export. So I calculate it based off the children that are exported/imported.

It looks like it’s a problem with the models.

The collisions are sort of detected. Right now, there’s an obvious collision when the two meshes are close (but not touching). Then the moving mesh sort of sinks into the platform mesh.
Here’s what he looks like in the end:

I tested collisions with generic (BABYLON.MeshBuilder) meshes, and the collisions worked - albeit with a significant gap between the two meshes.

Any additional help would be greatly appreciated!

Hi Patrick, will you be able to reproduce that in the playground? It’s hard to tell without actually seeing it

I’m assuming you want the imported model to walk over the large 3D box?

MoveWithCollision on the mesh of your imported model, with checkCollisions on both the BOX and the IMPORTED MODEL’s mesh should do it.

You should just upload a reduced version of your code to the playground containing the issue. It’s way more efficient for us to tell.

1 Like

@RaananW @StardustMotion I’ll create a playground with the code, unfortunately because this appears to at least be partially a model export/import issue I doubt I will be able to recreate the entire issue.

@RaananW @StardustMotion Here is the playground I’m setting up - I don’t have any models yet, but the code is almost C/V from my own.

https://playground.babylonjs.com/#IPR5ED#2

Ok the models I’m testing with are at: http://space-cheetah-1c131.web.app/assets/golem.glb and http://space-cheetah-1c131.web.app/assets/platform_1.glb

I can’t import them into the playground, but v7 is the latest: https://playground.babylonjs.com/#IPR5ED#7

PS Is there a way to get the ‘latest’ version of a PG without just adding to the version number until one doesn’t load?

Nope, you need to add the version as anybody could create a separate version of your playground by just pressing save :slight_smile:

@RaananW @StardustMotion Any ideas on why those two models might have issues? As I said it appears to be imported models that are having the problem…

So it appears that the ‘sinking’ issue is that the moveWithCollisions function doesn’t first check to see if it’s already collided with a mesh. The ‘gap’ issue I’m having appears to be a scaling issue.

2 Likes

I have the same issue i guess, moveWithCollision works fine for Meshes built in code, but is buggy for meshes imported as gltf.

e.g. You sink in the greater, gltf-imported box. You can also walk through it walls, whereas you the smaller box works well.

try it here:

https://to5ta.github.io/js_zero/dist/

Did you fix that sinking issue for you in the meantime?

Not sure if this will help and maybe you’ve already seen this, but have you tried adjusting the mesh.ellipsoid attribute?

Thanks for your reply. In which way exactly do you mean?

edit: i adjusted the ellipsoid to fit my character, it works fine for the geometry constructed using the MeshBuilder (.CreateBox()). The Problem occurs only on imported geometry. Somehow the problem also Patricks’ described i guess.

1 Like

Confirmed here that I am also having issues getting checkCollisions = true to work in a predictable way with imported gltf models.

Here’s my playground where as you move around, you slowly sink into the floor before falling to your doom into the playground abyss. https://playground.babylonjs.com/#4HUQQ#1019

It’s like it WANTS to work, but isn’t quite there, heh.

I’ve commented out the nav mesh code in the playground, but it’s interesting that I can create a nav mesh using the nav mesh extension and checkCollisions = true actually works for the nav mesh! You can try that by uncommenting my nav mesh code.

Any idea why checkCollisions might not work for imported models though? It works no problem with grounds or other shapes defined in Babylon, but curious for imported gltf models. @sebavan @RaananW

PS: the model was made in Blender. would one need to set anything on the Blender side in order for the checkCollisions to work on the Babylon side?

It is because you freeze everything before it is even computed: https://playground.babylonjs.com/#4HUQQ#1020

3 Likes

I imagine the facepalm I just did was loud enough to be heard wherever in the world you might be, @sebavan. Thanks. :slight_smile:

1 Like