CategoryFilters and Custom filter combined?

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

CategoryFilters and Custom filter combined?

Postby assofohdz » Mon Jun 12, 2017 3:57 am

Is there a way to combine both CategoryFilters and custom filters? I have CategoryFilters working. But I also want to omit collisions between bodies that are on the same team. This means that I will have to dynamically update the collision filter for a given body - depending on which team this body is on.

Is there any way to do this ?

William
Site Admin
Posts: 345
Joined: Sat Feb 06, 2010 10:23 pm

Re: CategoryFilters and Custom filter combined?

Postby William » Tue Jun 13, 2017 6:44 pm

Can you not remove the bit that represents the team that the body is on? For example:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.java)
  1. long TEAM_1 = 1;
  2. long TEAM_2 = 2;
  3. long OTHERS = 4;
  4.  
  5. // Team 1 can collide with Team 2 and others
  6. CategoryFilter team1Filter = new CategoryFilter(TEAM_1, TEAM_2 | OTHERS);
  7. // Team 2 can collide with Team 1 and others
  8. CategoryFilter team2Filter = new CategoryFilter(TEAM_2, TEAM_1 | OTHERS);
  9. // Others can collide with Team 1, Team 2, and others
  10. CategoryFilter otherFilter = new CategoryFilter(OTHERS, TEAM_1 | TEAM_2 | OTHERS);
  11.  
  12. System.out.println(team1Filter.isAllowed(team2Filter)); // true
  13. System.out.println(team2Filter.isAllowed(team1Filter)); // true
  14. System.out.println(team1Filter.isAllowed(otherFilter)); // true
  15. System.out.println(team2Filter.isAllowed(otherFilter)); // true
  16. System.out.println(otherFilter.isAllowed(team1Filter)); // true
  17. System.out.println(otherFilter.isAllowed(team2Filter)); // true
  18. System.out.println(otherFilter.isAllowed(otherFilter)); // true
  19. System.out.println(team1Filter.isAllowed(team1Filter)); // false
  20. System.out.println(team2Filter.isAllowed(team2Filter)); // false


Another option is to create your own filter class and wrap the CategoryFilter (or you could just extend it directly). Something like:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.html)
  1. public class MyFilter implements Filter {
  2.     private int team;
  3.     private CategoryFilter baseFilter;
  4.    
  5.     public MyFilter(int team) {
  6.         this.team = team;
  7.     }
  8.    
  9.     @Override
  10.     public boolean isAllowed(Filter filter) {
  11.         if (filter instanceof MyFilter) {
  12.             if (this.team == ((MyFilter)filter).team) {
  13.                 return false;
  14.             }
  15.         }
  16.         return baseFilter.isAllowed(filter);
  17.     }
  18. }


Yet another option is to filter them at the body level by extending the DefaultBroadphaseFilter class or implementing the BroadphaseFilter interface:
Code: [Select all] [Expand/Collapse] [Download] (Untitled.html)
  1. public class MyFilter extends DefaultBroadphaseFilter<Body, BodyFixture> {
  2.     @Override
  3.     public boolean isAllowed(Body body1, BodyFixture fixture1, Body body2, BodyFixture fixture2) {
  4.         if (!super.isAllowed(body1, fixture1, body2, fixture2)) {
  5.             return false;
  6.         }
  7.         MyData d1 = body1.getUserData();
  8.         MyData d2 = body2.getUserData();
  9.         return d1.getTeam() != d2.getTeam();
  10.     }
  11. }
  12.  
  13. // elsewhere...
  14. World world = new World();
  15. world.setDetectBroadphaseFilter(new MyFilter());


William

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

Re: CategoryFilters and Custom filter combined?

Postby assofohdz » Thu Jun 15, 2017 7:50 am

Thank you so much.

I need the second or last option, my game can have an arbitrary number of teams (say its an Integer, it can be anything - and it simply defines who your enemies are). So I'll probably create a lookup in a filter, that gets the current team of the body each time its checking for allowed.

Would the broad phase detector filter override the CategoryFilter I have already set using .setFilter ?


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest