How would you do player-to-player collision with box2d?

I’m trying to find a good way to achieve “correct” behavior when two players collide in a multiplayer game. My current situation is that when they collide, they apply forces to each other and starts spinning and changing direction.

I want them to be unable to walk through each other, but not to cause any force/impulse being applied. One idea I have is to add collision filtering for the players, and handle it manually.

You can add a collision listener, then when you detect that the two players collide, set their linear velocities to (0,0)

something like this, maybe?

private void createCollisionListener() { 
ContactListener cl = new ContactListener() { 

           @Override 
           public void beginContact(Contact contact) { 
               Fixture fixtureA = contact.getFixtureA(); 
               Fixture fixtureB = contact.getFixtureB(); 
                
               /* pseudocode start 
               
               (1) check whether fixtureA belongs to a player and fixtureB belongs to a player
               
               (2) if fixtureA and fixtureB are both players do:
               
               		(a) fixtureA.getBody().setLinearVelocity(0.0f,0.0f);
               		(b) fixtureB.getBody().setLinearVelocity(0.0f,0.0f);
                              
               pseudocode end */
           } 

           @Override 
           public void endContact(Contact contact) { 
               Fixture fixtureA = contact.getFixtureA(); 
               Fixture fixtureB = contact.getFixtureB();
               
               // TODO

           } 

           @Override 
           public void preSolve(Contact contact, Manifold oldManifold) { 
           } 

           @Override 
           public void postSolve(Contact contact, ContactImpulse impulse) { 
           } 

       }; 
        
	world.setContactListener(cl); 
   } 

Cool. I’ll try it out when I get some spare time.

A problem with this (naive?) strategy is that when two players collide… they are stuck :slight_smile:

I suppose that a quick and dirty fix would be to manually separate them just enough to be able to carry on. Any other suggestions?

Don’t know exactly what behaviour you want. Setting the fixture restitution to zero prevents from bouncing back at collisions.

I want to achieve something similar to this game http://www.java-gaming.org/topics/iconified/26049/view.html :point:

Players can block the way for others, but not being pushed.

Yes, what’s wrong with setting restitution to 0?

You can push your opponent?

So basically, when players collide, you want it such that each player treats the player he’s colliding into like a wall?

What happens if you set the linear velocity to (0,0) only if the linear velocity is not (0,0)? This way, once they collide and their linear velocities are already at (0,0) — you can move them again.

I think that’s a better way of handling it than separating them just enough so that they could move again.

for example:

if(fixtureA and fixtureB are both players){
   if(fixtureA.getBody().getLinearVelocity != (0,0)){
      fixtureA.getBody().setLinearVelocity(0.0f,0.0f);
   }

   if(fixtureB.getBody().getLinearVelocity != (0,0)){
      fixtureB.getBody().setLinearVelocity(0.0f,0.0f);
   }
}

Maybe you could try it out.

If there are no equal but opposite forces, yes.
Otherwise, set up contact listeners/filters and clear steering forces from both players. Setting linear velocity is not recommended and not necessary, afaik.
Box2d example

I tried, but it seems like this too causes the two bodies to become stuck. I guess that when velocity is set to zero they stay in a colliding state. ?

That’s exactly what I want! :slight_smile: