Physics, The ball goes through the terrain

I use this simple code, see. below. The ball(sphere) falls on the ground(terrain). The ball actually falls but passes through the terrain. There will be no bounces. Where is the mistake? Thank you for your time.

if (evt.sourceEvent.key == “f”) {

            //scene.enablePhysics(null, new BABYLON.CannonJSPlugin());

            scene.enablePhysics(null, new BABYLON.OimoJSPlugin());

            terrain.physicsImpostor = new BABYLON.PhysicsImpostor(terrain, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, friction: 0, restitution: 0.3});

            sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 1});

    }

OimoJS does not support the MeshImpostor. Only CannonJS and AmmoJS do.

https://www.babylonjs-playground.com/#7D74CT#1

It works!

Here is a nice documentation about physics with oimo:

As you wrote, that code works with basic objects. The problem will be in some detail. The imported mesh may have many polygons or something else. I was reading basic articles about the Physics, but I did not see a solution to this problem.

Can you reconstruct your problem in the playground?

So far I understood: There are only two types of physics imposters (box and sphere) which are used for physics calculations and surround your meshes. That means the complexity of the mesh structure is not considered. So the theory… But you are right in my casino game “Herbert” the coins fall through the ground, too, sometimes.

There are many reasons for that to happen. If the objects are too thin, if the balls are going too fast, if the steps or sub steps are not configured correctly.

Would be great to see an example in the playground, this will allow us all to better understand the situation

Hello there,

example is here:
https://playground.babylonjs.com/#42D30V

“If the objects are too thin”
What it means too thin? If objects are not thin then we wastink of count of polygon.

I suppose that the vertices are not correct after scaling…

Here I found maybe a solution for you:

I tried to modify the terrain object, I turned the scalling of the terrain model. I created a thicker model. I also saw a linked discussion. But the sphere does not work with the terrain, only simple objects work, such as planes. There may be a bad export of model from 3ds max. When I exported plane object from 3ds max, it is working.

One idea left… I would avoid the mesh-imposter and convert all the vertices of the terrain into flat planes… may be not all vertices rather than a surface mesh approximation to avoid cpu load.

https://doc.cgal.org/latest/Surface_mesh_approximation/index.html
(it’s not the right link but an idea what to look for)

There is some problem exporting from 3ds max, maybe. When I export plane object like 1 polygon only, it works properly…when I set more polygons of plane = it does not work…, very interesting. I also tried 3ds max default object - teapot. The sphere passes through the lid and the bottom but changes the motion vector, so there is some interaction, but not correct. There must be some export instructions.

As I wrote before, OimoJS does not support the MeshImpostor. It uses a box instead. So either use a BoxImpostor, or switch to a physics engine supporting the correct impostor-type, like CannonJS or AmmoJS: Use a Physics Engine - Babylon.js Documentation

Edit: Check this PG: Babylon.js Playground
Lines 21 - 23. Try OimoJS, and you’ll see the mesh isn’t used. Instead it uses the boundingBox of the model. CannonJS and AmmoJS both use the geometry.
Can be used with heightmap terrain as well: https://playground.babylonjs.com/#C0ILX9#1
(HeightmapImpostor only supported by CannonJS, OimoJS again uses boxes, and the AmmoJS plugin requires a MeshImpostor instead)

2 Likes

Thanks for info, its true, thanks… I did not get error message during debug process., why?

Then I tested Cannon engine, result from debugger:
Cannot read property ‘calculateWorldAABB’ of undefined

When I also tested Ammo engine, result from debugger:

Error: “abort(OOM). Build with -s ASSERTIONS=1 for more info.”
at abort (https://preview.babylonjs.com/ammo.js:27:256570)
at abortOnCannotGrowMemory (https://preview.babylonjs.com/ammo.js:10:51074)
at _emscripten_resize_heap (https://preview.babylonjs.com/ammo.js:10:51135)

Error: “abort(OOM). Build with -s ASSERTIONS=1 for more info.”
at abort (https://preview.babylonjs.com/ammo.js:27:256570)
at abortOnCannotGrowMemory (https://preview.babylonjs.com/ammo.js:10:51074)
at _emscripten_resize_heap (https://preview.babylonjs.com/ammo.js:10:51135)

As I cannot see the model loaded (i guess that it is a CORS issue when loading the model?)

As @Raggar wrote - Oimo does not support a mesh impostor. it falls back to a box impostor if I am not mistaken.

I also notices that you CAN initialize the physics before the model was loaded in your playground. the two are separated. Make sure you don’t initialize an impostor to an empty object. that can cause the issue you described in your last message. It can also cause a sphere to go through it, as the ground impostor doesn’t exist.

What i mean with “too thin” is a very simplified version of “if a physics step puts the object beyond the mesh, it will go through it”. A physics engine calculates the next steps on each frame. if the moving object passes the ground and doesn’t collide with it in one of those steps, it will not detect the collision and will simply go through it. But I am not sure it is the case here. Get us a working playground, we can take it from there

Hello RaananW, thanks for the response. I’m not currently solving the problem with a broken “mesh” Impostor. I solve the case when the ball passes through the mesh fully or partially. It does not fall but does not work properly. It’s in another thread.