Example usage for com.badlogic.gdx.maps.objects PolygonMapObject PolygonMapObject

List of usage examples for com.badlogic.gdx.maps.objects PolygonMapObject PolygonMapObject

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.objects PolygonMapObject PolygonMapObject.

Prototype

public PolygonMapObject(Polygon polygon) 

Source Link

Usage

From source file:com.rubentxu.juegos.core.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * creates {@link Fixture Fixtures} from a {@link MapObject}
 *
 * @param mapObject the {@link MapObject} to parse
 * @return an array of parsed {@link Fixture Fixtures}
 *///  www. j av a2 s .  c o m
public Fixture[] createFixtures(MapObject mapObject) {
    Polygon polygon;

    if (!(mapObject instanceof PolygonMapObject)
            || isConvex(polygon = ((PolygonMapObject) mapObject).getPolygon()))
        return new Fixture[] { createFixture(mapObject) };

    Polygon[] convexPolygons;
    if (triangulate) {
        if (areVerticesClockwise(polygon)) { // ensure the vertices are in counterclockwise order (not really necessary according to EarClippingTriangulator's javadoc, but sometimes better)
            Array<Vector2> vertices = new Array<Vector2>(toVector2Array(polygon.getVertices()));
            Vector2 first = vertices.removeIndex(0);
            vertices.reverse();
            vertices.insert(0, first);
            polygon.setVertices(toFloatArray(vertices.items));
        }
        convexPolygons = toPolygonArray(toVector2Array(
                new EarClippingTriangulator().computeTriangles(polygon.getTransformedVertices()).toArray()), 3);
    } else {
        Array<Array<Vector2>> convexPolys = BayazitDecomposer
                .convexPartition(new Array<Vector2>(toVector2Array(polygon.getTransformedVertices())));
        convexPolygons = new Polygon[convexPolys.size];
        for (int i = 0; i < convexPolygons.length; i++)
            convexPolygons[i] = new Polygon(
                    toFloatArray((Vector2[]) convexPolys.get(i).toArray(Vector2.class)));
    }

    // create the fixtures using the convex polygons
    Fixture[] fixtures = new Fixture[convexPolygons.length];
    for (int i = 0; i < fixtures.length; i++) {
        PolygonMapObject convexObject = new PolygonMapObject(convexPolygons[i]);
        convexObject.setColor(mapObject.getColor());
        convexObject.setName(mapObject.getName());
        convexObject.setOpacity(mapObject.getOpacity());
        convexObject.setVisible(mapObject.isVisible());
        convexObject.getProperties().putAll(mapObject.getProperties());
        fixtures[i] = createFixture(convexObject);
    }

    return fixtures;
}

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

License:Apache License

/** creates {@link Fixture Fixtures} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return an array of parsed {@link Fixture Fixtures} */
public Fixture[] createFixtures(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixtures(mapObject)) == null)
        return null;

    Polygon polygon;// www.  ja v a  2s . c om

    if (!(mapObject instanceof PolygonMapObject)
            || isConvex(polygon = ((PolygonMapObject) mapObject).getPolygon())
                    && (!Box2DUtils.checkPreconditions
                            || polygon.getVertices().length / 2 <= Box2DUtils.maxPolygonVertices))
        return new Fixture[] { createFixture(mapObject, body) };

    Polygon[] convexPolygons = triangulate ? triangulate(polygon) : decompose(polygon);
    Fixture[] fixtures = new Fixture[convexPolygons.length];
    for (int i = 0; i < fixtures.length; i++) {
        PolygonMapObject convexObject = new PolygonMapObject(convexPolygons[i]);
        convexObject.setColor(mapObject.getColor());
        convexObject.setName(mapObject.getName());
        convexObject.setOpacity(mapObject.getOpacity());
        convexObject.setVisible(mapObject.isVisible());
        convexObject.getProperties().putAll(mapObject.getProperties());
        fixtures[i] = createFixture(convexObject, body);
    }

    return fixtures;
}

From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java

License:Apache License

protected void loadObject(MapLayer layer, Element element) {
    if (element.getName().equals("object")) {
        MapObject object = null;//  w  w  w  .  ja v a 2 s .co  m

        float scaleX = convertObjectToTileSpace ? 1.0f / mapTileWidth : 1.0f;
        float scaleY = convertObjectToTileSpace ? 1.0f / mapTileHeight : 1.0f;

        float x = element.getIntAttribute("x", 0) * scaleX;
        float y = (yUp ? mapHeightInPixels - element.getIntAttribute("y", 0) : element.getIntAttribute("y", 0))
                * scaleY;

        float width = element.getIntAttribute("width", 0) * scaleX;
        float height = element.getIntAttribute("height", 0) * scaleY;

        if (element.getChildCount() > 0) {
            Element child = null;
            if ((child = element.getChildByName("polygon")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Integer.parseInt(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Integer.parseInt(point[1]) * scaleY;
                    if (yUp) {
                        vertices[i * 2 + 1] *= -1;
                    }
                }
                Polygon polygon = new Polygon(vertices);
                polygon.setPosition(x, y);
                object = new PolygonMapObject(polygon);
            } else if ((child = element.getChildByName("polyline")) != null) {
                String[] points = child.getAttribute("points").split(" ");
                float[] vertices = new float[points.length * 2];
                for (int i = 0; i < points.length; i++) {
                    String[] point = points[i].split(",");
                    vertices[i * 2] = Integer.parseInt(point[0]) * scaleX;
                    vertices[i * 2 + 1] = Integer.parseInt(point[1]) * scaleY;
                    if (yUp) {
                        vertices[i * 2 + 1] *= -1;
                    }
                }
                Polyline polyline = new Polyline(vertices);
                polyline.setPosition(x, y);
                object = new PolylineMapObject(polyline);
            } else if ((child = element.getChildByName("ellipse")) != null) {
                object = new EllipseMapObject(x, yUp ? y - height : y, width, height);
            }
        }
        if (object == null) {
            object = new RectangleMapObject(x, yUp ? y - height : y, width, height);
        }
        object.setName(element.getAttribute("name", null));
        String type = element.getAttribute("type", null);
        if (type != null) {
            object.getProperties().put("type", type);
        }
        int gid = element.getIntAttribute("gid", -1);
        if (gid != -1) {
            object.getProperties().put("gid", gid);
        }
        object.getProperties().put("x", x * scaleX);
        object.getProperties().put("y", (yUp ? y - height : y) * scaleY);
        object.setVisible(element.getIntAttribute("visible", 1) == 1);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(object.getProperties(), properties);
        }
        layer.getObjects().add(object);
    }
}

From source file:net.dermetfan.gdx.physics.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates {@link Fixture Fixtures} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return an array of parsed {@link Fixture Fixtures} */
public Fixture[] createFixtures(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixtures(mapObject)) == null)
        return null;

    Polygon polygon;//from  w  w  w.j av a2  s  . c om

    if (!(mapObject instanceof PolygonMapObject)
            || isConvex(polygon = ((PolygonMapObject) mapObject).getPolygon())
                    && Box2DUtils.check.isValidPolygonShape(polygon.getVertices()))
        return new Fixture[] { createFixture(mapObject, body) };

    Polygon[] convexPolygons = toPolygonArray(triangulate ? triangulate(polygon.getTransformedVertices())
            : decompose(polygon.getTransformedVertices()));
    Fixture[] fixtures = new Fixture[convexPolygons.length];
    for (int i = 0; i < fixtures.length; i++) {
        PolygonMapObject convexObject = new PolygonMapObject(convexPolygons[i]);
        convexObject.setColor(mapObject.getColor());
        convexObject.setName(mapObject.getName());
        convexObject.setOpacity(mapObject.getOpacity());
        convexObject.setVisible(mapObject.isVisible());
        convexObject.getProperties().putAll(mapObject.getProperties());
        fixtures[i] = createFixture(convexObject, body);
    }

    return fixtures;
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates {@link Fixture Fixtures} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return an array of parsed {@link Fixture Fixtures} */
public Fixture[] createFixtures(MapObject mapObject, Body body) {
    Polygon polygon;//w w w. j a v a 2  s. c  o  m

    if (!(mapObject instanceof PolygonMapObject)
            || isConvex(polygon = ((PolygonMapObject) mapObject).getPolygon()))
        return new Fixture[] { createFixture(mapObject, body) };

    Polygon[] convexPolygons;
    if (triangulate) {
        if (areVerticesClockwise(polygon)) { // ensure the vertices are in counterclockwise order (not really necessary according to EarClippingTriangulator's javadoc, but sometimes better)
            Array<Vector2> vertices = new Array<Vector2>(toVector2Array(polygon.getVertices()));
            Vector2 first = vertices.removeIndex(0);
            vertices.reverse();
            vertices.insert(0, first);
            polygon.setVertices(toFloatArray(vertices.items));
        }
        convexPolygons = triangulate(polygon);
    } else
        convexPolygons = decompose(polygon);

    // create the fixtures using the convex polygons
    Fixture[] fixtures = new Fixture[convexPolygons.length];
    for (int i = 0; i < fixtures.length; i++) {
        PolygonMapObject convexObject = new PolygonMapObject(convexPolygons[i]);
        convexObject.setColor(mapObject.getColor());
        convexObject.setName(mapObject.getName());
        convexObject.setOpacity(mapObject.getOpacity());
        convexObject.setVisible(mapObject.isVisible());
        convexObject.getProperties().putAll(mapObject.getProperties());
        fixtures[i] = createFixture(convexObject, body);
    }

    return fixtures;
}