Jump to content

Help with online multiplayer collisions


Recommended Posts

Hey everyone! 


I've build a little game with phaser a while back and i'm currently adding a play online feature.


My main problem at the moment is how to handle two players colliding. The players are supposed to bounce off of each other. In the non-mulitplayer game, this is how I handle the collision.

this.game.physics.arcade.collide(this.player, this.player2, this.playerCollide, this.beforePlayerCollide, this);

with the collide functions as follows:

beforePlayerCollide:function(player1,player2){      player1.body.bounce.setTo(1,1);      player2.body.bounce.setTo(1,1);      return true;    },
playerCollide:function(player1,player2){   if(!this.hitSound.isPlaying){      this.hitSound.play();   }   player1.body.bounce.setTo(0,0);   player2.body.bounce.setTo(0,0);}

For multiplayer, i'm currently just sending the other user's position and velocity on every movement through socket io and updating his position and velocity on the fly. I will deal with optimizing this method and accounting for lag later, but at the moment I just need to get the basic game to work. The problem here is that 

due to constant update of the player positions, the playerCollide function sometimes doesn't trigger after the beforePlayerCollide, causing the players to just have their bounce setting turned on. 


Has anyone faced this problem before? Should I change the way i'm making the players bounce off each other on collision? Or is there no way that this will work with constantly updating the opponents position? 


Any help would be appreciated, thank you :)

Link to comment
Share on other sites

I think the way you create the bounce is not that great because if a frame is somehow skipped your game might break. Why don't you create a material for the player and always keep the bounce set to 1 between the players but to 0 with everything else?


If you also have to play a sound for bounce you have two options:

-> Send the bounce event from the server (you need to have an authoritative client or server)

-> Check somehow if a bounce happened using the players' velocities.


If you are not going to have an authoritative server the bounce effect will be really hard to be accurately done by sending only position/velocity.

Link to comment
Share on other sites

Yes, I'm only updating the position of opponent, not the local player, so there aren't any overwrites. 


I'll look into moving the bounce sound effect to the server.


The materials suggestion is what I was looking for. However, I am currently using arcade physics and from a quick documentation search I noticed that material collisions are part of P2 physics. Is there a similar method I can use with arcade physics? 


Thanks for the suggestions, my current collision method definitely doesn't work when it comes to multiplayer. 

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...