# Switching from Arcade to P2 physics in Phaser

I just started using phaser for testing purposes. I've followed the tutorial from the phaser website ("making-your-first-phaser-game") everything is clear, so far no problems.

But since I'm making a pool game I need rotation on collision so I've switched to P2 for physics. Suddenly everything moves and collides!

Here is the live code: http://199.26.84.223/

I have 2 groups:

1. sides (sides of table)
2. balls

Point 1: I want sides to be immovable so I've added:

side.body.immovable = true;


to each object just as with Arcade physics, but it doesn't work. How can I make objects immovable in P2?

game.physics.startSystem(Phaser.Physics.P2JS);

side = sides.create(0,0 , 'sideA');
side.body.immovable = true;


Point 2: I don't want my player to collide with red balls only with the white ball.

So in arcade we had to define each collision otherwise they wouldn't collide:

game.physics.arcade.collide(player , wball);


But here there is no defining everything collides with everything.

How can I make exception for collisions, so my player wouldn't collide with anything other then the white ball (Wball)?

Point 3: What happend to overlap? Or collide callback functions? I need to add some holes to my table and make the balls disapearr on collide or overlap. In arcade I had it like this:

game.physics.arcade.overlap( balls , holes ,  killBall  , null, this);
function killBall  ( b , h  ){ b.kill() ; }


Arcade physics, unlike P2 and most other physics engines, is a very simple and simplistic system. The former will use a lot of lay terms whereas P2 will use terms plucked straight from a physics text book. Switching between the two is not trivial; you'll need to relearn a lot of stuff. I highly recommend the P2 examples page to get a grasp of what P2's features are and how to use them.

1. To make bodies immovable

sprite.body.immovable is an arcade physics thing. For P2 it's sprite.body.kinematic. "Kinematic" in P2 means, generate collision events, but don't be affected by physics itself like gravity and collisions.

2. To selectively collide

P2 handles this problem using collision groups. Basically if you want one group to collide with another, but none of the bodies in the same group to collide with each other, you put them in separate groups and specify that the two groups collide with each other. Pay close attention to the example and documentation because it's not trivial to set up.

3. To handle collision events

Collision event callbacks are still there but they are set up differently; see the example given in the last part. Instead of explicitly requesting a collide/overlap, P2 will be doing all its stuff all the time, and if you gave it a callback during setup it will call it as collisions occur.

As for overlap, P2 doesn't support it directly, but there is an equivalent solution known as the "Postbroadphase Callback". Basically this is a callback where P2 collects all potential collisions and then asks you - via the callback - whether to allow those collisions to resolve or be ignored. You return false in the callback to tell P2 not to perform collision resolution, but the fact that the callback was called in the first place means that those two bodies have overlapped, so this is where you put your overlap handling code.

Good luck with P2, take it a step at a time because it (like most physics engines) have a bit of a learning curve.

• tanx for a great answer , i've actually look at the example page of p2 for immovable problem but i gave up since everything was moving around in all examples , i guess i've missed that one ...i'll pay more attention this time thanks again
– max
Oct 9, 2015 at 18:23