有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Box2d过滤掩码,组类别

我有两个身体定义,创建:身体A和身体B,以及连接它们的关节

我希望主体A只有在它们链接时才能与主体B碰撞

同时,一个主体B不能碰撞另一个主体B,一个主体a不能碰撞另一个主体a

a身体B不能碰撞与他无关的身体a

我可以分配什么掩码、类别和组?我迷路了,我不明白

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    可能有几种方法可以实现这一点,但我想到的一种方法是: 设置类别和过滤器,使A和B通常不会碰撞

    接下来,用一个正整数为每对对象指定其唯一的组id。这将确保组内的对象彼此碰撞,而不考虑A和B的正常规则,而不同的组将使用正常规则(因此不会碰撞)

    组对象的构造函数可以采用组id,然后

    public JoinedObjects(int groupId){
    
        FixtureDef fdefa = new FixtureDef();
        fdefa.filter.categoryBits = 1;//category A
        fdefa.filter.maskBits = 0;//collide with nothing
    
        FixtureDef fdefb = new FixtureDef();
        fdefb.filter.categoryBits = 2;//category B
        fdefb.filter.maskBits = 0;//collide with nothing
    
        fdefa.filter.groupIndex = uniqueGroupId;//will collide with the rest of this group
        fdefb.filter.groupIndex = uniqueGroupId;//will collide with the rest of this group
    
        .....
    }
    

    然后创建每个组

    JoinedObjects firstGroup = new JoinedObjects(uniqueGroupId);
    uniqueGroupId++;
    JoinedObjects secondGroup = new JoinedObjects(uniqueGroupId);
    uniqueGroupId++;