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

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

Introduction

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

Prototype

public Vector2() 

Source Link

Document

Constructs a new vector at (0,0)

Usage

From source file:com.kotcrab.vis.editor.module.physicseditor.util.earclipping.bayazit.BayazitDecomposer.java

License:Apache License

public static Vector2 LineIntersect(Vector2 p1, Vector2 p2, Vector2 q1, Vector2 q2) {
    Vector2 i = new Vector2();
    float a1 = p2.y - p1.y;
    float b1 = p1.x - p2.x;
    float c1 = a1 * p1.x + b1 * p1.y;
    float a2 = q2.y - q1.y;
    float b2 = q1.x - q2.x;
    float c2 = a2 * q1.x + b2 * q1.y;
    float det = a1 * b2 - a2 * b1;
    if (!FloatEquals(det, 0)) {
        // lines are not parallel
        i.x = (b2 * c1 - b1 * c2) / det;
        i.y = (a1 * c2 - a2 * c1) / det;
    }/*from  w w  w  .  ja va2  s .c om*/
    return i;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.earclipping.bayazit.BayazitDecomposer.java

License:Apache License

public static Boolean LineIntersect(Vector2 point1, Vector2 point2, Vector2 point3, Vector2 point4,
        Boolean firstIsSegment, Boolean secondIsSegment, Vector2 point) {
    point = new Vector2();
    // these are reused later.
    // each lettered sub-calculation is used twice, except
    // for b and d, which are used 3 times
    float a = point4.y - point3.y;
    float b = point2.x - point1.x;
    float c = point4.x - point3.x;
    float d = point2.y - point1.y;
    // denominator to solution of linear system
    float denom = (a * b) - (c * d);
    // if denominator is 0, then lines are parallel
    if (!(denom >= -Epsilon && denom <= Epsilon)) {
        float e = point1.y - point3.y;
        float f = point1.x - point3.x;
        float oneOverDenom = 1.0f / denom;
        // numerator of first equation
        float ua = (c * e) - (a * f);
        ua *= oneOverDenom;/*from  ww w  .  j  a  va  2  s .  c o m*/
        // check if intersection point of the two lines is on line segment 1
        if (!firstIsSegment || ua >= 0.0f && ua <= 1.0f) {
            // numerator of second equation
            float ub = (b * e) - (d * f);
            ub *= oneOverDenom;
            // check if intersection point of the two lines is on line
            // segment 2
            // means the line segments intersect, since we know it is on
            // segment 1 as well.
            if (!secondIsSegment || ub >= 0.0f && ub <= 1.0f) {
                // check if they are coincident (no collision in this case)
                if (ua != 0f || ub != 0f) {
                    // There is an intersection
                    point.x = point1.x + ua * b;
                    point.y = point1.y + ua * d;
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static Array<Array<Vector2>> createPolygon(PolygonCreationAssistance pca) throws Exception {
    Array<Array<Vector2>> polygons = new Array<Array<Vector2>>();
    Array<Vector2> polygon;
    Array<Vector2> holePolygon;
    Vector2 holeEntrance = null;//from  w w w  .  ja v  a  2  s. c o m
    Vector2 polygonEntrance = null;
    Array<Vector2> blackList = new Array<Vector2>();
    // First of all: Check the array you just got.
    if (pca.IsValid()) {
        boolean searchOn;
        do {
            if (polygons.size == 0) {
                polygon = CreateSimplePolygon(pca, new Vector2(), new Vector2());
                if (polygon != null && polygon.size > 2) {
                    polygonEntrance = GetTopMostVertex(polygon);
                }
            } else if (polygonEntrance != null) {
                polygon = CreateSimplePolygon(pca, polygonEntrance,
                        new Vector2(polygonEntrance.x - 1f, polygonEntrance.y));
            } else {
                break;
            }
            searchOn = false;
            if (polygon != null && polygon.size > 2) {
                if (pca.HoleDetection) {
                    do {
                        holeEntrance = GetHoleHullEntrance(pca, polygon, holeEntrance);
                        if (holeEntrance != null) {
                            if (!vectorListContains(blackList, holeEntrance)) {
                                blackList.add(holeEntrance);
                                holePolygon = CreateSimplePolygon(pca, holeEntrance,
                                        new Vector2(holeEntrance.x + 1, holeEntrance.y));
                                if (holePolygon != null && holePolygon.size > 2) {
                                    holePolygon.add(holePolygon.get(0));
                                    Reference<Integer> vertex2IndexRef = new Reference<Integer>(0);
                                    Reference<Integer> vertex1IndexRef = new Reference<Integer>(0);
                                    if (SplitPolygonEdge(polygon, EdgeAlignment.Vertical, holeEntrance,
                                            vertex1IndexRef, vertex2IndexRef)) {

                                        polygon.ensureCapacity(holePolygon.size);
                                        for (int i = holePolygon.size - 1; i <= 0; i--) {
                                            polygon.insert(vertex2IndexRef.v, holePolygon.get(i));
                                        }
                                    }
                                }
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    } while (true);
                }
                polygons.add(polygon);
                if (pca.MultipartDetection) {
                    // 1: 95 / 151
                    // 2: 232 / 252
                    //
                    polygonEntrance = new Vector2();
                    while (GetNextHullEntrance(pca, polygonEntrance, polygonEntrance)) {
                        boolean inPolygon = false;
                        for (int i = 0; i < polygons.size; i++) {
                            polygon = polygons.get(i);
                            if (InPolygon(pca, polygon, polygonEntrance)) {
                                inPolygon = true;
                                break;
                            }
                        }
                        if (!inPolygon) {
                            searchOn = true;
                            break;
                        }
                    }
                }
            }
        } while (searchOn);
    } else {
        throw new Exception(
                "Sizes don't match: Color array must contain texture width * texture height elements.");
    }
    return polygons;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static boolean DistanceToHullAcceptable(PolygonCreationAssistance pca, Array<Vector2> polygon,
        Vector2 point, boolean higherDetail) {
    if (polygon != null && polygon.size > 2) {
        Vector2 edgeVertex2 = polygon.get(polygon.size - 1).cpy();
        Vector2 edgeVertex1 = new Vector2();
        if (higherDetail) {
            for (int i = 0; i < polygon.size; i++) {
                edgeVertex1.set(polygon.get(i));
                if (LineTools.DistanceBetweenPointAndLineSegment(point, edgeVertex1, edgeVertex2) <= pca
                        .getHullTolerance()
                        || LineTools.DistanceBetweenPointAndPoint(point, edgeVertex1) <= pca
                                .getHullTolerance()) {
                    return false;
                }/*from w w  w  .  j  a v  a  2s  . c  o m*/
                edgeVertex2.set(polygon.get(i));
            }
            return true;
        } else {
            for (int i = 0; i < polygon.size; i++) {
                edgeVertex1.set(polygon.get(i));
                if (LineTools.DistanceBetweenPointAndLineSegment(point, edgeVertex1, edgeVertex2) <= pca
                        .getHullTolerance()) {
                    return false;
                }
                edgeVertex2.set(polygon.get(i));
            }
            return true;
        }
    }
    return false;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static Array<CrossingEdgeInfo> GetCrossingEdges(Array<Vector2> polygon, EdgeAlignment edgeAlign,
        int checkLine) throws Exception {
    Array<CrossingEdgeInfo> edges = new Array<CrossingEdgeInfo>();
    Vector2 slope = new Vector2();
    Vector2 edgeVertex1 = new Vector2();
    Vector2 edgeVertex2 = new Vector2();
    Vector2 slopePreview = new Vector2();
    Vector2 edgeVertexPreview = new Vector2();
    Vector2 crossingPoint = new Vector2();
    boolean addCrossingPoint;
    if (polygon.size > 1) {
        edgeVertex2.set(polygon.get(polygon.size - 1));
        switch (edgeAlign) {
        case Vertical:
            for (int i = 0; i < polygon.size; i++) {
                edgeVertex1.set(polygon.get(i));
                if ((edgeVertex1.y >= checkLine && edgeVertex2.y <= checkLine)
                        || (edgeVertex1.y <= checkLine && edgeVertex2.y >= checkLine)) {
                    if (edgeVertex1.y != edgeVertex2.y) {
                        addCrossingPoint = true;
                        slope.set(vectorSub(edgeVertex2, edgeVertex1));
                        if (edgeVertex1.y == checkLine) {
                            edgeVertexPreview.set(polygon.get((i + 1) % polygon.size));
                            slopePreview.set(vectorSub(edgeVertex1, edgeVertexPreview));
                            if (slope.y > 0) {
                                addCrossingPoint = (slopePreview.y <= 0);
                            } else {
                                addCrossingPoint = (slopePreview.y >= 0);
                            }/* www  .  j a  va 2 s.c o  m*/
                        }
                        if (addCrossingPoint) {
                            crossingPoint = new Vector2(
                                    (checkLine - edgeVertex1.y) / slope.y * slope.x + edgeVertex1.x, checkLine);
                            edges.add(new CrossingEdgeInfo(edgeVertex1, edgeVertex2, crossingPoint, edgeAlign));
                        }
                    }
                }
                edgeVertex2.set(edgeVertex1);
            }
            break;
        case Horizontal:
            throw new Exception("EdgeAlignment.Horizontal isn't implemented yet. Sorry.");
        }
    }
    edges.sort();
    // Collections.sort(edges);
    return edges;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static boolean SplitPolygonEdge(Array<Vector2> polygon, EdgeAlignment edgeAlign,
        Vector2 coordInsideThePolygon, Reference<Integer> vertex1IndexRef, Reference<Integer> vertex2IndexRef)
        throws Exception {
    Array<CrossingEdgeInfo> edges;
    Vector2 slope = new Vector2();
    int nearestEdgeVertex1Index = 0;
    int nearestEdgeVertex2Index = 0;
    boolean edgeFound = false;
    float shortestDistance = Float.MAX_VALUE;
    boolean edgeCoordFound = false;
    Vector2 foundEdgeCoord = new Vector2();
    vertex1IndexRef.v = 0;//from ww w  .  j a  v  a2 s .co m
    vertex2IndexRef.v = 0;
    switch (edgeAlign) {
    case Vertical:
        edges = GetCrossingEdges(polygon, EdgeAlignment.Vertical, (int) coordInsideThePolygon.y);
        foundEdgeCoord.y = coordInsideThePolygon.y;
        if (edges != null && edges.size > 1 && edges.size % 2 == 0) {
            float distance;
            for (int i = 0; i < edges.size; i++) {
                if (edges.get(i).CrossingPoint.x < coordInsideThePolygon.x) {
                    distance = coordInsideThePolygon.x - edges.get(i).CrossingPoint.x;
                    if (distance < shortestDistance) {
                        shortestDistance = distance;
                        foundEdgeCoord.x = edges.get(i).CrossingPoint.x;
                        edgeCoordFound = true;
                    }
                }
            }
            if (edgeCoordFound) {
                shortestDistance = Float.MAX_VALUE;
                int edgeVertex2Index = polygon.size - 1;
                int edgeVertex1Index;
                for (edgeVertex1Index = 0; edgeVertex1Index < polygon.size; edgeVertex1Index++) {
                    Vector2 tempVector1 = polygon.get(edgeVertex1Index).cpy();
                    Vector2 tempVector2 = polygon.get(edgeVertex2Index).cpy();
                    distance = LineTools.DistanceBetweenPointAndLineSegment(foundEdgeCoord, tempVector1,
                            tempVector2);
                    if (distance < shortestDistance) {
                        shortestDistance = distance;
                        nearestEdgeVertex1Index = edgeVertex1Index;
                        nearestEdgeVertex2Index = edgeVertex2Index;
                        edgeFound = true;
                    }
                    edgeVertex2Index = edgeVertex1Index;
                }
                if (edgeFound) {
                    slope.set(vectorSub(polygon.get(nearestEdgeVertex2Index),
                            polygon.get(nearestEdgeVertex1Index)));
                    slope.nor();
                    Vector2 tempVector = polygon.get(nearestEdgeVertex1Index).cpy();
                    distance = LineTools.DistanceBetweenPointAndPoint(tempVector, foundEdgeCoord);
                    vertex1IndexRef.v = nearestEdgeVertex1Index;
                    vertex2IndexRef.v = nearestEdgeVertex1Index + 1;
                    // distance * slope + polygon[vertex1Index]
                    polygon.insert(nearestEdgeVertex1Index,
                            vectorAdd(vectorMul(slope, distance), polygon.get(vertex1IndexRef.v)));
                    polygon.insert(nearestEdgeVertex1Index,
                            vectorAdd(vectorMul(slope, distance), polygon.get(vertex2IndexRef.v)));
                    return true;
                }
            }
        }
        break;
    case Horizontal:
        throw new Exception("EdgeAlignment.Horizontal isn't implemented yet. Sorry.");
    }
    return false;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static Array<Vector2> CreateSimplePolygon(PolygonCreationAssistance pca, Vector2 entrance,
        Vector2 last) {//from w  ww  .  j a v a2 s. co m
    boolean entranceFound = false;
    boolean endOfHull = false;
    Array<Vector2> polygon = new Array<Vector2>();
    Array<Vector2> hullArea = new Array<Vector2>();
    Array<Vector2> endOfHullArea = new Array<Vector2>();
    Vector2 current = new Vector2();
    Vector2 zeroVec = new Vector2();
    // Get the entrance point. //todo: alle moglichkeiten testen
    if (vectorEquals(entrance, zeroVec) || !pca.InBounds(entrance)) {
        entranceFound = GetHullEntrance(pca, entrance);
        if (entranceFound) {
            current.set(entrance.x - 1f, entrance.y);
        }
    } else {
        if (pca.IsSolid(entrance)) {
            if (IsNearPixel(pca, entrance, last)) {
                current.set(last);
                entranceFound = true;
            } else {
                Vector2 temp = new Vector2();
                if (SearchNearPixels(pca, false, entrance, temp)) {
                    current.set(temp);
                    entranceFound = true;
                } else {
                    entranceFound = false;
                }
            }
        }
    }
    if (entranceFound) {
        polygon.add(entrance);
        hullArea.add(entrance);
        Vector2 next = entrance.cpy();
        do {
            // Search in the pre vision list for an outstanding point.
            Vector2 outstanding = new Vector2();
            if (SearchForOutstandingVertex(hullArea, pca.getHullTolerance(), outstanding)) {
                if (endOfHull) {
                    // We have found the next pixel, but is it on the last
                    // bit of the
                    // hull?
                    if (vectorListContains(endOfHullArea, outstanding)
                            && !vectorListContains(polygon, outstanding)) {
                        // Indeed.
                        polygon.add(outstanding);
                    }
                    // That's enough, quit.
                    break;
                }
                // Add it and remove all vertices that don't matter anymore
                // (all the vertices before the outstanding).
                polygon.add(outstanding);
                int index = vectorListIndexOf(hullArea, outstanding);
                if (index == -1) {
                    int debug = 1;
                }
                if (index >= 0) {
                    // hullArea = hullArea.subList(index + 1,
                    // hullArea.size);

                    // Array<Vector2> newArray = new Array<Vector2>
                    // (hullArea.size - (index + 1));
                    int counter = 0;
                    for (int i = index + 1; i < hullArea.size; i++) {
                        Vector2 v = hullArea.get(index);
                        // newArray.add(v);
                        hullArea.set(counter, v);
                        counter++;
                    }
                    // hullArea.clear();
                    // hullArea = newArray;
                    for (int i = 0; i < index + 1; i++) {
                        hullArea.pop();
                    }
                }
            }
            // Last point gets current and current gets next. Our little
            // spider is
            // moving forward on the hull ;).
            last.set(current);
            current.set(next);
            // Get the next point on hull.
            next = new Vector2();
            if (GetNextHullPoint(pca, last, current, next)) {
                // Add the vertex to a hull pre vision list.
                hullArea.add(next);
            } else {
                // Quit
                break;
            }
            if (vectorEquals(next, entrance) && !endOfHull) {
                // It's the last bit of the hull, search on and exit at next
                // found
                // vertex.
                endOfHull = true;
                endOfHullArea.addAll(hullArea);
            }
        } while (true);
    }
    return polygon;
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static boolean SearchForOutstandingVertex(Array<Vector2> hullArea, float hullTolerance,
        Vector2 outstanding) {/*from   ww w .  j ava2 s  .c o  m*/
    Vector2 outstandingResult = new Vector2();
    boolean found = false;
    if (hullArea.size > 2) {
        int hullAreaLastPoint = hullArea.size - 1;
        Vector2 tempVector1;
        Vector2 tempVector2 = hullArea.get(0);
        Vector2 tempVector3 = hullArea.get(hullAreaLastPoint);
        // Search between the first and last hull point.
        for (int i = 1; i < hullAreaLastPoint; i++) {
            tempVector1 = hullArea.get(i);
            // Check if the distance is over the one that's tolerable.
            if (LineTools.DistanceBetweenPointAndLineSegment(tempVector1, tempVector2,
                    tempVector3) >= hullTolerance) {
                outstandingResult.set(hullArea.get(i));
                found = true;
                break;
            }
        }
    }
    outstanding.set(outstandingResult);
    return found;
}

From source file:com.me.mygdxgame.Entities.MydebugRenderer.java

License:Apache License

public MydebugRenderer(boolean drawBodies, boolean drawJoints, boolean drawAABBs, boolean drawInactiveBodies,
        boolean drawVelocities, boolean drawContacts) {
    // next we setup the immediate mode renderer
    renderer = new ShapeRenderer();

    // initialize vertices array
    for (int i = 0; i < vertices.length; i++)
        vertices[i] = new Vector2();

    this.drawBodies = drawBodies;
    this.drawJoints = drawJoints;
    this.drawAABBs = drawAABBs;
    this.drawInactiveBodies = drawInactiveBodies;
    this.drawVelocities = drawVelocities;
    this.drawContacts = drawContacts;
}

From source file:com.me.mygdxgame.WallActor.java

License:Apache License

private void boundsToM(Rectangle boundsPix, PolygonShape rectangleShape) {
    Vector2 halfSizeM = new Vector2(), centerM = new Vector2();
    boundsPix.getPosition(centerM);/*w w w  . java  2s  .co  m*/
    centerM.div(gdxTest.getPixelsPerMeter());
    boundsPix.getPosition(centerM);
    centerM.div(gdxTest.getPixelsPerMeter());

    rectangleShape.setAsBox(halfSizeM.x, halfSizeM.y, centerM, 0f);
}