How to check overlap or collision with world bounds (arcade physics)

Hello there.
I am trying to check collision (or overlap) between group (enemies) and world bounds (Physics: arcade).
But i can’t find how to do this.
Overlap between objects works Ok.
I tried to add different params to self.physics.add.overlap, but it never call callback =(

function create() {
    self = this;
    self.physics.world.bounds.setTo(0, 0, config.width, config.height);
    self.physics.world.setBoundsCollision();
    cursors = self.input.keyboard.createCursorKeys();


    createPlayer();
    createEnemies();
}


function createEnemies() {
    enemies = self.physics.add.group({
        key: 'enemy',
        repeat: 5,
        setXY: { x: 12, y: 0, stepX: 120 }
    });

    enemies.children.iterate(function (child) {
        child.setCollideWorldBounds(true);
    });

    self.physics.add.overlap(player, enemies, (player, enemy) => {
        enemy.disableBody(true, true);
        player.disableBody(true, true);
    }, null, this);
}

Thank you

1 Like

Hi,
Try with:

child.body.setCollideWorldBounds(true);

instead of

child.setCollideWorldBounds(true);

Hi,
i tried this, but unfortunately it is not working.
Maybe, the second argument is wrong. But i tried different options there.

 enemies.children.iterate(function (child) {
    child.body.setCollideWorldBounds(true);
});


self.physics.add.overlap(enemies, self.physics.world.bounds.bottom, () => {
    console.log('Overlap');
}, null, this);

overlap() and collider() etc. don’t deal with the world bounds at all.

You can use the worldbounds event for collisions at the boundary, but not for overlaps.

If you just want to check if a body is in bounds, you can do something like

if (Phaser.Geom.Intersects.RectangleToRectangle(
  enemy.body,
  this.physics.world.bounds
) {
  /* … */
}
2 Likes

Thank you! That link helped a lot!

function addWorldInteractions() {
    self.physics.world.setBoundsCollision(true, true, true, true);

    self.physics.world.on("worldbounds", function (body) {
        if (body) {
            if (body.gameObject.texture.key === 'enemy') {
                if ((config.height - body.position.y) <= body.height) {
                    body.gameObject.disableBody(true, true);
                    self.physics.pause();
                    console.log('Game Over');
                }
            } 

            if (body.gameObject.texture.key === 'bullet') {
                body.gameObject.disableBody(true, true);
                console.log('Bullet')
            }
        }
    });
}
1 Like

This works for new enemies or bullets that are spawned.
Put this in the spawn function

const currentEnemy = this.enemy
this.events.on('update', () => {
    if(!this.physics.world.bounds.contains(currentEnemy.x, currentEnemy.y)) {
        currentEnemy.destroy()
    }
})

Don’t use setInterval() like that, just put it in update().

1 Like

to an existing group

enemies.children.iterate( (child) => {
    this.events.on('update', () => {
        if(!this.physics.world.bounds.contains(child.x, child.y)) {
           child.destroy()
        }
    })
});