Example usage for com.badlogic.gdx.math EarClippingTriangulator EarClippingTriangulator

List of usage examples for com.badlogic.gdx.math EarClippingTriangulator EarClippingTriangulator

Introduction

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

Prototype

EarClippingTriangulator

Source Link

Usage

From source file:com.flatfisk.gnomp.math.GeometryUtils.java

License:Apache License

/**
 * @param concave the concave polygon to triangulate
 * @return an array of triangles representing the given concave polygon
 * @see com.badlogic.gdx.math.EarClippingTriangulator#computeTriangles(float[])
 *///from w  ww . j  a  v  a2  s  . c  o  m
public static Polygon[] triangulate(Polygon concave) {
    Vector2[] polygonVertices = toVector2Array(concave.getTransformedVertices());
    ShortArray indices = new EarClippingTriangulator().computeTriangles(toFloatArray(polygonVertices));
    Vector2[] vertices = new Vector2[indices.size];
    for (int i = 0; i < indices.size; i++)
        vertices[i] = polygonVertices[indices.get(i)];
    return toPolygonArray(vertices, 3);
}

From source file:com.hajnar.GravityShip.GameObjects.Terrain.java

License:Apache License

public void generateMeshes() {
    ArrayList<Fixture> terrainFixtures = objectBody.getFixtureList();
    Vector2 boxVertex = new Vector2();
    meshes = new ArrayList<Mesh>();
    boundingBoxes = new ArrayList<BoundingBox>();
    EarClippingTriangulator ear = new EarClippingTriangulator();

    for (Fixture terrainFixture : terrainFixtures) {
        PolygonShape shape = (PolygonShape) terrainFixture.getShape();
        boxVertex = new Vector2();
        int vc = shape.getVertexCount();
        ArrayList<Vector2> boxVertices = new ArrayList<Vector2>();
        ArrayList<Vector2> triaBoxVertices = new ArrayList<Vector2>();

        for (int i = 0; i < vc; i++) {
            shape.getVertex(i, boxVertex);
            boxVertex = objectBody.getWorldPoint(boxVertex).mul(Helper.BOX_TO_WORLD);
            boxVertices.add(boxVertex.cpy());
        }//from www  .j av a2 s . c o m
        triaBoxVertices = (ArrayList<Vector2>) ear.computeTriangles(boxVertices);
        float[] meshVertices = new float[triaBoxVertices.size() * 4];
        short[] meshIndices = new short[triaBoxVertices.size()];
        for (int i = 0; i < triaBoxVertices.size(); i++) {
            meshVertices[i * 4] = triaBoxVertices.get(i).x;
            meshVertices[i * 4 + 1] = triaBoxVertices.get(i).y;
            meshVertices[i * 4 + 2] = triaBoxVertices.get(i).x * TEXTURE_SCALE;
            meshVertices[i * 4 + 3] = triaBoxVertices.get(i).y * TEXTURE_SCALE;
            meshIndices[i] = (short) i;
        }

        Mesh mesh = new Mesh(true, triaBoxVertices.size(), triaBoxVertices.size(),
                new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
                new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
        mesh.setVertices(meshVertices);
        mesh.setIndices(meshIndices);
        meshes.add(mesh);
        boundingBoxes.add(mesh.calculateBoundingBox());
    }
}

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}
 *///ww  w.  j av  a 2s.  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:dk.gruppeseks.bodtrd.engine.Game.java

private void drawFoV(float[] shape) {
    EarClippingTriangulator triangulator = new EarClippingTriangulator();
    ShortArray triangleIndices = triangulator.computeTriangles(shape);
    PolygonRegion polyReg = new PolygonRegion(_textureRegion, shape, triangleIndices.toArray());
    PolygonSprite polySprite = new PolygonSprite(polyReg);

    _polyBatch.begin();//from ww w.j a v  a2 s . c  om
    polySprite.draw(_polyBatch);
    _polyBatch.end();

    _shapeRenderer.setProjectionMatrix(_gameCamera.combined);
    _shapeRenderer.begin(ShapeType.Line);
    _shapeRenderer.setColor(Color.BROWN);
    _shapeRenderer.polygon(shape);
    _shapeRenderer.end();

}