Newbie question: mesh import

Hello everybody,

I have another beginner question and I’m pretty sure that I’m again missing some small thing. Maybe the kind folks here can give me a hint.

I use the glb file format to import meshes from Blender into a Babylon.js project which works fine. There is just one thing that doesn’t work as expected: Although I have activated collison detection in both the camera and the meshes the camera moves right through the mesh. There is no collison detected. It gives this effect that we know from badly programmed computer games when the camera dives through the surface of objects instead of stopping.

I have tried exporting the meshes both with physics for collison detection and solid body enabled. But it didn’t change anything. The camera interacts as expected with meshes created internally with the babylon.js mesh builder. So there’s clearly something I’m missing when importing meshes.

Here’s a code snippet that shows how I import the meshes:

BABYLON.SceneLoader.ImportMeshAsync(“Central Pool”, “models/”, “ThePersianGarden_CentralPool.glb”, scene).then((result) => {

var centralPool = result.meshes[0];
centralPool.position.x = 0;
centralPool.position.z = 0;
centralPool.position.y = -0.8;
centralPool.checkCollisions = true;

    })     

Maybe I have to set a solid body option for the imported mesh?

Thanks in advance for your help!
Hayden

GLB and gltf meshes import with an added root mesh/node that you need to traverse to get to the actual mesh that will do collisions and such.

Try looking at the imported mesh in the Inspector and you’ll see what I mean.

in summary:

Access the mesh with result.meshes[0].children[0] or similar

Hello Jelster,

thanks for your reply.

I have checked the above example in the Scene Explorer and Inspector of the Babylon.js sandbox. The imported file has a root node and a node with the mesh name. But simply accessing it with

var mesh = result.meshes[0].children[0];

doesn’t do the trick. The mesh is set into the scene but no collision is detected, even when I set mesh.checkCollisions = true;

What else do i have to do? My goal is that neither the camera nor (later) moving objects can simply move through the mesh’s surface.

All the best
Hayden

    const root = res.meshes[0]
    const childMeshes = root.getChildMeshes()

    for (let mesh of childMeshes) {
        mesh.checkCollisions = true
    }

Example - https://playground.babylonjs.com/#0VHCTL#26

2 Likes

Can you post a PG of the code you’re using? I’m on my phone atm, so might have missed where you mentioned that you’ve enabled collisions on the scene itself? Fwiw, that can also be toggled via the Inspector as an easy way to test the theory

HTH!

What helped me from camera clipping into meshes was to set

camera.minZ to value like 0.5 or higher.

My ArcCamera and setting:

scene.collisionsEnabled = true;
camera.checkCollisions = true;
for(const mesh of task.loadedMeshes) { // can be irritating because I use assetManager instead importMesh-function
mesh.checkCollisions = true;
}
camera.minZ = 0.01 (my regular models don't allow to go lower here, also got dynamic switching depending on camera.lockedTarget)
camera.collisionRadius = new BABYLON.Vector3(0.02,0.02,0.02)

My UniversalCamera got added:
camera.ellipsoid = new BABYLON.Vector3(0.04,0.04,0.04)

Not necessary, jelster. labris’ code has got me on the right track. I adapted it to my code and the camera behaves exactly as I want it now. It does not move through surfaces, it bumps over the object. Great!

If I understand this correctly, the imported mesh is composed of several child-meshes and I have to set collision detection for each one of them seperately with the for-loop. I will have to modify a few import functions in the scene, but that’s no big deal.

Thanks to all who have answered me here. We can regard this problem as solved.

Hayden

I would like to add that mesh geometry, number of meshes, and arrangement of those meshes can determine if you should add collision checks for each child mesh.

Number of meshes obviously affects the performance, while the other two can lead to some issues, mostly resulting in camera/object getting stuck in some corner (…so many times xD)

What I usually like to do is to create collision objects separately. And I use only boxes to achieve that. here is an example of that:



So instead of setting collisions for each individual mesh I am creating box and wrapping the whole object. I usually load these collision boxes separately using AseetsManager, but you can keep it as part of your mesh. So in the code you just set this box to isVisible = false and checkCollisions = true.

It’s more stable, and more performant.

One more example are walls, floors, door spaces, all of those narrow corners can give you some issues, and you getting stuck inside. So what I do, is I analyze the scene and define where collisions should occur, then I just create boxes to act as “walls” or “barrier” in those places. So it would look something like this for a simple house-like object.

This is probably not necessary in every use case, if your objects are simple enough (like, if those are already box-shaped, sphere, or some other primitive) then there is probably no need to wrap them with separate objects.

3 Likes

Not necessarly. A common technique is to set a collision box/mesh that accounts the bounderies of all submeshes. Thoroughly explained and detailed by @nogalo above (GJ :smiley:)

If so, would you mind to just tick the ‘solution box’. It’s good practice to do so if you are happy with the solution you found (or someone else gave you). Else, people will continue to see the topic and will eventually take time and effort to try solve the problem. Thanks in advance and have a great day :sunglasses: :sun_with_face:

2 Likes

Sorry, mawa. I have overlooked this. I will do so and keep that in mind for coming occasions.

Many thanks!

No need to apologize and thanks for your understanding. You are warmly welcomed here.
Have a great day :smiley: :parasol_on_ground:

Hello nogalo,

I will keep your considerations in mind for cases when I don’t need detailed collision checking and a bounding box would be sufficient.

I’m building a walkthrough scence and the ground segments I had problems with are more or less flat boxes with some channels and pools, rather simple. When I put up trees, however, it would be nonsense to perform collision checks for every single leaf. Better use a transparent column around the stem with collisions checking. For groups of rocks or small plants I could use a box, as you discribed. With a little planing it should not significantly affect the movement through the scene.

Thanks for all the valuable explanations in this thread!

Hayden

1 Like