CategoryFilters

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

CategoryFilters

Postby assofohdz » Wed Jun 07, 2017 9:01 am

Q1: I'm trying to understand the usage of category filters. I understand the binary part, but not the category part. Is this correctly set up:

Code: Select all

public class CollisionFilters {

    public static final long COLLISION_CATEGORY_SHIPS = 1;
    public static final long COLLISION_CATEGORY_ATTACKS = 2;
    public static final long COLLISION_CATEGORY_MAPTILES = 4;
    public static final long COLLISION_CATEGORY_MAPMOVABLES = 8;
    final long MASK_ALL = Long.MAX_VALUE;

    // Ships can collide with attacks, movables and map tiles
    CategoryFilter shipsCategoryFilter = new CategoryFilter(COLLISION_CATEGORY_SHIPS, COLLISION_CATEGORY_ATTACKS | COLLISION_CATEGORY_MAPTILES | COLLISION_CATEGORY_MAPMOVABLES);
   
    // Map tiles can collide with ships, movables and attacks
    CategoryFilter mapTilesCategoryFilter = new CategoryFilter(COLLISION_CATEGORY_MAPTILES, COLLISION_CATEGORY_ATTACKS | COLLISION_CATEGORY_MAPMOVABLES | COLLISION_CATEGORY_SHIPS);
   
    // Attacks can collide with ships, movables and map tiles
    CategoryFilter attacksFilter = new CategoryFilter(COLLISION_CATEGORY_ATTACKS, COLLISION_CATEGORY_MAPMOVABLES | COLLISION_CATEGORY_MAPTILES | COLLISION_CATEGORY_SHIPS);       
}


My idea was then to apply the SHIP-filter to all ships I create, the ATTACKS-filter to all attacks I create etc.

Q2: Is there any decent way to make a specific body not react to specific other bodies? (like if I attack, I dont want to get collision results from my own attack on my own body)

Kind regards,

zoom
Posts: 143
Joined: Sun Mar 17, 2013 3:57 pm
Location: Stockholm, Sweden
Contact:

Re: CategoryFilters

Postby zoom » Wed Jun 07, 2017 11:04 am

I did something similar and it worked fine for me (although I never finished that game).

Code: Select all

public class CollisionGroups {

   /**
    * Static bodies collides with all bodies except other static bodies.
    */
   public static final CategoryFilter STATIC_BODIES = new CategoryFilter(1, 14);
   /**
    * Dynamic bodies collide with all bodies.
    */
   public static final CategoryFilter DYNAMIC_BODIES = new CategoryFilter(2, 15);
   public static final CategoryFilter DYNAMIC_BODIES_ONLY = new CategoryFilter(2, 2);
   /**
    * Player bullet bodies collides with all bodies except player bodies.
    */
   public static final CategoryFilter PLAYER_BULLET_BODIES = new CategoryFilter(4, 7);
   /**
    * Player bodies collide with all bodies except player bullets.
    */
   public static final CategoryFilter PLAYER_BODIES = new CategoryFilter(8, 11);
}


For you other question I recall William has previously suggested to implement your own filters instad of CategoryFilters, then you can filter the collisions on whatever way makes sense for your game.

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

Re: CategoryFilters

Postby assofohdz » Wed Jun 07, 2017 1:30 pm

The masks you've picked, did you get those by checking out how the numbers are represented binary?

Edit: I guess they are the same as mine in a way, combined by the OR operator.

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

Re: CategoryFilters

Postby assofohdz » Thu Jun 08, 2017 2:28 am

Thanks. It's actually a very nice system for filtering. I guess what confused me was that you could interlink the categories by choosing eg. 1, 2 and 3 instead of 1, 2 and 4. But I guess that has its uses if you want hierarchical filters.

zoom
Posts: 143
Joined: Sun Mar 17, 2013 3:57 pm
Location: Stockholm, Sweden
Contact:

Re: CategoryFilters

Postby zoom » Thu Jun 08, 2017 5:10 am

assofohdz wrote:The masks you've picked, did you get those by checking out how the numbers are represented binary?

Edit: I guess they are the same as mine in a way, combined by the OR operator.


Yes, they should be similar, it's a bit mask.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests