2016-10-12 75 views
0

爲什麼玩家不與地面碰撞?我使用過濾器錯了嗎? 我將所有常量存儲在一個名爲Constants的類中。這裏是我使用的代碼:如何正確使用過濾器?

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; 
} 

這裏是我如何定義我的播放器。我如果那是一個傾倒的問題,但我在LibGDX一個完整的新手...

public void defineMainPlayer1() { 
    BodyDef bDef = new BodyDef(); 
    bDef.position.set(128/Constants.PPM, 256/Constants.PPM); 
    bDef.type = BodyDef.BodyType.DynamicBody; 
    b2body = world.createBody(bDef); 

    PolygonShape shape = new PolygonShape(); 
    shape.setAsBox(42/2/Constants.PPM, 94/2/Constants.PPM); 

    FixtureDef fDef = new FixtureDef(); 
    fDef.shape = shape; 
    fDef.filter.categoryBits = Constants.PLAYER1_BIT; // Setting the filter 
    // for my Player 
    fDef.filter.maskBits = Constants.BRICK_BIT; 

    b2body.createFixture(fDef).setUserData(this); 

    EdgeShape head = new EdgeShape(); 
    head.set(new Vector2(-30/Constants.PPM, 49/Constants.PPM), 

    new Vector2(30/Constants.PPM, 49/Constants.PPM)); 
    fDef.shape = head; 
    fDef.isSensor = true; 

    b2body.createFixture(fDef).setUserData("head"); 

} 
+0

你可以顯示你的Constants.BRICK_BIT和Constants.BRICK_BIT的定義嗎? – Aleris

回答

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); 
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;