Jump to content

P2 collision groups - have to set 'collides' twice?


boolean
 Share

Recommended Posts

Hi there

 

I'm learning up on how P2 collision groups work and I have a question.

 

It seems that if I have two collision groups, one for the player and one for platforms, to get the two to interact I have to write:

this.ground.body.collides(playerCollisionGroup);this.player.body.collides(groundCollisionGroup);

If I just use one of those statements the collisions don't work, I have to use both. This seems a little strange - if ground is set to collide with player, why do I also have to set the player to collide with ground? It seems a little redundant. Is there a reason it has to be bound twice?

 

Rich is a heck of a lot smarter than me so I assume I'm just missing a very good reason for this :)

 

Cheers!

Link to comment
Share on other sites

Sprite.body.collides([FirstCollisionGroup, SecondCollisionGroup, ThirdCollisionGroup]);

Use an array with alll the collision groups for each sprite and also be sure that you set each sprite a Collision Group :

Sprite.body.setCollisionGroup(FirstCollisionGroup);
Link to comment
Share on other sites

@icp:  this doesn't exactly answer the question..   i think boolean already figured out how to make them collide but is wondering why i have to set bodyA to collide with bodyB  and vice versa..   if A collides with B the logical outcome would be that B also collides with A - so why the redundance..

 

IMHO the best approach would be to set it independently from the bodies like.

 

phaser.pyhysics.p2.setCollisions(ArrayA, ArrayB);  //pseudocode !! all collisiongroups in arrayA should collide with all collisiongroups in arrayB)

Link to comment
Share on other sites

@valueerror: Thanks, yep that's correct - I was more curious about the 'why' rather than the 'how'. If there is a reason that you need to double-bind collisions, it would be good to know what case that solves. 

 

I like your pseudo code and it works a lot closer to how the collisions are setup in Arcade physics. I wonder what the reason for that not already existing is. Curious! :D

Link to comment
Share on other sites

I can't speak for Rich, but I can make up an example. Maybe enemies affect the player when colliding (e.g. hot death) while the player colliding with enemies should do nothing. The asymmetry is mirrored in the collision call.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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