如何正确使用过滤器?

时间:2016-10-12 20:27:40

标签: libgdx

为什么玩家不与地面发生碰撞?我使用过滤器错了吗? 我将所有常量存储在一个名为Constants的类中。这是我使用的代码:

(Class<T>) theVars.getVariable(variableName).getType()

以下是我定义播放器的方法。我是一个转储问题,但我是LibGDX中的一个完整的新手...

for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
    Rectangle rect = ((RectangleMapObject) object).getRectangle();

    bDef.type = BodyDef.BodyType.StaticBody;
    bDef.position.set((rect.getX() + rect.getWidth() / 2)
    / Constants.PPM, (rect.getY() + rect.getHeight() / 2)
    / Constants.PPM);

    body = world.createBody(bDef);

    shape.setAsBox(rect.getWidth() / 2 / Constants.PPM,
    rect.getHeight() / 2 / Constants.PPM);
    fDef.shape = shape;
    body.createFixture(fDef);

    fDef.filter.categoryBits = Constants.BRICK_BIT;
    fDef.filter.maskBits = Constants.PLAYER1_BIT;
}

2 个答案:

答案 0 :(得分:0)

您应该更改

的顺序
body.createFixture(fDef);

fDef.filter.categoryBits = Constants.BRICK_BIT;
fDef.filter.maskBits = Constants.PLAYER1_BIT;

fDef.filter.categoryBits = Constants.BRICK_BIT;
fDef.filter.maskBits = Constants.PLAYER1_BIT;

body.createFixture(fDef);

答案 1 :(得分:0)

我在下面写了一个例子,这些应该给你一个明确的解释和想法。

short CAT_PLAYER = 0x001;
short CAT_ENEMY = 0x002;
short CAT_SENSOR = 0x004;
short CAT_WALL = 0x008;

short MASK_PLAYER = ~CAT_PLAYER; // cannot collide to a player
short MASK_ENEMY = ~CAT_ENEMY; // cannot collide to a enemy
short MASK_SENSOR = CAT_PLAYER; // can only collide to a player
short MASK_WALL = -1; // can collide to all

一个简单的过滤器,用于玩家的固定装置。这意味着玩家可以与除自身之外的所有人发生碰撞。

filter.categoryBits = CAT_PLAYER;
filter.maskBits = MASK_PLAYER;