Example usage for com.badlogic.gdx.math Polyline getVertices

List of usage examples for com.badlogic.gdx.math Polyline getVertices

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Polyline getVertices.

Prototype

public float[] getVertices() 

Source Link

Document

Returns vertices without scaling or rotation and without being offset by the polyline position.

Usage

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TmxMapWriter.java

License:Apache License

/** @param object the {@link MapObject} to write in TMX format
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(MapObject object) throws IOException {
    MapProperties props = object.getProperties();
    element("object");
    attribute("name", object.getName());
    if (props.containsKey("type"))
        attribute("type", getProperty(props, "type", ""));
    if (props.containsKey("gid"))
        attribute("gid", getProperty(props, "gid", 0));

    int objectX = getProperty(props, "x", 0);
    int objectY = getProperty(props, "y", 0);

    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();
        int height = round(rect.height);
        attribute("x", objectX).attribute("y", toYDown(objectY + height));
        attribute("width", round(rect.width)).attribute("height", height);
    } else if (object instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) object).getEllipse();
        int height = round(ellipse.height);
        attribute("x", objectX).attribute("y", toYDown(objectY + height));
        attribute("width", round(ellipse.width)).attribute("height", height);
        element("ellipse").pop();
    } else if (object instanceof CircleMapObject) {
        Circle circle = ((CircleMapObject) object).getCircle();
        attribute("x", objectX).attribute("y", objectY);
        attribute("width", round(circle.radius * 2)).attribute("height", round(circle.radius * 2));
        element("ellipse").pop();
    } else if (object instanceof PolygonMapObject) {
        attribute("x", objectX).attribute("y", toYDown(objectY));
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        element("polygon");
        attribute("points", points(GeometryUtils.toYDown(polygon.getVertices())));
        pop();/*from w  w w .j a v  a 2  s.  co m*/
    } else if (object instanceof PolylineMapObject) {
        attribute("x", objectX).attribute("y", toYDown(objectY));
        Polyline polyline = ((PolylineMapObject) object).getPolyline();
        element("polyline");
        attribute("points", points(GeometryUtils.toYDown(polyline.getVertices())));
        pop();
    }

    if (props.containsKey("rotation"))
        attribute("rotation", getProperty(props, "rotation", 0f));
    if (props.containsKey("visible"))
        attribute("visible", object.isVisible() ? 1 : 0);
    if (object.getOpacity() != 1)
        attribute("opacity", object.getOpacity());

    @SuppressWarnings("unchecked")
    Array<String> excludedKeys = Pools.obtain(Array.class);
    excludedKeys.clear();
    excludedKeys.add("type");
    excludedKeys.add("gid");
    excludedKeys.add("x");
    excludedKeys.add("y");
    excludedKeys.add("width");
    excludedKeys.add("height");
    excludedKeys.add("rotation");
    excludedKeys.add("visible");
    excludedKeys.add("opacity");
    tmx(props, excludedKeys);
    excludedKeys.clear();
    Pools.free(excludedKeys);

    pop();
    return this;
}

From source file:headmade.arttag.utils.MapUtils.java

License:Apache License

public static void loadMap(ArtTagScreen artTagScreen, String mapName) {
    if (artTagScreen.currentRoom != null) {
        unloadMap(artTagScreen);//  w  w w .jav a2  s .c  o  m
    }

    if (null != Player.instance.warpDirection) {
        Gdx.app.log(TAG, "Loading map " + mapName);
        if (DIRECTION_LEFT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == 0) {
                artTagScreen.currentRoomIndexX = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexX--;
            }
        } else if (DIRECTION_RIGHT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexX = 0;
            } else {
                artTagScreen.currentRoomIndexX++;
            }
        } else if (DIRECTION_TOP.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexY = 0;
            } else {
                artTagScreen.currentRoomIndexY++;
            }
        } else if (DIRECTION_BOTTOM.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == 0) {
                artTagScreen.currentRoomIndexY = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexY--;
            }
        }

    }

    boolean isNewRoom = true;
    if (artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] == null) {
        artTagScreen.currentRoom = new Room(mapName);
        artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] = artTagScreen.currentRoom;
    } else {
        artTagScreen.currentRoom = artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY];
        isNewRoom = false;
    }

    final Box2DMapObjectParser parser = new Box2DMapObjectParser(ArtTag.UNIT_SCALE);
    // final Box2DMapObjectParser.Listener.Adapter listener = new Box2DMapObjectParser.Listener.Adapter() {
    //
    // @Override
    // public void created(Fixture fixture, MapObject mapObject) {
    //
    // Gdx.app.log(TAG, "mapObject.getProperties()" + fixture.getFilterData().maskBits);
    // super.created(fixture, mapObject);
    // }
    //
    // };
    // parser.setListener(listener);

    final TiledMap map = artTagScreen.currentRoom.getMap();

    final String onGameOver = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    final String hideJobDesc = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    artTagScreen.onGameOver = onGameOver;
    artTagScreen.isHideJobDesc = hideJobDesc == null ? false : true;

    parser.load(artTagScreen.world, map);
    if (null == artTagScreen.mapRenderer) {
        // artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(artTagScreen.map, artTagScreen.getGame().getBatch());
        artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(map, parser.getUnitScale(),
                artTagScreen.getGame().getBatch());
    } else {
        artTagScreen.mapRenderer.setMap(map);
    }

    MapLayer layer = map.getLayers().get("objects");
    for (final MapObject mapObject : layer.getObjects()) {
        if (OBJ_ART.equals(mapObject.getName())) {
            if (isNewRoom) {
                // add Art only if this a new room. If this is an old room the art was created before.
                if (mapObject instanceof RectangleMapObject) {
                    createNewArt(artTagScreen, mapObject, ((RectangleMapObject) mapObject).getRectangle(),
                            parser.getUnitScale());
                } else {
                    Gdx.app.error(TAG, OBJ_ART + " has to be a Rectangle");
                }
            }
        } else if (OBJ_WARP.equals(mapObject.getName())) {
            final Body warp = createWarp(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String direction = mapObject.getProperties().get(PROP_DIRECTION, String.class);
            final String room = mapObject.getProperties().get(PROP_ROOM, String.class);
            warp.setUserData(new WarpVo(direction, room));
        } else if (OBJ_EXIT.equals(mapObject.getName())) {
            createExit(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(), parser.getUnitScale());
        } else if (OBJ_HINT.equals(mapObject.getName())) {
            final Body hintBody = createHint(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String hint = mapObject.getProperties().get(PROP_TEXT, String.class);
            hintBody.setUserData(hint);
        } else if (OBJ_PLAYER.equals(mapObject.getName())) {
            final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
            if (null == Player.instance.body) {
                final String direction = mapObject.getProperties().get("direction", String.class);
                if (null != Player.instance.warpDirection) {
                    if (Player.instance.warpDirection.equalsIgnoreCase(direction)) {
                        Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                                (e.y + e.height / 2f) * parser.getUnitScale());
                        Player.instance.warpDirection = null;
                    }
                } else if (direction == null) {
                    Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                            (e.y + e.height / 2f) * parser.getUnitScale());
                }
            }
        }
    }

    if (!isNewRoom) {
        // create ArtSensors for old Art
        for (final Art art : artTagScreen.currentRoom.getArtList()) {
            createArtSensor(artTagScreen, art);
        }
    }

    { // guards
        final HashMap<String, Guard> guards = new HashMap<String, Guard>();
        final Array<MapObject> paths = new Array<MapObject>();
        layer = map.getLayers().get("guards");
        for (final MapObject mapObject : layer.getObjects()) {
            if (mapObject.getName() != null && mapObject.getName().contains(OBJ_GUARD)) {
                final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
                final Guard g = new Guard();
                g.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                        (e.y + e.height / 2f) * parser.getUnitScale());
                guards.put(mapObject.getName().trim(), g);
            } else if (mapObject.getName() != null && mapObject.getName().contains(OBJ_PATH)) {
                paths.add(mapObject);
            } else {
                Gdx.app.log(TAG, "WTF");
            }
        }
        for (final MapObject mapObject : paths) {
            final PolylineMapObject pl = (PolylineMapObject) mapObject;
            final String ownerName = pl.getProperties().get("owner", String.class);
            final Guard g = guards.get(ownerName.trim());
            Gdx.app.log(TAG, "guards " + guards.keySet());
            if (g != null) {
                final Polyline orgPolyline = pl.getPolyline();
                final Polyline p = new Polyline(orgPolyline.getVertices());
                p.setScale(parser.getUnitScale(), parser.getUnitScale());
                p.setPosition(orgPolyline.getX() * parser.getUnitScale(),
                        orgPolyline.getY() * parser.getUnitScale());
                final float[] vertices = p.getTransformedVertices();
                for (int i = 0; i < vertices.length; i += 2) {
                    g.path.add(new Vector2(vertices[i], vertices[i + 1]));
                }
                artTagScreen.guards.add(g);
            } else {
                Gdx.app.log(TAG, "No guard for path " + ownerName);
            }
        }

    }

    layer = map.getLayers().get("lights");
    for (final MapObject mapObject : layer.getObjects()) {
        if (mapObject.getProperties().get("type", String.class).contains(LIGTH_POINT)) {
            if (mapObject instanceof EllipseMapObject) {
                createPointLight(artTagScreen, (EllipseMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_POINT + " light has to be a Circle not " + mapObject);
            }
        } else if (mapObject.getProperties().get("type", String.class).contains(LIGTH_CONE)) {
            if (mapObject instanceof PolygonMapObject) {
                createConeLight(artTagScreen, (PolygonMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_CONE + " light has to be a Polygon");
            }
        }
    }

}

From source file:im.ligas.worms.util.Utils.java

License:Open Source License

public static boolean objectCollisions(Vector2 vector, Array<Array<Shape2D>> arrays) {
    for (Array<Shape2D> objects : arrays) {
        for (Shape2D object : objects) {
            if (object instanceof Circle) {
                Circle circle = (Circle) object;
                if (circle.contains(vector)) {
                    return true;
                }/*from   w  w  w.  j ava  2  s  .c  o m*/
            } else if (object instanceof Polyline) {
                Polyline polyline = (Polyline) object;
                if (checkCollision(vector, polyline.getVertices(), false)) {
                    return true;
                }

            }
        }
    }
    return false;
}

From source file:im.ligas.worms.util.Utils.java

License:Open Source License

public static boolean objectCollisions(Vector2 start, Vector2 end, Array<Array<Shape2D>> arrays,
        Vector2 intersection) {/*from   ww  w  . j a  va  2 s . com*/
    Vector2 help = new Vector2();
    for (Array<Shape2D> objects : arrays) {
        for (Shape2D object : objects) {
            if (object instanceof Circle) {
                Circle circle = (Circle) object;
                help.x = circle.x;
                help.y = circle.y;
                if (Intersector.intersectSegmentCircle(start, end, help, circle.radius)) {
                    Intersector.nearestSegmentPoint(start, end, help, intersection);
                    return true;
                }
            } else if (object instanceof Polyline) {
                Polyline polyline = (Polyline) object;
                if (checkCollision(start, end, polyline.getVertices(), false, intersection)) {
                    return true;
                }

            }
        }
    }
    return false;
}

From source file:releasethekraken.path.SeaCreaturePath.java

/**
 * Constructs a new SeaCreaturePath/*from   ww  w  .j  a  va2  s . c o m*/
 * @param id The path's ID, from the level editor
 * @param parent The path that leads to this path, or null if this is the first path
 * @param nextPaths A list of paths that can be gone on after this path.  The list is empty if there are none.
 * @param polyline The Polyline object that holds the path data
 */
public SeaCreaturePath(int id, SeaCreaturePath parent, Array<SeaCreaturePath> nextPaths, Polyline polyline) {
    this.id = id;
    this.parent = parent;
    this.nextPaths = nextPaths;

    final float scale = 1 / 16F; //Scale it down to match the world units

    //Scale the polyline to match the world units.
    //This has to be done manually because Polyline.setScale() doesn't seem to work.
    float[] vertices = polyline.getTransformedVertices();
    for (int i = 0; i < vertices.length; i++)
        vertices[i] *= scale;
    polyline = new Polyline(vertices);

    this.polyline = polyline;

    vertices = polyline.getVertices(); //Get an array of vertices, in x1, y1, x2, y2,... format
    Vector2[] points = new Vector2[vertices.length / 2]; //Make an array of Vector2 objects

    //Convert the vertices to Vector2 objects
    for (int i = 0; i < points.length; i++) {
        float x = vertices[i * 2];
        float y = vertices[(i * 2) + 1];
        points[i] = new Vector2(x, y);
    }

    //TODO: Fix this!
    //Add additional starting and ending points so that all of the original points are used, due to how catmull rom spline works
    Array<Vector2> pointsList = new Array<Vector2>(points);
    Vector2 firstPoint = points[0].cpy().add(points[0].cpy().sub(points[1]));
    pointsList.insert(0, firstPoint);
    Vector2 lastPoint = points[points.length - 1].cpy()
            .add(points[points.length - 1].cpy().sub(points[points.length - 2]));
    pointsList.insert(pointsList.size, lastPoint);

    //Make the smooth path with the points
    this.path = new CatmullRomSpline<Vector2>(pointsList.toArray(), false);

    Gdx.app.log("SCPath " + this.toString(true), "Points: " + Arrays.toString(points));
}

From source file:tools.TiledMapHelper.java

License:Apache License

private void loadLines(World world) {
    System.out.println("LoadingLines");
    Array<PolylineMapObject> objects = getMap().getLayers().get("collision1").getObjects()
            .getByType(PolylineMapObject.class);
    //      System.out.println("printing array");
    //      System.out.println(objects.size);
    if (objects != null && objects.size != 0) {
        for (int j = 0; j < objects.size; j++) {
            PolylineMapObject poly = objects.get(j);
            float x = (Integer) (poly.getProperties().get("x"));
            float y = (Integer) (poly.getProperties().get("y"));
            x /= 16;//  w ww. j  a  v  a  2 s.  c o  m
            y /= 16;
            //            System.out.println("x = " + x );
            //            System.out.println("y = " + y );

            Polyline p = poly.getPolyline();
            float vertices[] = p.getVertices();
            //         float vertices[] = new float[]{0,0, 20,0};

            //            System.out.println("vertices:");
            for (int i = 0; i < vertices.length; i++) {
                //               System.out.println(vertices[i]);
                vertices[i] = (Float) ((vertices[i] / scale) + x);
                //               System.out.println(vertices[i]);
                i++;
                //               System.out.println(vertices[i]);
                vertices[i] = (Float) ((vertices[i] / scale) + y);
                //               System.out.println(vertices[i]);
            }
            createPolygon(world, vertices);
        }
    }

}