n5ro / aframe-physics-system

Physics system for A-Frame VR, built on CANNON.js.
https://n5ro.github.io/aframe-physics-system/
MIT License
504 stars 137 forks source link

Inconsistent results from static-body collisions #8

Closed Toseben closed 7 years ago

Toseben commented 7 years ago

I'm facing a issue when trying to create collision geometries with static-body flag. I have created a custom loader component to update the loading screen progress bar.

However when using it, the results from static-body are inconsistent. Sometimes on page load both object don't have collisions, sometimes one has and sometimes both do. This is most likely something to do with the loading order of objects.

<!-- Geometry -->
<a-entity objloader='file: ceilingBaked' 
          position="0 0 0"
          static-body="shape: auto"></a-entity>
<a-entity objloader='file: interiorBaked' 
          position="0 0 0"
          static-body="shape: auto"></a-entity>
init: function () {

    thisElement = this.el.object3D;
    var objName = this.data.file;

    // Loader //
    loader = new THREE.OBJLoader(manager);

    loader.load( "geo/" + objName + ".obj", function ( object ) {

        var material = new THREE.MeshBasicMaterial();
        loader = new THREE.TextureLoader(manager);

        object.traverse( function ( child ) {
            if (child instanceof THREE.Mesh) {
                child.material = material;
                child.material.map = loader.load( "img/" + objName + '_nonCompressed' + ".jpg" );
                addGeo = child;
            }   
        } );
    } );
}

The whole page is here: https://toseben.github.io/interactive_webViz/aframe_defineComponent.html

I already fixed the issue by approaching the loading differently, by using with a custom shader instead. https://toseben.github.io/interactive_webViz/aframe_collision.html

donmccurdy commented 7 years ago

The built-in obj-model and collada-model components fire an event when they finish loading, something like this:

el.emit('model-loaded', {format: 'obj', model: object});

If the model isn't available immediately, the physics system will wait until the scene loads to initialize. The problem you're seeing, most likely, is that the model is inconsistently loaded before or after the scene. So, you'll need to fire an event like that, after adding the model, to let the physics system know it can check again.