Cannot read property 'collide' of undefined

Im pretty sure this is a scope related problem that I dont understand.
In my update loop I have:

this.otherPlayers.getChildren().forEach(function(otherPlayer) {
    this.physics.collide(this.player, otherPlayer);
});

I tried doing:

this.physics.collide(this.player, otherPlayers);

which didn’t give me any errors but the collision was rarely happening. They were just passing through each other.

Hi,
In the create function, not in update, try:

this.physics.add.collider(this.player, this.otherPlayers, (player, otherPlayer) => {
    console.log(player, otherPlayer)
}, null, this);
1 Like

Like BlunT said you should create colliders in the create function.

Even when I try your method the players stop for half a second and then pass through each other. (Same as earlier)
I’m currently not using an authoritative server. I tried using one with phaser running in headless mode but that was a bit too complicated. Although the collision was working on that as collision was managed on the server side.
I pretty much followed this tutorial for multiplayer with a few changes here and there:

I looked the tutorial, they update the position of each player with coords x y, and physics collisions don’t work when you change positions, you need to apply velocity for collisions working correctly.
So instead of sending positions to the server, send the actual velocity x y of each player, and apply it for each player once receveid.

1 Like

I must have been doing something wrong because i tried again and your previous answer works perfectly now.

Yes but the collision was being checked locally and then the velocity was also applied locally and then the new position was being sent to the server, so the collision should have worked…

I even have another slightly unrelated problem. When I follow the authoritative multiplayer tutorial here: https://phasertutorials.com/creating-a-simple-multiplayer-game-in-phaser-3-with-an-authoritative-server-part-2/

Everything works fine except the setDrag which has no effect whatsoever, and even the collideWorldBounds doesn’t work which I added by myself.

function addPlayer(self, playerInfo) {
    const player = self.physics.add.image(playerInfo.x, playerInfo.y, 'ship').setOrigin(0.5, 0.5).setDisplaySize(53, 40);
    player.setDrag(100);
    player.body.collideWorldBounds(true);
    player.setAngularDrag(100);
    player.setMaxVelocity(200);
    player.playerId = playerInfo.playerId;
    self.players.add(player);
}

Nice if it works, i need to give a try to a multiplayer game :slightly_smiling_face: