src/base/Level.js
/**
     * Adds a collider to the level scene. It will fire the options.onCollide callback
     * when the collider intersects options.collisionMesh. It can be used to fire actions when
     * player enters an area for example.
     * @param {*} name 
     * @param {*} options 
     */
Adds a collider to the level scene. It will fire the options.onCollide callback
when the collider intersects options.collisionMesh. It can be used to fire actions when
player enters an area for example.
@param {*} name
@param {*} options
Adds a collider to the level scene . It will fire the options . onCollide callback when the collider intersects options . collisionMesh . It can be used to fire actions when player enters an area for example . @param { * } name @param { * } options
addCollider(name, options) {
        let collider = BABYLON.MeshBuilder.CreateBox(name, {
            width: options.width || 1, 
            height: options.height || 1, 
            depth: options.depth || 1
        }, this.scene);
        BABYLON.Tags.AddTagsTo(collider, 'collider boxCollider');
        collider.position.x = options.positionX || 0;
        collider.position.y = options.positionY || 0;
        collider.position.z = options.positionZ || 0;
        collider.isVisible = (options.visible) ? options.visible : false;
        if(collider.isVisible) {
            let colliderMaterial = new BABYLON.StandardMaterial(name + 'Material');
            colliderMaterial.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0);
            colliderMaterial.alpha = 0.5;
            collider.material = colliderMaterial;
        }
        options.timeToDispose = (options.timeToDispose) ? options.timeToDispose : 0;
        collider.actionManager = new BABYLON.ActionManager(this.scene);
        collider.actionManager.registerAction(
            new BABYLON.ExecuteCodeAction(
                {
                    trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger,
                    parameter: options.collisionMesh
                },
                () => { 
                    if(options.onCollide) {
                        options.onCollide();
                    }
                    if(options.disposeAfterCollision) {
                        setTimeout(() => {
                            collider.dispose();
                        }, options.timeToDispose);
                    }
                }
            )
        );
        return collider;
    }
addCollider ( name , options ) { let collider = BABYLON . MeshBuilder . CreateBox ( name , { width : options . width || 1 , height : options . height || 1 , depth : options . depth || 1 } , this . scene ) ; BABYLON . Tags . AddTagsTo ( collider , 'collider boxCollider' ) ; collider . position . x = options . positionX || 0 ; collider . position . y = options . positionY || 0 ; collider . position . z = options . positionZ || 0 ; collider . isVisible = ( options . visible ) ? options . visible : false ; if ( collider . isVisible ) { let colliderMaterial = new BABYLON . StandardMaterial ( name + 'Material' ) ; colliderMaterial . diffuseColor = new BABYLON . Color3 ( 0.5 , 0.5 , 0 ) ; colliderMaterial . alpha = 0.5 ; collider . material = colliderMaterial ; } options . timeToDispose = ( options . timeToDispose ) ? options . timeToDispose : 0 ; collider . actionManager = new BABYLON . ActionManager ( this . scene ) ; collider . actionManager . registerAction ( new BABYLON . ExecuteCodeAction ( { trigger : BABYLON . ActionManager . OnIntersectionEnterTrigger , parameter : options . collisionMesh } , ( ) => { if ( options . onCollide ) { options . onCollide ( ) ; } if ( options . disposeAfterCollision ) { setTimeout ( ( ) => { collider . dispose ( ) ; } , options . timeToDispose ) ; } } ) ) ; return collider ; }
Adds a collider to the level scene.
Adds a collider to the level scene .
// Add a tag to identify the object as collider and to simplify group operations (like dispose) // Runs onCollide callback if exists // If true, will dispose the collider after timeToDispose