Example usage for com.badlogic.gdx.math Polygon getTransformedVertices

List of usage examples for com.badlogic.gdx.math Polygon getTransformedVertices

Introduction

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

Prototype

public float[] getTransformedVertices() 

Source Link

Document

Calculates and returns the vertices of the polygon after scaling, rotation, and positional translations have been applied, as they are position within the world.

Usage

From source file:com.bladecoder.engine.model.Scene.java

License:Apache License

public void drawBBoxLines(ShapeRenderer renderer) {
    // renderer.begin(ShapeType.Rectangle);
    renderer.begin(ShapeType.Line);

    for (BaseActor a : actors.values()) {
        Polygon p = a.getBBox();

        if (p == null) {
            EngineLogger.error("ERROR DRAWING BBOX FOR: " + a.getId());
        }//  ww  w . java  2  s.  c  o m

        if (a instanceof ObstacleActor) {
            renderer.setColor(OBSTACLE_COLOR);
            renderer.polygon(p.getTransformedVertices());
        } else if (a instanceof AnchorActor) {
            renderer.setColor(Scene.ANCHOR_COLOR);
            renderer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY());
            renderer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS);
        } else {
            renderer.setColor(ACTOR_BBOX_COLOR);
            renderer.polygon(p.getTransformedVertices());
        }

        // Rectangle r = a.getBBox().getBoundingRectangle();
        // renderer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }

    if (polygonalNavGraph != null) {
        renderer.setColor(WALKZONE_COLOR);
        renderer.polygon(polygonalNavGraph.getWalkZone().getTransformedVertices());

        // DRAW LINEs OF SIGHT
        renderer.setColor(Color.WHITE);
        ArrayList<NavNodePolygonal> nodes = polygonalNavGraph.getGraphNodes();
        for (NavNodePolygonal n : nodes) {
            for (NavNodePolygonal n2 : n.neighbors) {
                renderer.line(n.x, n.y, n2.x, n2.y);
            }
        }
    }

    renderer.end();
}

From source file:com.bladecoder.engine.polygonalpathfinder.PolygonalNavGraph.java

License:Apache License

/**
 * Search the first polygon vertex inside the walkzone.
 * /*w  ww  . j a  v  a 2s.  c o m*/
 * @param p the polygon 
 * @param target the vertex found
 */
private void getFirstVertexInsideWalkzone(Polygon p, Vector2 target) {
    float verts[] = p.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
        if (PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], true)) {
            target.x = verts[i];
            target.y = verts[i + 1];

            return;
        }
    }
}

From source file:com.bladecoder.engine.polygonalpathfinder.PolygonalNavGraph.java

License:Apache License

public void createInitialGraph(Collection<BaseActor> actors) {
    graphNodes.clear();/* ww  w .j ava 2  s . co  m*/

    // 1.- Add WalkZone convex nodes
    float verts[] = walkZone.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
        if (!PolygonUtils.isVertexConcave(walkZone, i)) {
            graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1]));
        }
    }

    // 2.- Add obstacles concave nodes
    obstacles.clear();

    for (BaseActor a : actors) {
        if (a instanceof ObstacleActor && a.isVisible())
            obstacles.add(a.getBBox());
    }

    for (Polygon o : obstacles) {
        verts = o.getTransformedVertices();

        for (int i = 0; i < verts.length; i += 2) {
            if (PolygonUtils.isVertexConcave(o, i)
                    && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) {
                graphNodes.add(new NavNodePolygonal(verts[i], verts[i + 1]));
            }
        }
    }

    // 3.- CALC LINE OF SIGHTs
    for (int i = 0; i < graphNodes.size() - 1; i++) {
        NavNodePolygonal n1 = graphNodes.get(i);

        for (int j = i + 1; j < graphNodes.size(); j++) {
            NavNodePolygonal n2 = graphNodes.get(j);

            if (inLineOfSight(n1.x, n1.y, n2.x, n2.y)) {
                n1.neighbors.add(n2);
                n2.neighbors.add(n1);
            }
        }
    }
}

From source file:com.bladecoder.engine.polygonalpathfinder.PolygonalNavGraph.java

License:Apache License

private void addObstacleToGrapth(Polygon poly) {
    float verts[] = poly.getTransformedVertices();
    for (int i = 0; i < verts.length; i += 2) {
        if (PolygonUtils.isVertexConcave(poly, i)
                && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) {
            NavNodePolygonal n1 = new NavNodePolygonal(verts[i], verts[i + 1]);

            for (int j = 0; j < graphNodes.size(); j++) {
                NavNodePolygonal n2 = graphNodes.get(j);

                if (inLineOfSight(n1.x, n1.y, n2.x, n2.y)) {
                    n1.neighbors.add(n2);
                    n2.neighbors.add(n1);
                }//from   w  w  w  . j ava  2 s . co  m
            }

            graphNodes.add(n1);
        }
    }
}

From source file:com.bladecoder.engine.polygonalpathfinder.PolygonalNavGraph.java

License:Apache License

public boolean removeDinamicObstacle(Polygon poly) {
    boolean exists = obstacles.remove(poly);

    if (!exists)//ww  w.j a v a  2  s .c  o m
        return false;

    float verts[] = poly.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
        if (PolygonUtils.isVertexConcave(poly, i)
                && PolygonUtils.isPointInside(walkZone, verts[i], verts[i + 1], false)) {
            for (int j = 0; j < graphNodes.size(); j++) {
                NavNodePolygonal n = graphNodes.get(j);

                if (n.x == verts[i] && n.y == verts[i + 1]) {
                    graphNodes.remove(n);
                    j--;

                    for (NavNodePolygonal n2 : graphNodes) {
                        n2.neighbors.removeValue(n, true);
                    }

                }
            }
        }
    }

    return true;
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

public static boolean deletePoint(Polygon poly, float x, float y, float tolerance) {
    float verts[] = poly.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
        if (Vector2.dst(x, y, verts[i], verts[i + 1]) < tolerance) {
            deletePoint(poly, i);/*from  w w w.ja va  2s  .c  om*/

            return true;
        }
    }

    return false;
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

/**
 * Clamp the point to the nearest polygon segment
 * /*from   w w  w.  ja  v  a 2  s . c om*/
 * @param poly
 *            The polygon
 * @param x
 *            The original point X
 * @param y
 *            The original point Y
 * @param dest
 *            The clamped point
 * @return The segment where the clamped point belongs
 */
public static int getClampedPoint(Polygon poly, float x, float y, Vector2 dest) {
    float verts[] = poly.getTransformedVertices();
    float dTmp;

    Intersector.nearestSegmentPoint(verts[0], verts[1], verts[2], verts[3], x, y, dest);

    int nearest = 0;
    float d = Vector2.dst(x, y, dest.x, dest.y);

    for (int i = 2; i < verts.length; i += 2) {
        Intersector.nearestSegmentPoint(verts[i], verts[i + 1], verts[(i + 2) % verts.length],
                verts[(i + 3) % verts.length], x, y, tmp);
        dTmp = Vector2.dst(x, y, tmp.x, tmp.y);

        if (dTmp < d) {
            d = dTmp;
            nearest = i;
            dest.set(tmp);
        }
    }

    return nearest;
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

public static boolean isVertexConcave(Polygon poly, int index) {
    float verts[] = poly.getTransformedVertices();

    float currentX = verts[index];
    float currentY = verts[index + 1];
    float nextX = verts[(index + 2) % verts.length];
    float nextY = verts[(index + 3) % verts.length];
    float previousX = verts[index == 0 ? verts.length - 2 : index - 2];
    float previousY = verts[index == 0 ? verts.length - 1 : index - 1];

    float leftX = currentX - previousX;
    float leftY = currentY - previousY;
    float rightX = nextX - currentX;
    float rightY = nextY - currentY;

    float cross = (leftX * rightY) - (leftY * rightX);

    return cross < 0;
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

public static boolean isPointInside(Polygon polygon, float x, float y, boolean toleranceOnOutside) {
    float verts[] = polygon.getTransformedVertices();

    boolean inside = false;

    float oldX = verts[verts.length - 2];
    float oldY = verts[verts.length - 1];

    float oldSqDist = Vector2.dst2(oldX, oldY, x, y);

    for (int i = 0; i < verts.length; i += 2) {
        float newX = verts[i];
        float newY = verts[i + 1];
        float newSqDist = Vector2.dst2(newX, newY, x, y);

        if (oldSqDist + newSqDist + 2.0f * Math.sqrt(oldSqDist * newSqDist)
                - Vector2.dst2(newX, newY, oldX, oldY) < TOLERANCE_IS_POINT_INSIDE)
            return toleranceOnOutside;

        float leftX = newX;
        float leftY = newY;
        float rightX = oldX;
        float rightY = oldY;

        if (newX > oldX) {
            leftX = oldX;/*w w  w  .  j a v  a  2 s.c  om*/
            leftY = oldY;
            rightX = newX;
            rightY = newY;
        }

        if (leftX < x && x <= rightX && (y - leftY) * (rightX - leftX) < (rightY - leftY) * (x - leftX))
            inside = !inside;

        oldX = newX;
        oldY = newY;
        oldSqDist = newSqDist;
    }

    return inside;
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

public static boolean inLineOfSight(Vector2 p1, Vector2 p2, Polygon polygon, boolean obstacle) {
    tmp.set(p1);//from  w w  w  .j  av  a 2  s.co  m
    tmp2.set(p2);

    float verts[] = polygon.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
        if (lineSegmentsCross(tmp.x, tmp.y, tmp2.x, tmp2.y, verts[i], verts[i + 1],
                verts[(i + 2) % verts.length], verts[(i + 3) % verts.length]))
            return false;
    }

    tmp.add(tmp2);
    tmp.x /= 2;
    tmp.y /= 2;

    boolean result = PolygonUtils.isPointInside(polygon, tmp.x, tmp.y, !obstacle);

    return obstacle ? !result : result;
}