CategoryFilter on Sensor not working

Posts that don't fit into other categories.
assofohdz
Posts: 25
Joined: Mon Apr 17, 2017 10:46 am

CategoryFilter on Sensor not working

Postby assofohdz » Fri Jun 16, 2017 2:40 pm

Hi

I have a flag (a sensor) for king of the hill style games, which detects the collision intended correctly. However, it also detects collisions with other categories - which I find odd because I thought I had the CategoryFilters worked out. Here's my ship (which it should detect):

Code: Select all

Filter filter = CollisionFilters.FILTER_CATEGORY_DYNAMIC_PLAYERS;
        BodyFixture fixture = new BodyFixture(new Circle(PhysicsConstants.SHIPSIZERADIUS));
        fixture.setFilter(filter);

Here is my bombs (which it should not detect):

Code: Select all

Filter filter = CollisionFilters.FILTER_CATEGORY_DYNAMIC_PROJECTILES;
        BodyFixture fixture = new BodyFixture(new Circle(PhysicsConstants.BOMBSIZERADIUS));
        fixture.setFilter(filter);


Here is the flag:

Code: Select all

Filter filter = CollisionFilters.FILTER_CATEGORY_SENSOR_FLAGS;
        BodyFixture fixture = new BodyFixture(new Circle(PhysicsConstants.FLAGSIZERADIUS));
        fixture.setFilter(filter);
        fixture.setSensor(true);


And the CategoryFilters:

Code: Select all

    /**
     * Static bodies collides with all bodies except other static bodies.
     */
    private static final long COLLISION_CATEGORY_STATIC_BODIES = 1;
    private static final long COLLISION_CATEGORY_STATIC_GRAVITY = 2;
    private static final long COLLISION_CATEGORY_DYNAMICS_PLAYERS = 4;
    private static final long COLLISION_CATEGORY_DYNAMICS_PROJECTILES = 8;
    private static final long COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS = 16;
    private static final long COLLISION_CATEGORY_SENSOR_FLAGS = 32;
    private static final long COLLISION_CATEGORY_ALL = Long.MAX_VALUE;

    /**
     * Static bodies collides with all dynamics
     */
    public static final CategoryFilter FILTER_CATEGORY_STATIC_BODIES = new CategoryFilter(COLLISION_CATEGORY_STATIC_BODIES,
            COLLISION_CATEGORY_DYNAMICS_PLAYERS | COLLISION_CATEGORY_DYNAMICS_PROJECTILES | COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS);

    /**
     * Gravities collides with all dynamics
     */
    public static final CategoryFilter FILTER_CATEGORY_STATIC_GRAVITY = new CategoryFilter(COLLISION_CATEGORY_STATIC_GRAVITY,
            COLLISION_CATEGORY_DYNAMICS_PLAYERS | COLLISION_CATEGORY_DYNAMICS_PROJECTILES | COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS);

    /**
     * Dynamic players collide with everything except other players
     */
    public static final CategoryFilter FILTER_CATEGORY_DYNAMIC_PLAYERS = new CategoryFilter(COLLISION_CATEGORY_DYNAMICS_PLAYERS,
            COLLISION_CATEGORY_STATIC_GRAVITY | COLLISION_CATEGORY_DYNAMICS_PROJECTILES | COLLISION_CATEGORY_STATIC_BODIES | COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS);

    /**
     * Dynamic projectiles collide with everything except other projectiles
     */
    public static final CategoryFilter FILTER_CATEGORY_DYNAMIC_PROJECTILES = new CategoryFilter(COLLISION_CATEGORY_DYNAMICS_PROJECTILES,
            COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS | COLLISION_CATEGORY_DYNAMICS_PLAYERS | COLLISION_CATEGORY_STATIC_GRAVITY | COLLISION_CATEGORY_STATIC_BODIES);

    /**
     * Dynamic map objects collide with everything
     */
    public static final CategoryFilter FILTER_CATEGORY_DYNAMIC_MAPOBJECTS = new CategoryFilter(COLLISION_CATEGORY_DYNAMICS_MAPOBJECTS,
            COLLISION_CATEGORY_ALL);

    /**
     * Flags collide only with players
     */
    public static final CategoryFilter FILTER_CATEGORY_SENSOR_FLAGS = new CategoryFilter(COLLISION_CATEGORY_SENSOR_FLAGS,
            COLLISION_CATEGORY_DYNAMICS_PLAYERS);


Why is it still detecting collisions between bombs and the flag?

This is my detection code:

Code: Select all

//Collision detected by the broadphase
    @Override
    public boolean collision(org.dyn4j.dynamics.Body body1, BodyFixture fixture1, org.dyn4j.dynamics.Body body2, BodyFixture fixture2) {
        //Default, keep processing this event

        return fixture1.getFilter().isAllowed(fixture2.getFilter()) && fixture2.getFilter().isAllowed(fixture1.getFilter());
    }

    //Collision detected by narrowphase
    @Override
    public boolean collision(org.dyn4j.dynamics.Body body1, BodyFixture fixture1, org.dyn4j.dynamics.Body body2, BodyFixture fixture2, Penetration penetration) {
        return fixture1.getFilter().isAllowed(fixture2.getFilter()) && fixture2.getFilter().isAllowed(fixture1.getFilter());
    }

    //Contact manifold created by the manifold solver
    @Override
    public boolean collision(org.dyn4j.dynamics.Body body1, BodyFixture fixture1, org.dyn4j.dynamics.Body body2, BodyFixture fixture2, Manifold manifold) {

        EntityId one = (EntityId) body1.getUserData();
        EntityId two = (EntityId) body2.getUserData();

        if (gravityState.isWormholeFixture(fixture1) || gravityState.isWormholeFixture(fixture2)) {
            gravityState.collide(body1, fixture1, body2, fixture2, manifold, time.getTpf());
            return false;
        }

        if (flagState.isFlag(one) || flagState.isFlag(two)) {
            flagState.collide(body1, fixture1, body2, fixture2, manifold, time.getTpf());
            return false;
        }

        return true; //Default, keep processing this event
    }

    //Contact constraint created
    @Override
    public boolean collision(ContactConstraint contactConstraint) {
        return true; //Default, keep processing this event
    }


The issue is that I dont want to call flagState.collide with other couple than flags and ships. if I do - I get runtime errors.

assofohdz
Posts: 25
Joined: Mon Apr 17, 2017 10:46 am

Re: CategoryFilter on Sensor not working

Postby assofohdz » Fri Jun 16, 2017 2:46 pm

Ah - by debugging I can see that the filters are DEFAULT_FILTER on both fixtures - which is really odd.

assofohdz
Posts: 25
Joined: Mon Apr 17, 2017 10:46 am

Re: CategoryFilter on Sensor not working

Postby assofohdz » Fri Jun 16, 2017 2:56 pm

Ah - when I created the body I used addFixture(fixture.getShape()) instead of just addFixture(fixture)


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests