0

So, I'm having trouble with babylon.js. I'm trying to make a simple example where I have box colliders on my player object and the ground with physics applied for gravity and collision. I've run out of ideas as to what I could be doing wrong. Please help! I'll provide the playground link since people don't think we use it, along with the raw code.

link: http://www.babylonjs-playground.com/#1PK6ED#1

Raw Code:

window.addEventListener('DOMContentLoaded', function(){

    var canvas = document.getElementById('canvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    var engine = new BABYLON.Engine(canvas, true);

    var createScene = function(){
        //var gravity = parseFloat(0.1);

        var scene = new BABYLON.Scene(engine);
        var camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene);
        camera.attachControl(canvas, false);
        var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0,1,0), scene);

        scene.enablePhysics(new BABYLON.Vector3(0, -10, 0), new BABYLON.OimoJSPlugin());        
        scene.collisionsEnabled = true;

        var player = BABYLON.Mesh.CreateBox("player",2,scene);
        player.position = new BABYLON.Vector3(0,20,0);
        player.checkCollisions = true;

        var wing = BABYLON.Mesh.CreateBox("wing",2,scene);
        wing.position = new BABYLON.Vector3(0,0,0);
        wing.scaling.x = 2;
        wing.scaling.y = .3;
        wing.checkCollisions = true;

        wing.parent = player;
        camera.parent = player;

        var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "data/images/heightMap_jpg.jpg", 400, 400, 500, 0, 10, scene, false);

        var meshesColliderList = [];

        for (var i = 1; i < scene.meshes.length; i++) {
            if (scene.meshes[i].checkCollisions && scene.meshes[i].isVisible) {
            scene.meshes[i].setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0.7 });
            meshesColliderList.push(scene.meshes[i]);
            }
        }
        console.log(meshesColliderList);

        return scene;
    }

    var scene = createScene();

    engine.runRenderLoop(function(){
        scene.render();
    });

    window.addEventListener('resize', function(){
        engine.resize();
    });
});

1 Answers1

1

actually collisions (used by the camera to simulate collisions with environment) and physics are separated engine.

You can find a sample of collisions here:http://www.babylonjs-playground.com/?9. You can see that you can wander around because collisions are enabled.

If you want to simulate physics (interactions between rigid bodies), you are almost done :) You need to add: `player.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, { mass: 1, friction: 0.5, restitution: 0.7 });

No need to enable collisions then.

Here is a complete physics example: https://github.com/BabylonJS/Samples/blob/master/Scenes/Customs/physics.js `

David Catuhe
  • 1,747
  • 1
  • 10
  • 9
  • I should have asked this before, but what I'm trying to do is simulate bird flight with the box I'm using. What would be the best way to do this? I was think I could change the gravity on the box when the user clicks or something, but I bet there's an easier way that I haven't seen. – jermaine berkley Sep 13 '15 at 22:35
  • You should keep the gravity constant and instead apply an impulse from bottom to top on your box using box.applyImpulse() – David Catuhe Sep 14 '15 at 23:04
  • I'm applying the impulse, but I can't figure out how to do it from the bottom, my cube just hops on the x, and z, but no y. – jermaine berkley Sep 24 '15 at 01:44
  • This should be something like (0,20,0) in order to compensate gravity – David Catuhe Sep 25 '15 at 14:09