Example usage for com.badlogic.gdx.physics.box2d ChainShape createChain

List of usage examples for com.badlogic.gdx.physics.box2d ChainShape createChain

Introduction

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

Prototype

public void createChain(Vector2[] vertices) 

Source Link

Document

Create a chain with isolated end vertices.

Usage

From source file:com.siondream.core.physics.MapBodyManager.java

License:Open Source License

private Shape getPolyline(PolylineMapObject polylineObject) {
    float[] vertices = polylineObject.getPolyline().getTransformedVertices();
    Vector2[] worldVertices = new Vector2[vertices.length / 2];

    for (int i = 0; i < vertices.length / 2; ++i) {
        worldVertices[i] = new Vector2();
        worldVertices[i].x = vertices[i * 2] / units;
        worldVertices[i].y = vertices[i * 2 + 1] / units;
    }/*  ww w .java2 s . co  m*/

    ChainShape chain = new ChainShape();
    chain.createChain(worldVertices);
    return chain;
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** creates a deep copy of a {@link Shape}<br>
 *  <strong>Note: The {@link ChainShape#setPrevVertex(float, float) previous} and {@link ChainShape#setNextVertex(float, float) next} vertex of a {@link ChainShape} will not be copied since this is not possible due to the API.</strong>
 *  @param shape the {@link Shape} to copy
 *  @return a {@link Shape} exactly like the one passed in */
@SuppressWarnings("unchecked")
public static <T extends Shape> T clone(T shape) {
    T clone;/*from   ww w .ja v a2  s  .c o  m*/
    switch (shape.getType()) {
    case Circle:
        CircleShape circleClone = (CircleShape) (clone = (T) new CircleShape());
        circleClone.setPosition(((CircleShape) shape).getPosition());
        break;
    case Polygon:
        PolygonShape polyClone = (PolygonShape) (clone = (T) new PolygonShape()), poly = (PolygonShape) shape;
        float[] vertices = new float[poly.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            poly.getVertex(i, vec2_0);
            vertices[i++] = vec2_0.x;
            vertices[i] = vec2_0.y;
        }
        polyClone.set(vertices);
        break;
    case Edge:
        EdgeShape edgeClone = (EdgeShape) (clone = (T) new EdgeShape()), edge = (EdgeShape) shape;
        edge.getVertex1(vec2_0);
        edge.getVertex2(vec2_1);
        edgeClone.set(vec2_0, vec2_1);
        break;
    case Chain:
        ChainShape chainClone = (ChainShape) (clone = (T) new ChainShape()), chain = (ChainShape) shape;
        vertices = new float[chain.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            chain.getVertex(i, vec2_0);
            vertices[i++] = vec2_0.x;
            vertices[i] = vec2_0.y;
        }
        if (chain.isLooped())
            chainClone.createLoop(GeometryUtils.toVector2Array(vertices));
        else
            chainClone.createChain(vertices);
        break;
    default:
        return null;
    }
    clone.setRadius(shape.getRadius());
    return clone;
}

From source file:com.talas777.ZombieLord.Levels.HomeTown.java

License:Open Source License

@Override
public void applyCollisionBoundaries(World world, float pixels_per_meter) {
    // TODO Auto-generated method stub

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyDef.BodyType.StaticBody;
    Body groundBody = world.createBody(groundBodyDef);
    { // church/*w  ww  .j  ava2 s. c  o  m*/
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 1473, 1473, 1507, 1700, 1775, 1861, 1950, 1985, 1985, 1795, 1795, 1758, 1758 },
                new float[] { 322, 450, 480, 480, 565, 480, 480, 450, 322, 322, 342, 342, 322 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestone 1
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 1898, 1898, 1941, 1941 },
                new float[] { 225, 265, 265, 225 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestone 2
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 1953, 1953, 1985, 1985 },
                new float[] { 288, 322, 322, 288 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestones 3
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 1989, 1989, 2106, 2106 },
                new float[] { 226, 254, 254, 226 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestones 4
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 2026, 2026, 2043, 2043, 2047, 2047, 2107, 2107, 2069, 2069 },
                new float[] { 289, 329, 340, 350, 350, 382, 382, 352, 352, 289 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestones 5
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 1985, 1985, 2008, 2000 },
                new float[] { 353, 382, 382, 353 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // gravestone 6
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 2110, 2110, 2129, 2138, 2144 },
                new float[] { 288, 352, 352, 345, 290 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // farm fence (only outer)
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 2445, 2445, 2476, 2476, 2975, 2975, 2931, 2931, 3040, 3040, 3008, 3008, 3085,
                        3085, 3200 },
                new float[] { 1, 734, 734, 797, 797, 774, 774, 734, 734, 776, 776, 797, 797, 828, 828 });

        environmentShape.createChain(vertices); // not a loop!
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }

    { // pond (near square)
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 1906, 1906, 1961, 1971, 2026, 2026, 2002, 2002, 2022, 2047, 2150, 2162, 2162,
                        2217, 2224, 2274, 2289, 2315, 2324, 2380, 2380, 2305, 2293, 2280, 2200, 2190, 2159,
                        2159, 2135, 2122, 2002, 2002, 1970, 1970 },
                new float[] { 1048, 1135, 1135, 1108, 1108, 1127, 1151, 1194, 1194, 1232, 1232, 1254, 1293,
                        1293, 1172, 1171, 1143, 1136, 1108, 1101, 1010, 1010, 1000, 979, 979, 1001, 1001, 952,
                        939, 883, 883, 935, 953, 1033 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }

    { // top houses and fence
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 3206, 3138, 3138, 3099, 3099, 2946, 2946, 2908, 2908, 2880, 2880, 2754, 2754,
                        2717, 2717, 2689, 2689, 2654, 2654, 2563, 2563, 2348, 2348, 2304, 2304, 2207, 2207,
                        2112, 2112, 2077, 2077, 2020, 2016, 1922, 1922, 1953, 2269, 2303, 2303, 2403, 2403,
                        2369, 2388, 2479 },
                new float[] { 1409, 1409, 1446, 1446, 1408, 1408, 1439, 1439, 1409, 1409, 1440, 1440, 1505,
                        1505, 1442, 1442, 1477, 1477, 1440, 1440, 1512, 1512, 1799, 1799, 1666, 1666, 1536,
                        1536, 1569, 1569, 1536, 1536, 1730, 1730, 1974, 1983, 1983, 1974, 1822, 1822, 1859,
                        1874, 1925, 1948 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // bottom right house
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 2945, 2945, 2974, 3103, 3136, 3136, 3042, 3042 },
                new float[] { 1122, 1301, 1310, 1310, 1301, 1090, 1090, 1121 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // bottom left house
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 2658, 2658, 2627, 2626, 2657, 2848, 2880, 2876, 2845, 2845, 2816, 2816, 2784,
                        2784 },
                new float[] { 1121, 1154, 1154, 1332, 1342, 1342, 1334, 1152, 1152, 1120, 1120, 1152, 1152,
                        1120 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // big tree (at square)
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 1771, 1750, 1702, 1698, 1709, 1750, 1766, 1792, 1824, 1824, 1816, 1797 },
                new float[] { 1417, 1417, 1439, 1480, 1539, 1583, 1592, 1580, 1515, 1496, 1469, 1453 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // border, fences, water
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 3197, 1008, 1008, 950, 942, 916, 910, 910, 910, 980, 980, 950, 950, 1195, 1360,
                        1335, 1423, 1423, 1477, 1508, 1544, 1545, 1545, 1480, 1489, 1469, 1457, 1457, 1430,
                        1325, 1325, 1354, 1354, 1298, 1291, 1291, 1266, 1262, 1231, 1226, 1226, 1194, 1113,
                        1076, 1075, 1075, 1041, 1041, 915, 914, 897, 533, 533, 1328, 1328, 1427, 1454, 1454,
                        1504, 1539, 1677, 1759, 1759, 1654, 1653, 2357, 2357, 2700, 2700, 2796, 2796, 2755,
                        2716, 2715, 2631, 2587, 2539, 2533, 2533, 2484, 2467, 2386, 2377, 2383, 2382, 3198 },
                new float[] { 1, 2, 82, 82, 115, 119, 147, 198, 199, 257, 518, 540, 683, 687, 865, 911, 911,
                        978, 979, 1012, 1012, 1012, 1041, 1105, 1153, 1178, 1209, 1389, 1406, 1402, 1342, 1320,
                        1241, 1199, 1147, 1146, 1133, 984, 969, 921, 920, 888, 888, 922, 922, 1006, 1007, 980,
                        980, 980, 1008, 1019, 1558, 1576, 1442, 1442, 1466, 1535, 1584, 1584, 1714, 1714, 1781,
                        1782, 2888, 2888, 2922, 2922, 2728, 2728, 2377, 2327, 2327, 2327, 2232, 2232, 2172,
                        2137, 2136, 2128, 2040, 2035, 1958, 1938, 1938, 1467 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // water
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(
                new float[] { 1793, 1793, 1793, 1835, 1853, 1910, 1910, 1876, 1862, 1845, 1831 },
                new float[] { 1712, 1776, 1777, 1777, 1809, 1809, 1778, 1770, 1743, 1737, 1716 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // stall1
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 2945, 2945, 3007, 3007 },
                new float[] { 885, 1009, 1009, 885 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // stall2
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 3039, 3039, 3103, 3104 },
                new float[] { 887, 1009, 1009, 888 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // stall3
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 3136, 3136, 3166, 3167 },
                new float[] { 888, 976, 976, 888 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }

}

From source file:com.talas777.ZombieLord.Levels.SecondTownInn2.java

License:Open Source License

@Override
public void applyCollisionBoundaries(World world, float pixels_per_meter) {
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyDef.BodyType.StaticBody;
    Body groundBody = world.createBody(groundBodyDef);
    { // borders/*from   ww w.j  a  va2  s.  co  m*/
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 8, 8, 386, 386, 413, 413, 439, 439, 631, 631, 359, 359 },
                new float[] { 1792, 1952, 1952, 1936, 1936, 1953, 1953, 1737, 1737, 1696, 1696, 1791 });

        environmentShape.createLoop(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }
    { // stairs
        ChainShape environmentShape = new ChainShape();

        Vector2[] vertices = vectorize(new float[] { 63, 63, 33, 33 }, new float[] { 1919, 1857, 1857, 1919 });

        environmentShape.createChain(vertices);
        groundBody.createFixture(environmentShape, 0);
        environmentShape.dispose();
    }

}

From source file:de.cwclan.gdxtest.core.games.CarGame.java

@Override
public void show() {
    world = new World(new Vector2(0, -9.8f), true);
    debugRenderer = new Box2DDebugRenderer();
    batch = new SpriteBatch();

    camera = new OrthographicCamera(Gdx.graphics.getWidth() / 25, Gdx.graphics.getHeight() / 25);

    BodyDef bodyDef = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();
    FixtureDef wheelFixtureDef = new FixtureDef();

    //car//from  w  w w .j  ava2s. c  o  m
    fixtureDef.density = 5;
    fixtureDef.friction = .4f;
    fixtureDef.restitution = .3f;

    wheelFixtureDef.density = fixtureDef.density * 1.5f;
    wheelFixtureDef.friction = 50;
    wheelFixtureDef.restitution = .4f;
    car = new Car(world, fixtureDef, wheelFixtureDef, 0, 3, 3, 1.5f);

    Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {

        @Override
        public boolean keyDown(int keycode) {

            switch (keycode) {
            case Keys.ESCAPE:
                ((com.badlogic.gdx.Game) Gdx.app.getApplicationListener()).setScreen(new LevelMenu());
                break;

            }
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            camera.zoom += amount / 25f;
            return true;
        }
    }, car));

    /*
     * the ground
     */
    ChainShape groundShape = new ChainShape();
    groundShape.createChain(new Vector2[] { new Vector2(-50, 10), new Vector2(-49, 0), new Vector2(-40, 0),
            new Vector2(-30, -1), new Vector2(-20, 0), new Vector2(-10, -1), new Vector2(0, 1),
            new Vector2(10, 2), new Vector2(20, 0), new Vector2(30, -1), new Vector2(40, -2),
            new Vector2(50, 0) });

    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(0, 0);
    fixtureDef.friction = .5f;
    fixtureDef.restitution = 0;
    fixtureDef.shape = groundShape;
    world.createBody(bodyDef).createFixture(fixtureDef);
    groundShape.dispose();

    /*
     * another ground 
     */

    groundShape = new ChainShape();
    groundShape.createChain(
            new Vector2[] { new Vector2(40, -5), new Vector2(50, -3), new Vector2(60, 0), new Vector2(70, -1),
                    new Vector2(80, 3), new Vector2(90, -1), new Vector2(100, 1), new Vector2(110, 2),
                    new Vector2(120, 0), new Vector2(130, -1), new Vector2(140, -2), new Vector2(150, 0) });
    world.createBody(bodyDef).createFixture(fixtureDef);
    groundShape.dispose();

}

From source file:de.cwclan.gdxtest.core.games.JumperGame.java

@Override
public void show() {

    if (Gdx.app.getType() == Application.ApplicationType.Desktop) {
        Gdx.graphics.setDisplayMode((int) (Gdx.graphics.getHeight() / 1.5), Gdx.graphics.getHeight(), false);
    }//from  w  w w .j  ava  2 s  .  com

    world = new World(new Vector2(0, -9.8f), true);
    debugRenderer = new Box2DDebugRenderer();
    batch = new SpriteBatch();

    camera = new OrthographicCamera(Gdx.graphics.getWidth() / 25, Gdx.graphics.getHeight() / 25);

    BodyDef bodyDef = new BodyDef();
    FixtureDef fixtureDef = new FixtureDef();
    FixtureDef wheelFixtureDef = new FixtureDef();

    //car
    fixtureDef.density = 5;
    fixtureDef.friction = .4f;
    fixtureDef.restitution = .3f;

    wheelFixtureDef.density = fixtureDef.density * 1.5f;
    wheelFixtureDef.friction = 50;
    wheelFixtureDef.restitution = .4f;
    player = new Player(world, 0, 1, 1);
    world.setContactFilter(player);
    world.setContactListener(player);

    Gdx.input.setInputProcessor(new InputMultiplexer(new InputAdapter() {

        @Override
        public boolean keyDown(int keycode) {

            switch (keycode) {
            case Keys.ESCAPE:
                ((com.badlogic.gdx.Game) Gdx.app.getApplicationListener()).setScreen(new LevelMenu());
                break;

            }
            return false;
        }

        @Override
        public boolean scrolled(int amount) {
            camera.zoom += amount / 25f;
            return true;
        }

    }, player));

    /*
     * the ground
     */
    ChainShape groundShape = new ChainShape();
    buttomLeft = new Vector3(0, Gdx.graphics.getHeight(), 0);
    buttomRight = new Vector3(Gdx.graphics.getWidth(), buttomLeft.y, 0);
    camera.unproject(buttomLeft);
    camera.unproject(buttomRight);

    groundShape.createChain(new float[] { buttomLeft.x, buttomLeft.y, buttomRight.x, buttomRight.y });

    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(0, 0);
    fixtureDef.friction = .5f;
    fixtureDef.restitution = 0;
    fixtureDef.shape = groundShape;

    Body ground = world.createBody(bodyDef);
    ground.createFixture(fixtureDef);

    groundShape.dispose();

    //        //a test platform
    //        groundShape = new ChainShape();
    //        groundShape.createChain(new float[]{buttomRight.x / 10, 3, buttomRight.x / 4, 3});
    //        world.createBody(bodyDef).createFixture(fixtureDef);
    //        
    levelGenerator = new LevelGenerator(ground, buttomLeft.x, buttomRight.x, player.HEIGHT / 2,
            player.HEIGHT * 3, player.WIDTH * 1.5f, player.WIDTH * 4f, player.WIDTH / 3, 0);//10*MathUtils.degRad);

}

From source file:eu.asterics.component.actuator.ponggame.PongGameServer.java

License:Open Source License

private void createBounds() {
    GameWorldStatic.instance.boundVectors = boundVectors[nbPlayers - 1];

    // Create our body definition
    BodyDef boundsBodyDef = new BodyDef();
    // Set its world position
    boundsBodyDef.position.set(new Vector2(0, 0));

    // Create a body from the defintion and add it to the world
    boundsBody = world.createBody(boundsBodyDef);

    // Create a polygon shape
    ChainShape boundingBox = new ChainShape();
    boundingBox.createChain(boundVectors[nbPlayers - 1]);
    // Set the polygon shape as a box which is twice the size of our view
    // port and 10 high
    // Create a fixture from our polygon shape and add it to our ground body

    FixtureDef fixtureDef2 = new FixtureDef();
    fixtureDef2.shape = boundingBox;/*from  w  w  w.  j av a  2  s  . c  o  m*/
    fixtureDef2.density = 0f;
    fixtureDef2.friction = 0.0f;
    fixtureDef2.restitution = 0f; // Make it bounce a little bit
    boundsBody.createFixture(fixtureDef2);

    boundingBox.dispose();
}

From source file:genuini.world.WorldManager.java

private void createTerrainLayers(World world) {
    BodyDef bdef = new BodyDef();
    //go through all the cells in terrainLayer
    for (int row = 0; row < terrainLayer.getHeight(); row++) {
        for (int col = 0; col < terrainLayer.getWidth(); col++) {
            // get cell
            TiledMapTileLayer.Cell cell = terrainLayer.getCell(col, row);

            // check that there is a cell
            if (cell == null) {
                continue;
            }/*from   w ww . j  av a 2  s  .  c o m*/
            if (cell.getTile() == null) {
                continue;
            }
            // create body from cell
            bdef.type = BodyDef.BodyType.StaticBody;
            bdef.position.set((col + 0.5f) * tileSize / PPM, (row + 0.5f) * tileSize / PPM); //centered at center

            ChainShape cs = new ChainShape();
            FixtureDef fd = new FixtureDef();

            float box2dTileSize = tileSize / PPM;
            if (cell.getTile().getProperties().get("slide_left") != null) {
                //to link the cell edges
                Vector2[] v = new Vector2[4];
                v[0] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                v[1] = new Vector2(box2dTileSize / 2, box2dTileSize / 2);//top right corner
                v[2] = new Vector2(box2dTileSize / 2, -box2dTileSize / 2);//bottom right corner
                v[3] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                cs.createChain(v);
                fd.friction = 0.8f;
                fd.shape = cs;
                fd.isSensor = false;
                fd.filter.categoryBits = BIT_TERRAIN;
                fd.filter.maskBits = BIT_PLAYER | BIT_FIREBALL | BIT_MOB;
                world.createBody(bdef).createFixture(fd);
            } else if (cell.getTile().getProperties().get("slide_right") != null) {
                //to link the cell edges
                Vector2[] v = new Vector2[4];
                v[0] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                v[1] = new Vector2(-box2dTileSize / 2, box2dTileSize / 2);//top left corner
                v[2] = new Vector2(box2dTileSize / 2, -box2dTileSize / 2);//bottom right corner
                v[3] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                cs.createChain(v);
                fd.friction = 0.8f;
                fd.shape = cs;
                fd.isSensor = false;
                fd.isSensor = false;
                fd.filter.categoryBits = BIT_TERRAIN;
                fd.filter.maskBits = BIT_PLAYER | BIT_FIREBALL | BIT_MOB;
                world.createBody(bdef).createFixture(fd);
            } else if (cell.getTile().getProperties().get("bounce") != null) {
                //to link the cell edges
                Vector2[] v = new Vector2[5];
                v[0] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                v[1] = new Vector2(-box2dTileSize / 2, box2dTileSize / 9);//top left corner
                v[2] = new Vector2(box2dTileSize / 2, box2dTileSize / 9);//top right corner
                v[3] = new Vector2(box2dTileSize / 2, -box2dTileSize / 2);//bottom right corner
                v[4] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                cs.createChain(v);
                fd.friction = 0;
                fd.shape = cs;
                fd.isSensor = false;
                fd.filter.categoryBits = BIT_TERRAIN;
                fd.filter.maskBits = BIT_PLAYER | BIT_MOB;
                world.createBody(bdef).createFixture(fd).setUserData("bounce");
            } else if (cell.getTile().getProperties().get("spike") != null) {
                //to link the cell edges
                Vector2[] v = new Vector2[5];
                v[0] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                v[1] = new Vector2(-box2dTileSize / 2, -0.1f);//top left corner
                v[2] = new Vector2(box2dTileSize / 2, -0.1f);//top right corner
                v[3] = new Vector2(box2dTileSize / 2, -box2dTileSize / 2);//bottom right corner
                v[4] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                cs.createChain(v);
                fd.friction = 0;
                fd.shape = cs;
                fd.isSensor = false;
                fd.filter.categoryBits = BIT_TERRAIN;
                fd.filter.maskBits = BIT_PLAYER | BIT_MOB;
                world.createBody(bdef).createFixture(fd).setUserData("spike");
            } else {
                //to link the cell edges
                Vector2[] v = new Vector2[5];
                v[0] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                v[1] = new Vector2(-box2dTileSize / 2, box2dTileSize / 2);//top left corner
                v[2] = new Vector2(box2dTileSize / 2, box2dTileSize / 2);//top right corner
                v[3] = new Vector2(box2dTileSize / 2, -box2dTileSize / 2);//bottom right corner
                v[4] = new Vector2(-box2dTileSize / 2, -box2dTileSize / 2);//bottom left corner
                cs.createChain(v);
                fd.friction = 0.05f;
                fd.shape = cs;
                fd.isSensor = false;
                fd.filter.categoryBits = BIT_TERRAIN;
                fd.filter.maskBits = BIT_PLAYER | BIT_FIREBALL | BIT_MOB;
                world.createBody(bdef).createFixture(fd);
            }
            cs.dispose();
        }
    }
}

From source file:genuini.world.WorldManager.java

private ChainShape getPolyline(PolylineMapObject polylineObject) {
    float[] vertices = polylineObject.getPolyline().getTransformedVertices();
    Vector2[] worldVertices = new Vector2[vertices.length / 2];

    for (int i = 0; i < vertices.length / 2; ++i) {
        worldVertices[i] = new Vector2();
        worldVertices[i].x = vertices[i * 2] / PPM;
        worldVertices[i].y = vertices[i * 2 + 1] / PPM;
    }/*from   ww  w .  ja v  a2 s .c o  m*/

    ChainShape chain = new ChainShape();
    chain.createChain(worldVertices);
    return chain;
}

From source file:me.scarlet.undertailor.lua.meta.LuaWorldObjectMeta.java

License:Open Source License

public LuaWorldObjectMeta() {
    this.metatable = new LuaTable();

    // worldObject:getRoom()
    set("getRoom", asFunction(vargs -> {
        return orNil(obj(vargs).getRoom());
    }));/*  w  ww.j  av  a2 s  .  com*/

    // worldObject:getActor()
    set("getActor", asFunction(vargs -> {
        return orNil(obj(vargs).getActor());
    }));

    // worldObject:setActor(actor)
    set("setActor", asFunction(vargs -> {
        Renderable renderable = (Renderable) checkType(vargs.arg(2), LuaRenderableMeta.class).getObject();
        obj(vargs).setActor(renderable);
        return NIL;
    }));

    // worldObject:isVisible()
    set("isVisible", asFunction(vargs -> {
        return valueOf(obj(vargs).isVisible());
    }));

    // worldObject:setVisible(visible)
    set("setVisible", asFunction(vargs -> {
        obj(vargs).setVisible(vargs.checkboolean(2));
        return NIL;
    }));

    // worldObject:isPersistent()
    set("isPersistent", asFunction(vargs -> {
        return valueOf(obj(vargs).isPersistent());
    }));

    // worldObject:setPersistent(persistent)
    set("setPersistent", asFunction(vargs -> {
        obj(vargs).setPersistent(vargs.checkboolean(2));
        return NIL;
    }));

    // lua-only functions

    // we're modular and can actually be safely reused without error
    // worldObject:remove()
    set("remove", asFunction(vargs -> {
        WorldObject obj = obj(vargs);
        if (obj.getParent() != null) {
            return valueOf(obj.release(obj.getParent()));
        }

        return LuaValue.FALSE;
    }));

    // bounding shape creation methods respect overworld scale
    // worldObject:addBoundingPolygon(...)
    set("addBoundingPolygon", asFunction(vargs -> {
        WorldObject obj = obj(vargs);
        if (vargs.narg() % 2 != 0) {
            throw new LuaError("uneven points");
        }

        if (vargs.narg() < 6) {
            throw new LuaError("Polygons must have at least 3 points");
        }

        if (vargs.narg() > 16) {
            throw new LuaError("libGDX's Box2D will not permit polygons with more than 8 vertices");
        }

        float[] vertices = new float[vargs.narg()];
        for (int i = 0; i < vargs.narg(); i++) {
            vertices[i] = vargs.checknumber(i + 1).tofloat() * OverworldController.PIXELS_TO_METERS;
        }

        PolygonShape polygon = new PolygonShape();
        polygon.set(vertices);
        obj.queueBoundingShape(polygon);

        return NIL;
    }));

    // worldObject:addBoundingCircle(radius[, offsetX, offsetY])
    set("addBoundingCircle", asFunction(vargs -> {
        WorldObject obj = obj(vargs);
        float radius = vargs.checknumber(2).tofloat() * OverworldController.PIXELS_TO_METERS;
        Vector2 offset = new Vector2();
        offset.x = vargs.isnil(3) ? 0 : (vargs.checknumber(3).tofloat() * OverworldController.PIXELS_TO_METERS);
        offset.y = vargs.isnil(4) ? 0 : (vargs.checknumber(4).tofloat() * OverworldController.PIXELS_TO_METERS);

        CircleShape circle = new CircleShape();
        circle.setRadius(radius);
        circle.setPosition(offset);
        obj.queueBoundingShape(circle);

        return NIL;
    }));

    // worldObject:addBoundingBox(width, height[, offsetX, offsetY])
    set("addBoundingBox", asFunction(vargs -> {
        WorldObject obj = obj(vargs);
        float width = vargs.checknumber(2).tofloat() * OverworldController.PIXELS_TO_METERS;
        float height = vargs.checknumber(3).tofloat() * OverworldController.PIXELS_TO_METERS;
        Vector2 offset = new Vector2();
        offset.x = vargs.isnil(3) ? 0 : (vargs.checknumber(3).tofloat() * OverworldController.PIXELS_TO_METERS);
        offset.y = vargs.isnil(4) ? 0 : (vargs.checknumber(4).tofloat() * OverworldController.PIXELS_TO_METERS);
        // setAsBox seems to offset itself to have the box centered when used without an offset param, so we do this too
        offset.x -= width;

        PolygonShape box = new PolygonShape();
        box.setAsBox(width / 2F, height / 2F, offset, 0F);
        obj.queueBoundingShape(box);

        return NIL;
    }));

    // worldObject:addBoundingChain(...)
    set("addBoundingChain", asFunction(vargs -> {
        WorldObject obj = obj(vargs);

        float[] vertices = new float[vargs.narg()];
        for (int i = 0; i < vargs.narg(); i++) {
            vertices[i] = vargs.checknumber(i + 1).tofloat() * OverworldController.PIXELS_TO_METERS;
        }

        ChainShape chain = new ChainShape();
        chain.createChain(vertices);
        obj.queueBoundingShape(chain);

        return NIL;
    }));

    // worldObject:addBoundingChainLoop(...)
    set("addBoundingChainLoop", asFunction(vargs -> {
        WorldObject obj = obj(vargs);

        float[] vertices = new float[vargs.narg()];
        for (int i = 0; i < vargs.narg(); i++) {
            vertices[i] = vargs.checknumber(i + 1).tofloat() * OverworldController.PIXELS_TO_METERS;
        }

        ChainShape chain = new ChainShape();
        chain.createLoop(vertices);
        obj.queueBoundingShape(chain);

        return NIL;
    }));
}