Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有PhysiJS物理引擎的ThreeJS未触发碰撞事件_Javascript_Three.js_Collision Detection_Physics Engine_Physijs - Fatal编程技术网

Javascript 带有PhysiJS物理引擎的ThreeJS未触发碰撞事件

Javascript 带有PhysiJS物理引擎的ThreeJS未触发碰撞事件,javascript,three.js,collision-detection,physics-engine,physijs,Javascript,Three.js,Collision Detection,Physics Engine,Physijs,预期结果:一个盒子会掉在地上,它会产生一个警告框,上面写着“盒子刚落地” 发生的情况:未创建警报框。冲突时也不会生成相关的javascript控制台日志 我正在共享一个基于的小代码。您可以克隆它并在您的chrome浏览器中自己运行它。您可以检查源代码中*******文件中的physijsBox.addEventListener()部分 var sceneObj = (function(){ "use strict"; Physijs.scripts.worker = "sc

预期结果:一个盒子会掉在地上,它会产生一个警告框,上面写着“盒子刚落地”

发生的情况:未创建警报框。冲突时也不会生成相关的javascript控制台日志


我正在共享一个基于的小代码。您可以克隆它并在您的chrome浏览器中自己运行它。您可以检查源代码中*******文件中的
physijsBox.addEventListener()
部分

var sceneObj = (function(){

    "use strict";

    Physijs.scripts.worker = "scripts/physijs_worker.js";
    Physijs.scripts.ammo = "ammo.js";

    var scene, camera, renderer
    var physijsBox, physijsGround

    function initScene(){
        scene = new Physijs.Scene();
        scene.setGravity = new THREE.Vector3(0, -50, 0);

        camera = new THREE.PerspectiveCamera(35, window.innerWidth/window.innerHeight , 1, 1000);
        camera.position.z = 100;

        renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.getElementById("webgl-container").appendChild(renderer.domElement);

        addPhysijsBox();
        addPhysijsGround();
        render();
    }

    function addPhysijsBox(){
        var myBoxMaterial = Physijs.createMaterial(
            new THREE.MeshBasicMaterial({
                color: 0xff00ff
            }),
            0,  // friction
            0.8 // restitution / bounciness
        );
        physijsBox = new Physijs.BoxMesh(new THREE.CubeGeometry(15,15,15), myBoxMaterial);
        physijsBox.position.set(0,30,10);
        physijsBox.rotation.set(0,50,90);
        scene.add(physijsBox);

        physijsBox.addEventListener('collision', function(
            theOtherObject, linearVelocity, angularVelocity, arg4
        ){
            console.log("box collided with something");
            if (theOtherObject.name == "ground"){
                alert("Box just hit the ground");
            }
        })
    }

    function addPhysijsGround(){
        var myGroundMaterial = Physijs.createMaterial(
            new THREE.MeshBasicMaterial({
                color: 0x008888
            }),
            0, // friction
            0.4 // restitution / bounciness
        );
        physijsGround = new Physijs.BoxMesh(new THREE.CubeGeometry(150, 3, 150), myGroundMaterial, 0);
        physijsGround.name = "ground";
        physijsGround.position.y = -15;
        scene.add(physijsGround);
    }

    function render(){
        scene.simulate();
        renderer.render(scene, camera);
        requestAnimationFrame(render);
    }

    window.onLoad = initScene();
    return scene;

})();
相关文件:


我建议添加更新侦听器:

function initScene(){

    // Create scene object here...

    scene.addEventListener("update", function(){
        scene.simulate(undefined, 2);
    });

    // Rest of start function here...

    scene.simulate();
}
相应地调整渲染功能:

function render(){
    requestAnimationFrame(render);
    renderer.render(scene, camera);
}
哦,还有一个小错误在
窗口。onLoad

window.onload = initScene;
此外,请确保为移动的长方体指定质量,否则它将默认为零,从而使其不可移动

重新读取代码

我看到您有
.setGravity
作为属性。实际上,这是一个函数:

scene.setGravity(new THREE.Vector3(0, -50, 0));

为什么要将两个对象的摩擦力都设置为0?冲量的计算与摩擦系数有关。冲量大小是物理引擎用来检测碰撞的。这里给出了冲量计算中摩擦力的关系-你能试着给他们一些摩擦力吗?设置摩擦力不会有任何改善。如果拉动代码并运行它,您将看到冲突实际上正在发生。落下的箱子改变了路线,当它撞到地面时会四处颠簸。但是由于某些原因,它没有触发碰撞事件,因此碰撞事件可以被
physijsBox.addEventListener('collision',function(){})
Yep拾取。只是试着按照他们的一个例子编写不同版本的代码——甚至尝试添加两个对象并使它们发生碰撞。没用!似乎根本没有触发碰撞事件。hi@XavCo7。我试过你的建议。不幸的是,它们在应用程序中没有任何影响。您是否能够通过运行我共享的github代码使其以这种方式工作?@syedrakib实际上,我没有尝试运行代码repo。我觉得我可以通过看看你在设置中做错了什么来解决这个问题。我刚发现一些新的东西,请再次检查我的答案。