Optimized way to detect 2 detailed models collision

Hello, I am trying to do optimized code for 2 detailed models collision detection.
1 model has a lot of vertexes, so with raycast I can’t do it normally. I need solution which moves object +1 to z-coord until it collides.

image

Thanks for your time.

It looks like a mission for three-mesh-bvh.
It helps raycasting performance against high poly meshes.

3 Likes

I was going to suggest Box3 but wow yeah, that’s a very cool lib there. Garrett strikes again with another win!

2 Likes

Yea, it’s really nice lib. Could you give any examples how to use it? Because I can’t find any examples that wouldn’t be minified or obfuscated .

The unminified examples are available in the /example folder of the project. If you have something specific you’re stuck on or have a question about I can point you in the right direction.

1 Like
             function zPut(glass, head){
                console.log(glass.position);
                let hit;
                const transformMatrix = new THREE.Matrix4().copy( head.matrixWorld ).invert().multiply( glass.matrixWorld );
                for(let i = 0; i < 12; i++){
                //    glass.applyMatrix4(new THREE.Matrix4().makeTranslation(position.x, position.y, position.z-i));
                    glass.position.z -= i;

                    console.log(glass.children[0].geometry);
                    hit = head.children[0].geometry.boundsTree.intersectsGeometry( head, glass.children[0].geometry, transformMatrix );
                    console.log(hit);

                    glass.material.color.set( hit ? 0xE91E63 : 0x666666 );
	                glass.material.emissive.set( 0xE91E63 ).multiplyScalar( hit ? 0.25 : 0 );  

                    console.log(glass.position.z-i);
                }
            }

I have this code, I was following three-mesh-bvh/shapecast.js at 97e32fdc1c2b6ffd04ae7830c2b3182ef14707e9 · gkjohnson/three-mesh-bvh · GitHub example, but I am getting this error:

Thanks for your time!

When calling the intersectsGeometry function and generating the local matrix transform its important that you use the mesh with the geometry that the BVH was generated for. In your case that’s head.children[ 0 ] rather than head, which looks like it’s a Group.

These two lines need to change to use head.children[ 0 ] rather than head:

const transformMatrix = new THREE.Matrix4()
    .copy( head.children[ 0 ].matrixWorld )
    .invert()
    .multiply( glass.matrixWorld );

and

hit = head.children[0].geometry.boundsTree
    .intersectsGeometry(
        head.children[ 0 ],
        glass.children[0].geometry,
        transformMatrix
    );

In an upcoming version of the library you won’t need to a mesh object into intersectsGeometry which will simplify things a bit but that version hasn’t been published yet.

1 Like

Thanks, error has vanished!
But I have problem, that I am only getting hit: false. My models merged with each other, so I can’t understand what I am doing wrong. Matrix is always updating…

           function zPut(glass, head){
                console.log(glass.position);
                let hit;

                let transformMatrix = new THREE.Matrix4().copy( head.children[ 0 ].matrixWorld ).invert().multiply( glass.matrixWorld );

                for(let i = 0; i < 12; i++){
                    glass.position.z -= i;
                    glass.updateMatrixWorld();

                    transformMatrix = new THREE.Matrix4().copy( head.children[ 0 ].matrixWorld ).invert().multiply( glass.matrixWorld );

                    hit = head.children[0].geometry.boundsTree
                    .intersectsGeometry(
                        head.children[0],
                        glass.children[0].geometry,
                        transformMatrix
                    );
                    
                    console.log("HIT: " + hit);
                }
            }

Can you provide a jsfiddle or other demo of the issue?

Can I contact with you in private? Because I can’t really show much demo. But I can share code where I load models

The issue is that when you load the model you compute the bounds tree and then modify the geometry with applyMatrix which invalidates the BVH. It should work if you compute the bounds tree after the geometry modifications.

Thanks to @gkjohnson

1 Like