phaser-ce icon indicating copy to clipboard operation
phaser-ce copied to clipboard

P2 bodies do not collide with world unless updateCollisionMask is called

Open javidcf opened this issue 8 years ago • 5 comments

I was having trouble getting a body to collide with the world, and I finally realized that the collision only worked if I called updateCollisionMask() on the body. Right now this is only called by Phaser when you toggle the value of collideWorldBounds, so maybe the fix is just calling it at the end of the constructor of the body? I'm not too sure...

There is a demo at https://codepen.io/javidcf/pen/RgajRb. World bounds collision only works when line 30 (containing the call to updateCollisionMask) is uncommented.

I am using Phaser-CE v2.8.0

javidcf avatar Jun 11 '17 17:06 javidcf

Is this the same as #207 ?

samme avatar Jun 14 '17 02:06 samme

@samme I'm not sure... I had seen #207, but this is not really that a sprite stops colliding with objects other than the world bounds when collideWorldBounds is set to false, but instead it is not colliding with the bounds themselves. In that issue it is stated that it is a expected behavior and is just a documentation problem, I'm not sure if that is the case here too.

I have realized that the issue has to do with me calling updateBoundsCollisionGroup(true). If I understood this correctly, this sets up a collision group for the bounds... However, I would expect that new collision group is taken into account when creating a new sprite with collideWorldBounds set to true after that (without needing to explicitly call updateCollisionMask). In summary, what I see is:

  • p2.updateBoundsCollisionGroup(true) + sprite.body.updateCollisionMask(): collision works.
  • p2.updateBoundsCollisionGroup(true) + no sprite.body.updateCollisionMask(): collision does not work.
  • no p2.updateBoundsCollisionGroup(true) + sprite.body.updateCollisionMask(): collision does not work.
  • no p2.updateBoundsCollisionGroup(true) + no sprite.body.updateCollisionMask(): collision works.

javidcf avatar Jun 14 '17 08:06 javidcf

I think this and #207 are sort of the same thing. It's kind of like a p2 has two different modes it can work in. The default is to collide everything with everything else, and the other is to have you explicitly list which things collide with which other things.

If you change a body's collision group or mask it switches to the second mode and won't collide with anything you haven't told it to. Without knowing this it can seem like p2 is doing all sorts of weird things. For example setting a body's collideWorldBounds property to false also causes it to stop colliding with anything else.

And also why, in @javidcf's example, calling updateCollisionMask gets the body to collide with the world bounds again, but also causes the body to stop colliding with everything else. Demo

Pretty much every function that touches the collision info (I think all of them, but I didn't check) changes the collision mask from the default of 1 to the the value returned by getCollisionMask. It's not very intuitive, and just from the name it doesn't seem like collideWorldBounds should have this effect, but it sort of makes sense once you know what's going on.

I think the fix I suggested for #207 would resolve this too, and there's probably an even better solution, but again I'm unsure if it's better to just leave it.

jbpuryear avatar Jun 19 '17 01:06 jbpuryear

@jbpuryear would you like to updates the docs?

samme avatar Jul 02 '17 18:07 samme

There is a demo at https://codepen.io/javidcf/pen/RgajRb. World bounds collision only works when line 30 (containing the call to updateCollisionMask) is uncommented.

Also fixed by removing

p2.updateBoundsCollisionGroup(true);

samme avatar Jul 10 '17 18:07 samme