Example usage for com.badlogic.gdx.physics.box2d Filter Filter

List of usage examples for com.badlogic.gdx.physics.box2d Filter Filter

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Filter Filter.

Prototype

Filter

Source Link

Usage

From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java

License:Apache License

/**
 * Reads a file describing the collision boundaries that should be set
 * per-tile and adds static bodies to the boxd world.
 * /*  w  w  w  . j  a  v a 2 s.  c om*/
 * @param collisionsFile
 * @param world
 * @param pixelsPerMeter
 *            the pixels per meter scale used for this world
 */
public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) {
    /**
     * Detect the tiles and dynamically create a representation of the map
     * layout, for collision detection. Each tile has its own collision
     * rules stored in an associated file.
     * 
     * The file contains lines in this format (one line per type of tile):
     * tileNumber XxY,XxY XxY,XxY
     * 
     * Ex:
     * 
     * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31
     * 
     * For a 32x32 tileset, the above describes one line segment for tile #3
     * and two for tile #4. Tile #3 has a line segment across the top. Tile
     * #1 has a line segment across most of the top and a line segment from
     * the top to the bottom, 30 pixels in.
     */

    // FileHandle fh = Gdx.files.internal(collisionsFile);
    // String collisionFile = fh.readString();
    // String lines[] = collisionFile.split("\\r?\\n");

    // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new
    // HashMap<Integer, ArrayList<LineSegment>>();

    // /**
    // * Some locations on the map (perhaps most locations) are "undefined",
    // * empty space, and will have the tile type 0. This code adds an empty
    // * list of line segments for this "default" tile.
    // */
    // tileCollisionJoints.put(Integer.valueOf(0), new
    // ArrayList<LineSegment>());

    // for (int n = 0; n < lines.length; n++) {
    // String cols[] = lines[n].split(" ");
    // int tileNo = Integer.parseInt(cols[0]);
    //
    // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>();
    //
    // for (int m = 1; m < cols.length; m++) {
    // String coords[] = cols[m].split(",");
    //
    // String start[] = coords[0].split("x");
    // String end[] = coords[1].split("x");
    //
    // tmp.add(new LineSegment(Integer.parseInt(start[0]),
    // Integer.parseInt(start[1]),
    // Integer.parseInt(end[0]), Integer.parseInt(end[1]), ""));
    // }
    //
    // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp);
    // }

    ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>();

    for (int l = 0; l < getMap().getLayers().getCount(); l++) {
        MapLayer nextLayer = getMap().getLayers().get(l);
        if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) {
            TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer;
            if (layer.getProperties().containsKey("collide")) {
                for (int y = 0; y < layer.getHeight(); y++) {
                    for (int x = 0; x < layer.getWidth(); x++) {
                        Cell tile = layer.getCell(x, y);
                        if (tile != null) {
                            int tileID = tile.getTile().getId();

                            int start = 0;
                            String type = "";
                            if (tile.getTile().getProperties().containsKey("type")) {
                                type = (String) getMap().getTileSets().getTile(tileID).getProperties()
                                        .get("type");
                            }
                            if (layer.getProperties().containsKey("WALLS")) {
                                type = "WALLS";
                                addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32,
                                        collisionLineSegments, type);
                            } else if (layer.getProperties().containsKey("STAIRS")) {
                                if (!type.equals("LEFTSTAIRS")) {
                                    type = "STAIRS";
                                    addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32,
                                            collisionLineSegments, type);
                                } else {
                                    addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32,
                                            collisionLineSegments, type);
                                }
                            } else if (layer.getProperties().containsKey("PLATFORMS")) {
                                type = "PLATFORMS";
                                addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32,
                                        collisionLineSegments, type);
                            }
                        }
                    }
                }
            }
        } else

        {
            MapObjects objects = nextLayer.getObjects();
            for (MapObject o : objects) {
                RectangleMapObject rect = (RectangleMapObject) o;
                if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) {
                    Rectangle shape = rect.getRectangle();

                    BodyDef nodeBodyDef = new BodyDef();
                    nodeBodyDef.type = BodyDef.BodyType.StaticBody;
                    nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX,
                            (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX);

                    Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef);
                    if (!nextLayer.getName().equals("DOORS")) {
                        String set = (String) rect.getProperties().get("set");
                        nodeBody.setUserData(new AINode(Integer.parseInt(set)));
                    } else {
                        if (rect.getProperties().containsKey("to_level")
                                && rect.getProperties().containsKey("exitX")
                                && rect.getProperties().containsKey("exitY")) {
                            String to_level = (String) rect.getProperties().get("to_level");
                            String xPos = (String) rect.getProperties().get("exitX");
                            String yPos = (String) rect.getProperties().get("exitY");
                            nodeBody.setUserData(new Door(to_level, xPos, yPos));
                        } else {
                            nodeBody.setUserData(new Door("9999", "1024", "256"));

                        }
                    }

                    PolygonShape nodeShape = new PolygonShape();

                    nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX,
                            shape.height * 0.5f * Util.PIXEL_TO_BOX);
                    FixtureDef fixture = new FixtureDef();
                    fixture.shape = nodeShape;
                    fixture.isSensor = true;
                    fixture.density = 0;
                    fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS;
                    fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
                    nodeBody.createFixture(fixture);
                    nodeShape.dispose();
                }
            }
        }
    }

    int platnum = 0;
    for (LineSegment lineSegment : collisionLineSegments)

    {
        BodyDef groundBodyDef = new BodyDef();
        groundBodyDef.type = BodyDef.BodyType.StaticBody;
        groundBodyDef.position.set(0, 0);
        Body groundBody = GamePlayManager.world.createBody(groundBodyDef);
        if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) {
            groundBody.setUserData(
                    new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()));
        } else if (lineSegment.type.equals("WALLS")) {
            groundBody.setUserData(new Entity().setType(EntityType.WALL));
        } else {
            Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(),
                    lineSegment.end());
            groundBody.setUserData(plat);
            if (GamePlayManager.lowestPlatInLevel == null
                    || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) {
                GamePlayManager.lowestPlatInLevel = plat;
            }
        }
        EdgeShape environmentShape = new EdgeShape();

        environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter),
                lineSegment.end().scl(1 / pixelsPerMeter));
        FixtureDef fixture = new FixtureDef();
        fixture.shape = environmentShape;
        fixture.isSensor = true;
        fixture.density = 1f;
        fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS;
        fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES;
        groundBody.createFixture(fixture);
        environmentShape.dispose();
    }

    /**
     * Drawing a boundary around the entire map. We can't use a box because
     * then the world objects would be inside and the physics engine would
     * try to push them out.
     */

    TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3);

    BodyDef bodydef = new BodyDef();
    bodydef.type = BodyType.StaticBody;
    bodydef.position.set(0, 0);
    Body body = GamePlayManager.world.createBody(bodydef);

    // left wall
    EdgeShape mapBounds = new EdgeShape();
    if (level == 1)

    {
        mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
        body.setUserData(new Entity().setType(EntityType.WALL));
        Fixture fixture = body.createFixture(mapBounds, 0);
        Filter filter = new Filter();
        filter.categoryBits = Util.CATEGORY_PLATFORMS;
        filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
        fixture.setFilterData(filter);
    } else

    {
        mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f),
                new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
        body.setUserData(new LevelWall(level - 1));
        Fixture fixture = body.createFixture(mapBounds, 0);
        Filter filter = new Filter();
        filter.categoryBits = Util.CATEGORY_PLATFORMS;
        filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
        fixture.setFilterData(filter);
    }

    // right wall
    body = GamePlayManager.world.createBody(bodydef);
    body.setUserData(new LevelWall(level + 1));

    EdgeShape mapBounds2 = new EdgeShape();
    mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX),
            new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
    Fixture fixture = body.createFixture(mapBounds2, 0);
    Filter filter = new Filter();
    filter.categoryBits = Util.CATEGORY_PLATFORMS;
    filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER;
    fixture.setFilterData(filter);

    // roof
    body = GamePlayManager.world.createBody(bodydef);
    body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32),
            new Vector2(layer.getWidth() * 32, layer.getHeight())));
    EdgeShape mapBounds3 = new EdgeShape();
    mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX),
            new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX));
    fixture = body.createFixture(mapBounds3, 0);
    fixture.setFilterData(filter);

    mapBounds.dispose();
    mapBounds2.dispose();
    mapBounds3.dispose();

}

From source file:com.gdx.game.sprites.InteractiveTileObject.java

public void setCategoryFilter(short filterBit) {
    Filter filter = new Filter();
    filter.categoryBits = filterBit;//from w  w  w .j av  a  2  s.c om
    fixture.setFilterData(filter);
}

From source file:com.redtoorange.game.states.MissionState.java

License:Open Source License

/** Build a new lighting filter so the rays collide correctly. */
private void resetLightingFilter() {
    Filter f = new Filter();
    f.categoryBits = Global.LIGHT;//  w w w . j  a v a 2 s.  c om
    f.maskBits = Global.ENEMY | Global.WALL;
    Light.setGlobalContactFilter(f);
}

From source file:com.redtoorange.game.states.MissionState.java

License:Open Source License

/** Build all the walls for the game map based on the TMX's files objects layer. */
private void initWalls() {
    for (Rectangle r : gameMap.walls) {
        Filter w = new Filter();
        w.groupIndex = Global.WALL;/*from  ww  w . j a  va2 s .co m*/
        w.maskBits = Global.ENEMY | Global.PLAYER | Global.LIGHT | Global.BULLET_LIVE;

        Body b = Box2DFactory.createStaticBody(physicsSystem, r);
        b.getFixtureList().first().setFilterData(w);
        b.setUserData(r);
    }
}

From source file:loon.physics.PhysicsObject.java

License:Apache License

PhysicsObject(LTexture img, Body b) {
    this.body = b;
    this.texture = img;
    this.x = body.getPosition().x;
    this.y = body.getPosition().y;
    this.type = body.getType();
    this.color = LColor.white;
    this.polyType = Circle;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    this.width = texture.getWidth();
    this.height = texture.getHeight();
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.useInster = true;
    this.visible = true;
}

From source file:loon.physics.PhysicsObject.java

License:Apache License

private void init(int polyType, LTexture img, BodyType type, int x, int y, int w, int h) {
    this.visible = true;
    this.x = x;/*from  w  ww  . j  ava2 s  .  com*/
    this.y = y;
    this.width = w;
    this.height = h;
    if (width < 0) {
        width = 1;
    }
    if (height < 0) {
        height = 1;
    }
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.type = type;
    this.color = LColor.white;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    this.texture = img;
    if (texture == null) {
        this.polyType = polyType;
        this.useImage = false;
    } else {
        int index = img.hashCode();
        this.polyTriangles = lazyTriangles.get(index);
        if (polyTriangles == null) {
            PhysicsPolygon ppolygon = new PhysicsPolygon(img);
            polyTriangles = ppolygon.getPolygon().getTriangles();
            lazyTriangles.put(index, polyTriangles);
        }
        this.polyType = Other;
        this.useImage = true;
    }

}

From source file:org.loon.framework.android.game.physics.PhysicsObject.java

License:Apache License

PhysicsObject(Bitmap img, Body body) {
    this.body = body;
    this.texture = new LTexture(GLLoader.getTextureData(new LImage(img)), Format.LINEAR);
    this.x = (int) body.getPosition().x;
    this.y = (int) body.getPosition().y;
    this.type = body.getType();
    this.color = LColor.blue;
    this.polyType = Circle;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    this.width = texture.getWidth();
    this.height = texture.getHeight();
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.useInster = true;
    this.visible = true;
}

From source file:org.loon.framework.android.game.physics.PhysicsObject.java

License:Apache License

private void init(int polyType, int polyInterval, Bitmap img, BodyType type, int x, int y, int w, int h) {
    this.visible = true;
    this.x = x;// www .  jav a2s  .c o m
    this.y = y;
    this.width = w;
    this.height = h;
    if (width < 0) {
        width = 1;
    }
    if (height < 0) {
        height = 1;
    }
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.type = type;
    this.color = LColor.blue;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    if (img == null) {
        this.polyType = polyType;
        this.useImage = false;
    } else {
        SpriteImage spriteImage = new SpriteImage(img);
        spriteImage.setMakePolygonInterval(polyInterval);
        int index = GraphicsUtils.hashBitmap(img);
        this.polyTriangles = lazyTriangles.get(index);
        if (polyTriangles == null) {
            PhysicsPolygon ppolygon = new PhysicsPolygon(spriteImage, 0, 0);
            polyTriangles = ppolygon.getPolygon2D().getTriangles();
            lazyTriangles.put(index, polyTriangles);
        }
        this.polyType = Other;
        this.useImage = true;
    }
    if (img != null) {
        this.texture = new LTexture(GLLoader.getTextureData(new LImage(img)), Format.LINEAR);
    }
}

From source file:org.loon.framework.javase.game.physics.PhysicsObject.java

License:Apache License

PhysicsObject(BufferedImage img, Body body) {
    this.body = body;
    this.texture = new LTexture(GLLoader.getTextureData(img), Format.LINEAR);
    this.x = (int) body.getPosition().x;
    this.y = (int) body.getPosition().y;
    this.type = body.getType();
    this.color = GLColor.blue;
    this.polyType = Circle;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    this.width = texture.getWidth();
    this.height = texture.getHeight();
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.useInster = true;
    this.visible = true;
}

From source file:org.loon.framework.javase.game.physics.PhysicsObject.java

License:Apache License

private void init(int polyType, int polyInterval, BufferedImage img, BodyType type, int x, int y, int w,
        int h) {//from w ww . jav  a  2s .  c  om
    this.visible = true;
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
    if (width < 0) {
        width = 1;
    }
    if (height < 0) {
        height = 1;
    }
    this.halfWidth = width / 2;
    this.halfHeight = height / 2;
    this.type = type;
    this.color = GLColor.blue;
    this.filter = new Filter();
    this.bodyDef = new BodyDef();
    this.bodyDef.type = type;
    this.bodyDef.position.x = x;
    this.bodyDef.position.y = y;
    if (img == null) {
        this.polyType = polyType;
        this.useImage = false;
    } else {
        SpriteImage spriteImage = new SpriteImage(img);
        spriteImage.setMakePolygonInterval(polyInterval);
        int index = GraphicsUtils.hashImage(img);
        this.polyTriangles = lazyTriangles.get(index);
        if (polyTriangles == null) {
            PhysicsPolygon ppolygon = new PhysicsPolygon(spriteImage, 0, 0);
            polyTriangles = ppolygon.getPolygon2D().getTriangles();
            lazyTriangles.put(index, polyTriangles);
        }
        this.polyType = Other;
        this.useImage = true;
    }
    if (img != null) {
        this.texture = new LTexture(GLLoader.getTextureData(img), Format.LINEAR);
    }
}