2

Screenshot of falling box

Expected outcome: a box will drop on the ground and it will produce an alert box saying "Box just hit the ground"

What's happening: alert box is not being created. Relevant javascript console logs are also not being produced upon collision.


I am sharing a small code base on my github repo. You can clone it and run it yourself in your chrome browser. You can inspect the physijsBox.addEventListener() portion in the ****scripts/app.js**** file in the source code.

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;

})();

Relevant PhysiJS documentation:

Rakib
  • 12,376
  • 16
  • 77
  • 113
  • Why are you setting friction for both of the objects to 0? The calculation of `impulse` is related to co-efficient of friction. Impulse magnitude is what physics engines use to detect a collision AFAIK. The relation of friction in impulse calculation is given here - http://www.euclideanspace.com/physics/dynamics/collision/threed/ – Munim Nov 10 '16 at 16:39
  • Can you try giving them some amount of friction may be? – Munim Nov 10 '16 at 16:43
  • Setting the friction doesn't make any improvement. If you pull the code and run it, you will see that the collision is actually taking place. The falling box changes its course and bumps around when it hits the ground. But for some reason, it is not firing the collision ***EVENT*** so that the collision event can be picked up by the `physijsBox.addEventListener('collision', function(){})` – Rakib Nov 10 '16 at 17:10
  • Yep. Just tried it out writing a different version of the code following one of their examples - https://gist.github.com/dibosh/b7055c6a41a127814e86ad30f53f3384 Tried even adding two objects and making them collide. Didn't work! Seems like the `collision` event doesn't get triggered at all. – Munim Nov 10 '16 at 18:28

1 Answers1

0

I recommend adding an update listener:

function initScene(){

    // Create scene object here...

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

    // Rest of start function here...

    scene.simulate();
}

Tweak your rendering function accordingly:

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

Oh, and one tiny mistake at window.onLoad:

window.onload = initScene;

Also, make sure you specify your mass for your moving box or else it will default to zero, which makes it immovable.

Re-reading code...

I see you have .setGravity as a property. Well, it's actually a function:

scene.setGravity(new THREE.Vector3(0, -50, 0));
  • hi @XavCo7 . I have tried your suggestions. Unfortunately they didn't have any impact in the application. Were you able to make it work this way by running the github code that i shared? – Rakib Nov 16 '16 at 05:19
  • @syedrakib actually, I did not try and run the code repo. I felt I could just solve it by looking at what you did wrong in your setup. I just found something new, please check my answer again. –  Nov 16 '16 at 21:05