so i just started using phaser for test

i've followed the tutorial in the phaser website (making-your-first-phaser-game) everything is clear no problem .

but since i've making a pool game i need rotation on collision so i've switched to P2 ... suddenly everything moves and collides!

here is the live code

i have 2 groups

sides ( sides of table ) and balls

1 - i want sides to be immovable so i 've added

side.body.immovable = true;

to each object like in arcade ... it doesn't work ... how can i make objects immovable in p2 ?


    sides = game.add.physicsGroup(Phaser.Physics.P2JS);
    side = sides.create(0,0 , 'sideA');
    side.body.immovable = true;

2 - i dont want my player to collide with red balls only with 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 in ocllision so my player wouldn't collides with anything other then white ball (Wball) ?

3 - what happend to overlap ? or collide call back functions ? i need to add some holes to my table and make the balls dissapir on collide or overlap ... in the 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.

To answer your questions:

  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.

  • \$\begingroup\$ 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 \$\endgroup\$
    – max
    Oct 9 '15 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.