Jump to content

box start moving and fall after created even when you doesn't apply any force at all


leon wang
 Share

Recommended Posts

i'm using babylon.js and oimo.js to build a simple scene which has boxes on top of another, there's 7 boxes, and after the scene was build the boxes starts moving and fall, i didn't apply any force and the gravity i'm sure is ok, anyone know what's the reason causing this? and how to stop the moving  thanks very very much!

Link to comment
Share on other sites

save button on playground not working fro me right now, can you guys please copy below codes and try? cubes will fall in 20 secs normally.

thanks.

BTW, love your show on MVA :)

 

var createScene = function () {

    var scene  = new BABYLON.Scene(engine);

    var gravityVector = new BABYLON.Vector3(0, -9.81, 0);
    var physicsPlugin  = new BABYLON.OimoJSPlugin();
    scene.enablePhysics(gravityVector, physicsPlugin);

    var ground  = BABYLON.Mesh.CreateGround("ground", 200, 200, 100, scene);
    var mat1 = new BABYLON.StandardMaterial("mat1", scene);
    mat1.diffuseColor = BABYLON.Color3.Gray();
    mat1.wireframe = true;
    ground.material = mat1;
    ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, scene);

    //build cubes
    var mat_cube1 = new BABYLON.StandardMaterial("mat_cube1", scene);
    mat_cube1.diffuseColor = BABYLON.Color3.White();

    var mat_cube2 = new BABYLON.StandardMaterial("mat_cube2", scene);
    mat_cube2.diffuseColor = BABYLON.Color3.Black();

    var p = {x:0,y:0,z:-40};
    [10,9,8,7,6,5,4,3,2,1].forEach(function (y) {
        [1,].forEach(function (x) {
            [1,].forEach(function (z) {
                var box = BABYLON.Mesh.CreateBox("cube_" + x + y + z, 1, scene);
                box.position = new BABYLON.Vector3(p.x + x , y - 0.5 , p.z + z );
                //mat2.wireframe = true;
;
                //set physic
                box.physicsImpostor = new BABYLON.PhysicsImpostor(box, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0.1,friction:1, restitution: 0.1 }, scene);
                box.physicsImpostor.physicsBody.allowSleep = true;
                //box.physicsImpostor.physicsBody.isStatic = true;


                if ((x + y + z) % 2 == 0) {
                    box.material = mat_cube1;
                }
                else {
                    box.material = mat_cube2;
                }
            })
        });
    });


    //create light
    var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 10, 0), scene);
    light.intensity = 1;


    //create camera
    var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(10, 15, -80), scene);
    camera.setTarget(new BABYLON.Vector3(-5,-10, 0));
    camera.attachControl(canvas, false)

    return scene;
};

 

Link to comment
Share on other sites

Is it the same with cannon.js?

Indeed this is a problem of many physic engines. They handle the forces of overlaying boxes like if they fall onto each other although they already are in contact for a while. And due to floatingpoint operations there are always some numbers that had to be shortened, so you get some effects that you don't wanna have. The best would be that the engine recognizes the stable position of the boxes and would then reduce the calculation down to zero until the boxes move again. Don't know if there is an algorithm for that but ... obviously it should give one. :)

Link to comment
Share on other sites

On 4/15/2017 at 8:53 PM, jellix said:

Is it the same with cannon.js?

Indeed this is a problem of many physic engines. They handle the forces of overlaying boxes like if they fall onto each other although they already are in contact for a while. And due to floatingpoint operations there are always some numbers that had to be shortened, so you get some effects that you don't wanna have. The best would be that the engine recognizes the stable position of the boxes and would then reduce the calculation down to zero until the boxes move again. Don't know if there is an algorithm for that but ... obviously it should give one. :)

in cannon.js you can set allowsleep = true of cannonworld  to avoid this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...