Search code examples
androidbox2dandenginegame-physics2d-games

Updating maskbit of a physics body in runtime not working ANDENGINE


I am trying to develop a game like run! black and white in andEngine. the platform mask bits change on runtime if you look at the game.

to achieve this effect, I am trying to change each platforms mask bit on runtime , something like this.

Filter filter1 = new Filter();
                    filter1.maskBits = CATEGORYBIT_NOT_COLLIDE; 
                    single_divider.physics_body.getFixtureList().get(0).setFilterData(filter1);

but platforms mask bit does not change on runtime. I dont know what seems to be the problem. I think the filter data of physics body needs to reset before setting new filter data. But I dont know how.

If anyone can guide me to right direction.


Solution

  • If you want to temporarily turn off colliding the better idea is to use setSensor() method

        single_divider.physics_body.getFixtureList().get(0).setSensor(true);
    

    The sensor is kind of fixtures that detects collision so you can still handle them in the collision listener but the bodies won't collide. Read more here

    Of course you can also call

        single_divider.physics_body.getFixtureList().get(0).setSensor(false);
    

    to turn off being sensor (so again turn on colliding)