从巴比伦跑 - 重力,对撞机,一切都行不通

时间:2015-09-11 21:48:41

标签: 3d game-physics mesh babylonjs collider

所以,我遇到了babylon.js的问题。我正在尝试做一个简单的例子,我在我的玩家对象和地面上有箱子碰撞器,物理应用于重力和碰撞。我已经没想到我可能做错了什么。请帮忙!我将提供游乐场链接,因为人们不认为我们使用它,以及原始代码。

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

原始代码:

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 个答案:

答案 0 :(得分:1)

实际上碰撞(由摄像机用来模拟与环境的碰撞)和物理是分开的引擎。

您可以在此处找到碰撞样本:http://www.babylonjs-playground.com/?9。你可以看到你可以四处闲逛,因为碰撞已经启用。

如果你想模拟物理(刚体之间的相互作用),你差不多完成:)你需要添加:`player.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor,{mass:1,friction:0.5,恢复原状:0.7 });

无需启用碰撞。

这是一个完整的物理示例:https://github.com/BabylonJS/Samples/blob/master/Scenes/Customs/physics.js `