Question to collision and mesh behaviour - sphere, box and ground

I am a Newbie to Babylon.js - hence is this a cool and great tool :smiley: - i created a scene with a ground, a box and a sphere. I figured out how to implement physics, gravity and collision detection to ground and sphere, and even am showing their boundingboxes.

If sphere falls through the ground it turns from green to red. So far so good. I don’t want it to fall through the ground - but on the ground :smiley: therefore I tried to implement an animation to start a movement of sphere if she hits the box that lies on the ground - but actually, sphere just pretends to keep on falling :smiley:

No clue what to do - so I got 2 questions:
a) how can i just let my sphere fall on the ground?
b) if impossible, how can I get my box solid, so that sphere won’t fall through? :))

Hey @Markus_Zeitlhofer! Could you check out Cameras, Mesh Collisions and Gravity - Babylon.js Documentation ? Using moveWithCollisions with an ellipsoid set and setting checkCollisions = true for other meshes is one way to accomplish this.

Also you could look into using a physics engine. I’m not sure who created this awesome demo: Babylon.js Playground

1 Like

thanks a lot for your guidance and fast response :slight_smile:
I already added an elipsoid to my sphere (?) and set checkCollissions to true - as well for the box as for the sphere - but I am still clueless for nothing happens :smiley:

I also found a demo on the playground which is pretty much what I need,
but actually I am unable to rebuild in my existing scene :smiley: If i try just ends up in a white screen, so I am of the opinion that I might include another library to accomplish or anything else :)) any assistance would be highly preferred :slight_smile:

https://www.babylonjs-playground.com/#4PFRCY#6

Could you look at your console log? Maybe there is a crash somewhere. You can open the console log in most browsers by pressing Ctrl + Shift + i.

so console is completley clear and empty, hence a warning shows up if i refresh the page: “A cookie associated with a cross-site resource at http://babylonjs.com/ was set without the SameSite attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at [yadayadayada] and https://www.chromestatus.com/feature/5633521622188032.W

Guess i just mixed something up, trying to set up the playground scene in a local instance to analyse and learn :smiley:

1 Like

ok so I came further with this :smiley: actually I have a sphere and a ground. The sphere has a boundingSphere (yay, what else?) and the ground has a boundingBox. If the intersect each other, the sphere changes it’s color. YAY :smiley:

But still sphere keeps pretending it’s most fun to fall straight into infinity, instead of bouncing or stopping or doing anything else when it intersects ground’s bounding box :smiley:

What can I do to change this? :smiley:

Here my snipplet of code - I just built this together from playground :smiley:

sphere.ellipsoid.checkCollisions = true;
//sphere.ellipsoid.moveWithCollisions(forwards);
sphere.getBoundingInfo().boundingSphere.scale(0.32);
myGround.getBoundingInfo().boundingBox.scale(1);
sphere.showBoundingBox = true;
myGround.showBoundingBox = true;
myGround.checkCollisions = true;
    

var alpha = 0;

engine.runRenderLoop(()=>{
    alpha += 0.01;

if( myGround.getBoundingInfo().boundingBox.intersectsSphere(sphere.getBoundingInfo().boundingSphere)){
newMat.diffuseColor = new BABYLON.Color3(1,0,0);
}else{
newMat.diffuseColor = new BABYLON.Color3(0,1,0);
}
sphere.position.x = 2.8- Math.sin(alpha);

});

Hey @Markus_Zeitlhofer, moveWithCollisions is commented out in your code. That method is required if you don’t want objects to pass through each other.

Changing a mesh’s position by changing position.x will move a mesh through other meshes even if checkCollisions = true.

thanks a lot :slight_smile: i already figured out what worked for me :smiley:

I simply added a query that sphere.physicImpostor.mass gets 0, if sphere hit’s ground :smiley: So it dwells on the board, but i still can throw it off :smiley:

So what I did was:

 if(myGround.getBoundingInfo().boundingBox.intersectsSphere(sphere.getBoundingInfo().boundingSphere))
 {

        sphere.physicsImpostor.mass = 0;
    }else{
         sphere.physicsImpostor.mass = 1;
    }

I gave the moveWithCollisions a test yesterday but did not notice any difference in sphere’s behaviour. However, I will be keeping in learning and testing - thanks a lot for support and advice. Even though it might not look so, it helped me a lot.,

1 Like