怎么做身体上的掩饰?

时间:2016-06-23 12:20:42

标签: box2d

我是box2d的新手,我正在开发小游戏。我的游戏中有不同的演员,如

  1. 敌人
  2. 播放器
  3. 背景
  4. 子弹
  5. 默认情况下,在box2d中,所有人体都相互碰撞。我想删除某些身体上的碰撞

    敌人会与玩家发生碰撞。  玩家将与敌人和地面相撞。  背景不会与任何东西发生碰撞  子弹只会与地面相撞。

    我编写的代码如下所述

    // Actor Util创建所有actor

    公共类ActorUtils {

    public static World createWorld() {
        return new World(Constants.WORLD_GRAVITY, true);
    }
    
    public static Body createGround(World world, float x, float y, float width, float height) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.position.set(new Vector2(x, y));
        Body body = world.createBody(bodyDef);
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(width, height);
        Fixture fixture = body.createFixture(shape, Constants.GROUND_DENSITY);
    
        Filter filterData = fixture.getFilterData();
        filterData.categoryBits = ActorMasking.CATEGORY_GROUND;
        filterData.maskBits = ActorMasking.MASK_GROUND;
        body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT));
        fixture.setFilterData(filterData);
        shape.dispose();
        return body;
    }
    
    public static Body createRunner(World world) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(0, Constants.RUNNER_Y));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
        Body body = world.createBody(bodyDef);
        Fixture fixture = body.createFixture(shape, Constants.RUNNER_DENSITY);
        body.resetMassData();
        body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
    
    
        Filter filterData = fixture.getFilterData();
        filterData.categoryBits = ActorMasking.CATEGORY_PLAYER;
        filterData.maskBits = ActorMasking.MASK_PLAYER;
        fixture.setFilterData(filterData);
    
        shape.dispose();
        return body;
    }
    
    public static Body createEnemy(World world) {
        EnemyType enemyType = RandomUtils.getRandomEnemyType();
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.KinematicBody;
    
        float minX = 25.0f;
        float maxX = 40f;
    
        float minY = 1.5f;
        float maxY = 14.0f;
        Random rand = new Random();
    
        float finalX = rand.nextFloat() * (maxX - minX) + minX;
    
        float finalY = rand.nextFloat() * (maxY - minY) + minY;
    
        bodyDef.position.set(new Vector2(/*enemyType.getX()*/finalX, /*enemyType.getY())*/finalY));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
        Body body = world.createBody(bodyDef);
        Fixture fixture = body.createFixture(shape, enemyType.getDensity());
        body.resetMassData();
        EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions());
        body.setUserData(userData);
    
    
        Filter filterData = fixture.getFilterData();
        filterData.categoryBits = ActorMasking.CATEGORY_ENEMY;
        filterData.maskBits = ActorMasking.MASK_ENEMY;
        fixture.setFilterData(filterData);
    
        shape.dispose();
        return body;
    }
    
    
    public static Body createBullet(World world, Player player) {
        BulletType bulletType1 = BulletType.BULLET_TYPE_1;
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        float finalX = player.body.getPosition().x;
        float finalY = player.body.getPosition().y;
        bodyDef.position.set(new Vector2(finalX, finalY/*+0.5f*/));
    
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(bulletType1.getWidth() / 2, bulletType1.getHeight() / 2);
        Body body = world.createBody(bodyDef);
        Fixture fixture = body.createFixture(shape, 0.0f);
    
    
        body.resetMassData();
        BulletUserData userData = new BulletUserData(bulletType1.getWidth(), bulletType1.getHeight());
        body.setUserData(userData);
    
    
        Filter filterData = fixture.getFilterData();
        filterData.categoryBits = ActorMasking.CATEGORY_BULLET;
        filterData.maskBits = ActorMasking.MASK_BULLET;
        fixture.setFilterData(filterData);
    
        shape.dispose();
        return body;
    }
    

    }

    //所有类别和屏蔽位的屏蔽类

    公共类ActorMasking {

    public static final short CATEGORY_ENEMY = 0x0001;
    public static final short CATEGORY_PLAYER = 0x0002;
    public static final short CATEGORY_GROUND = 0x0004;
    public static final short CATEGORY_BULLET = 0x008;
    
    
    public static final short MASK_PLAYER = CATEGORY_ENEMY | CATEGORY_GROUND;
    public static final short MASK_ENEMY = CATEGORY_PLAYER| CATEGORY_GROUND;
    public static final short MASK_GROUND = CATEGORY_PLAYER | CATEGORY_BULLET;
    public static final short MASK_BULLET = CATEGORY_GROUND;
    

    }

    它无法正常工作,因为我的玩家会与敌人发生碰撞,敌人会与玩家发生碰撞。但是我的子弹不会与它碰撞的地面发生碰撞。

    请帮忙。

0 个答案:

没有答案