0

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

链接: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();
    });
});
4

1 回答 1

1

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

您可以在此处找到碰撞示例:http ://www.babylonjs-playground.com/?9 。您可以看到您可以四处走动,因为启用了碰撞。

如果你想模拟物理(刚体之间的相互作用),你几乎完成了:) 你需要添加:`player.setPhysicsState(BABYLON.PhysicsEngine.BoxImpostor, { mass: 1,friction: 0.5, restitution: 0.7 });

无需启用碰撞。

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

于 2015-09-12T14:27:41.773 回答