Hiding/showing physics objects from collision

Hi all,

I’ve had a look round the forums/google etc but can’t seem to find an answer to this issue. I’ll try and simplify.

I have a black box and white box in the center of the screen which are placed on top of each other. At the start of the game the black box is hidden under the white box (isVisible = false). I then have black circles fall down from and collide with the white box. The issue is that they are also colliding (creating collision events) with the black box even though it is hidden. The point is that if the user holds a button the white box will be hidden and the black box will appear in its place. They are both static physics objects, is this the problem? Essentially when the boxes are hidden they should not be able to interact with anything (although they still should be able to move (the black box xy position is tied to the white box xy position which is controlled with a virtual joystick)).

As they are going to swap around a lot during the course of the game I though it best not to keep adding and removing them? I dont think mask bits is an option either as when the boxes are visible they will need to interact with the circles. Do i need to add/remove physics bodies all the time (although I dont think you can do this in collision events)?

If the two boxes are exactly the same size, shape and in the same position, they will most likely cause collision events with anything that hits them. If objects which need to hit them should only hit the visible one, you could simply try removing the collision listener from the invisible object and adding it to the visible object, when the player touch/releases the screen.

My preferred option, however, is not to have two boxes in exactly the same location. Have one display group with two rectangle objects (one white, one black) and switch them on/off with the user’s touch. That way, you know which box the dropping circles should collide with and can handle the collision events (which will only occur once per circle collision, of course) properly.

Hi Horacebury, thanks for the reply, it’s very helpful.

In terms of your second comment can I just ask what you mean when you say “switching them on/off”? Using what method?

Visible/Invisible. Ie: just pretend to have two boxes but actually have one and change it’s colour.

If the two boxes are exactly the same size, shape and in the same position, they will most likely cause collision events with anything that hits them. If objects which need to hit them should only hit the visible one, you could simply try removing the collision listener from the invisible object and adding it to the visible object, when the player touch/releases the screen.

My preferred option, however, is not to have two boxes in exactly the same location. Have one display group with two rectangle objects (one white, one black) and switch them on/off with the user’s touch. That way, you know which box the dropping circles should collide with and can handle the collision events (which will only occur once per circle collision, of course) properly.

Hi Horacebury, thanks for the reply, it’s very helpful.

In terms of your second comment can I just ask what you mean when you say “switching them on/off”? Using what method?

Visible/Invisible. Ie: just pretend to have two boxes but actually have one and change it’s colour.