Search code examples
javascriptgame-physicsaframe

aframe-physics-extras: collision of dynamic entity with static gltf model


I'm trying to catch collision event between a dynamic sphere and a static gltf model. I'm building the gltf entity the following way:

    const template = document.createElement('a-entity');
    template.setAttribute('physics-collider', 'ignoreSleep: false');
    template.setAttribute('collision-filter', 'collisionForces: false');
    template.setAttribute('body', 'type:static; shape:hull');
    // add position, scale, url etc
    // ...
    template.addEventListener('collisions', e => {
        // debugger;
    })

This code creates an entity, but there is no wire net around it in debug mode and the collisions event is not being fired. For debug purposes I tried different shapes. This creates a cylinder around the entity, but it seems too big. The dynamic shape crosses the cylinder, but the collisions event is being fired not always, rather rare.

    template.setAttribute('body', 'type:static; shape:cylinder');

Then I tried to build the shape manually:

    template.setAttribute('body', 'type:static; shape:none');
    template.setAttribute('shape', 'shape: cylinder; height: 5;');

In this case I'm getting the following error:

Cannot read property 'bodyOverlapKeeper' of null at NewComponent.<anonymous> (physics-collider.js:34)

So now I'm stuck. Could someone advice what I'm doing wrong. I'd like to use the shape of the gltf model itself. I opened it in blender, but it seems okay, I don't understand why shape:hull doesn't work.

P. S. : if it matters, I'm using webpack


Solution

  • Meanwhile I was able to implement animated collision mesh for a gltf model. I used a helper function implemented by @Piotr Adam Milewski here. See more details about the function in this answer. Any other suggestions to improve performance are appreciated.

    AFRAME.registerComponent('animated-collision-shape', {
        init: function () {
            this.nodeMap = {};
    
            this.el.setAttribute('body', 'type: static; shape: none;')
    
            this.el.addEventListener('model-loaded', () => {
                const size = new THREE.Vector3();
                let box = new THREE.Box3().setFromObject(this.el.object3D);
                box.getSize(size);
                this.offset = new CANNON.Vec3(0, size.y / 2, 0);
    
                let mesh = this.el.getObject3D("mesh");
                mesh.traverse(node => {
                    if (node.isSkinnedMesh) {
                        this.skinnedMesh = node;
                        this.nodeMap[node.uuid] = {
                            mesh: node,
                            box: new THREE.Box3()
                        }
                    }
                });
    
                if (!Object.keys(this.nodeMap).length) {
                    this.nodeMap[0] = {
                        mesh: this.el.object3D,
                        box: new THREE.Box3()
                    };
                }
    
                this.el.components["body"].shouldUpdateBody = true;
            })
        },
    
        remove: function () {
            this.removeBoxes();
        },
    
        tick: (function () {
            const size = new THREE.Vector3();
            let common_box_uuid = null;
    
            return function tick() {
                if (
                    !Object.keys(this.nodeMap).length ||
                    !this.el.body) {
                    return;
                }
    
                let combine = this.data.combine === true
    
                let i = 0;
                for (let uuid in this.nodeMap) {
                    // Non - skinned case
                    if (!this.nodeMap[uuid].mesh.isSkinnedMesh) {
                        this.nodeMap[uuid].box.setFromObject(this.el.object3D);
                        return;
                    }
                    // skinned model. Either separate boxes, or combined
                    if (common_box_uuid && combine) {
                        utils.SkinnedMeshBBox.expandAABB(this.nodeMap[uuid].mesh, this.nodeMap[common_box_uuid].box);
                    } else {
                        utils.SkinnedMeshBBox.getAABB(this.nodeMap[uuid].mesh, this.nodeMap[uuid].box);
                        common_box_uuid = uuid
                    }
    
                    if (isFinite(this.nodeMap[common_box_uuid].box.max.x)) {
                        this.nodeMap[common_box_uuid].box.getSize(size);
                        if (this.el.body.shapes[i]) {
                            this.el.body.shapes[i].halfExtents = new CANNON.Vec3(size.x / 2, size.y / 2, size.z / 2);
                            this.el.body.shapes[i].updateConvexPolyhedronRepresentation();
                        } else {
                            let shape = new CANNON.Box(new CANNON.Vec3(size.x / 2, size.y / 2, size.z / 2))
                            this.el.body.addShape(shape, this.offset, shape.orientation);
                        }
                        i++;
                    }
                }
    
                this.el.components["body"].shouldUpdateWireframe = true;
            };
        })()
    })