phaserjs / phaser-ce

Phaser CE is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
http://phaser.io
MIT License
1.35k stars 491 forks source link

P2 bodies do not collide with world unless updateCollisionMask is called #257

Open javidcf opened 7 years ago

javidcf commented 7 years ago

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

samme commented 7 years ago

Is this the same as #207 ?

javidcf commented 7 years ago

@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:

jbpuryear commented 7 years ago

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.

samme commented 7 years ago

@jbpuryear would you like to updates the docs?

samme commented 7 years ago

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);