Calculate bounding box

I have a next game:

How to make the ball fly through the hole?
I consider collisions like this:

if (platforms[i].intersectsMesh(Ball)) {

What you want in this case is a mesh colission instead of the bounding box as the box is calculated around your whole platform object. Or maybe you could divide your platforms into chunks to use the boxes.

For mesh colission it might be worth looking at Mesh Physics Impostors.

// Just saw this thread from yesterday that might be relevant here, too. Physics Performance MeshImpostor vs multiple PrimitiveImpostors

I would follow what @GreyWorks suggests. We did something similar for a Sony Game. Setting up mesh collisions is basically a bounding box around meshes with only collision properties. But they are not conditional on a physics engine. So when a collision is detected, we altered the behavior of an animation. It worked great.


Ok, Thanks!

But then immediately another question. Can I make the sphere fall only down and not roll off the platforms. In other words, for the vector of physics to work exclusively on the Y axis?

Yes you can, though not natively with babylon.

If you decide to use ammo.js as physics plugin, you can do it with the following functions.

//stops rolling
ballMesh.physicsImpostor.physicsBody.setAngularFactor( Ammo.btVector3(0, 0, 0) );
//prevents sliding
ballMesh.physicsImpostor.physicsBody.setLinearFactor( Ammo.btVector3(0, 1, 0) );

1 Like

Thank! Is there an example for cannonjs?
I ask because in my version of babylonjs there is no ammo engine and the ball object does not have the necessary methods.

You won’t get code completion for custom physics function as these have any as type.

Read through the Babylon 101, they explain how to setup ammo.js there.

There seems to be something named linearDamping or linearFactor but I olny read about that in github comments either use ammo or do some research about these properties if you want to use cannon.

Sorry! Can you give link about installing ammo.js in babylonjs?
Just Use new BABYLON.AmmoJSPlugin();
instead of new BABYLON.CannonJSPlugin();

And remember to add ammo to your html file with a script element.

Ok, but i have some errors like that.


in result next error:

Ammo.Js get from:

What i do wrong?

You probably used the V3 version of ammo. Ammo is only supported in V4.

I did it! Now all working. Just need change babylon.js version to 4.0
But sphere rolling when collide with platrofm. How stop rolling?

//stops rolling
ballMesh.physicsImpostor.physicsBody.setAngularFactor( Ammo.btVector3(0, 0, 0) );
//prevents sliding
ballMesh.physicsImpostor.physicsBody.setLinearFactor( Ammo.btVector3(0, 1, 0) );

This not working :frowning:

Could you share a playground for that?

Project is biggest and have many files. But i try give code here…

Game.Scene.enablePhysics(null, new BABYLON.AmmoJSPlugin());
        Game.Scene.actionManager = new BABYLON.ActionManager(Game.Scene);
        Game.Ball           = TModels.generateBall();
        Game.Ball.position  = new BABYLON.Vector3(-2.55,10,0);
		Game.Ball.physicsImpostor = new BABYLON.PhysicsImpostor(Game.Ball, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 0});
        Game.Ball.physicsImpostor.physicsBody.setAngularFactor( Ammo.btVector3(0, 0, 0) );
		Game.Ball.physicsImpostor.physicsBody.setLinearFactor( Ammo.btVector3(0, 1, 0) );

// platforms is imported fbx models.
//  TModels.discs << array of models

var disc = TModels.discs[rnd].clone(); // get random model from array
                        disc.visibility = true;
                    disc.checkCollisions = true;
					disc.physicsImpostor = new BABYLON.PhysicsImpostor(disc, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0, friction:8, restitution :0});

is this enough?

My fault it is
new Ammo.btVector3(x, y, z), I forgot the new.

Here a working playground example:

For BoxImpostor working! But i use MeshImpostor in platforms and this code not work. Ball still rolling :frowning:

where I use the boxImpostor sphere does not fly to empty places :frowning:

When boxImpostor:

I changed the ground to using a mesh imposter. It’s working, so there must be an error in your code:

EDIT: Even if I give the sphere a meshImposter too, it’s working.

Game.Ball.physicsImpostor = new BABYLON.PhysicsImpostor(Game.Ball, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 0});

Your sphere imposter needs a mass.

1 Like

YES! If anybody get this error…

I did not set the mass for the sphere initially, as I did it later through the browser console. And as it turned out - the mass needs to be asked right away.

mauricedoepke, thank you very much!

1 Like