Collision detection in TypeScript with Box3 and Sphere is not working

Hi.

The worst problems are for me if something does run without an error but simply does not work. So maybe someone can help me and tell me what i am doing wrong in my code.

Basically i am trying to detect collisions and have found a solution at the mozila dev side. Unfortunately it is not working so i think i have a misunderstanding here.

In my typescript code i have done the following:

    /*
     constructor 
     Param: sceneFile : string : path with filename to load 
     */
    constructor(private gameObject: any) {
        this._scene = new Scene();
        this._scene.add(gameObject);
        this.arrOfObstcls = new Array<object>;

        // https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection/Bounding_volume_collision_detection_with_THREE.js
        // expand THREE.js Sphere to support collision tests vs Box3
        // we are creating a vector outside the method scope to
        // avoid spawning a new instance of Vector3 on every check
        Sphere.__closest = new Vector3();
        Sphere.prototype.intersectsBox = function (box: Mesh) {
        // get box closest point to sphere center by clamping
        Sphere.__closest.set(this.center.x, this.center.y, this.center.z);
        Sphere.__closest.clamp(box.min, box.max);
        const distance = this.center.distanceToSquared(Sphere.__closest);
        return distance < this.radius * this.radius;
        };
    }

    private collision() {
        const playerBox = this._scene.getObjectByName('PlayerBoxObj');
        playerBox.geometry.computeBoundingBox();
        const knotBBox = new Box3(
            playerBox.geometry.boundingBox.min,
            playerBox.geometry.boundingBox.max
          );

        const specialPill = this._scene.getObjectByName('Pill3Obj');
        specialPill.geometry.computeBoundingSphere();
        const knotBSphere = new Sphere(specialPill.position, 
                                       specialPill.geometry.boundingSphere.radius );

        if(knotBSphere.intersectsBox(knotBBox)) {
            console.log('Kontakt !!!');
        }
    }

the collision method is called in my update.function. But the collision is not logged to the console. What is the problem ?

Kind regards and happy new year :slight_smile:

Try using intersectsSphere afaik spheres use a spherical bounding volume which may be part of the issue but you may also need to call the function as

if(knotBSphere.boundingBox.intersectsBox(knotBBox))