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

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

Introduction

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

Prototype

@Override
    public Vector2 set(Vector2 v) 

Source Link

Usage

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  ava  2  s .  com
                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);
                            }//from w  ww . jav a2  s. co 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   w w  w. ja 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 boolean SearchForOutstandingVertex(Array<Vector2> hullArea, float hullTolerance,
        Vector2 outstanding) {/* w w  w. j  a  v  a2s.  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.kotcrab.vis.ui.widget.VisTextField.java

License:Apache License

private VisTextField findNextTextField(Array<Actor> actors, VisTextField best, Vector2 bestCoords,
        Vector2 currentCoords, boolean up) {
    Window modalWindow = findModalWindow(this);

    for (int i = 0, n = actors.size; i < n; i++) {
        Actor actor = actors.get(i);/*from  w  ww.ja  v  a 2  s .  co m*/
        if (actor == this)
            continue;
        if (actor instanceof VisTextField) {
            VisTextField textField = (VisTextField) actor;

            if (modalWindow != null) {
                Window nextFieldModalWindow = findModalWindow(textField);
                if (nextFieldModalWindow != modalWindow)
                    continue;
            }

            if (textField.isDisabled() || !textField.focusTraversal
                    || isActorVisibleInStage(textField) == false)
                continue;

            Vector2 actorCoords = actor.getParent()
                    .localToStageCoordinates(tmp3.set(actor.getX(), actor.getY()));
            if ((actorCoords.y < currentCoords.y
                    || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) {
                if (best == null || (actorCoords.y > bestCoords.y
                        || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) {
                    best = (VisTextField) actor;
                    bestCoords.set(actorCoords);
                }
            }
        } else if (actor instanceof Group)
            best = findNextTextField(((Group) actor).getChildren(), best, bestCoords, currentCoords, up);
    }
    return best;
}

From source file:com.minikara.ttfinput.TextField.java

License:Apache License

private TextField findNextTextField(Array<Actor> actors, TextField best, Vector2 bestCoords,
        Vector2 currentCoords, boolean up) {
    for (int i = 0, n = actors.size; i < n; i++) {
        Actor actor = actors.get(i);//from   w w  w.  j a  v  a 2s  . co  m
        if (actor == this)
            continue;
        if (actor instanceof TextField) {
            TextField textField = (TextField) actor;
            if (textField.isDisabled() || !textField.focusTraversal)
                continue;
            Vector2 actorCoords = actor.getParent()
                    .localToStageCoordinates(tmp3.set(actor.getX(), actor.getY()));
            if ((actorCoords.y < currentCoords.y
                    || (actorCoords.y == currentCoords.y && actorCoords.x > currentCoords.x)) ^ up) {
                if (best == null || (actorCoords.y > bestCoords.y
                        || (actorCoords.y == bestCoords.y && actorCoords.x < bestCoords.x)) ^ up) {
                    best = (TextField) actor;
                    bestCoords.set(actorCoords);
                }
            }
        } else if (actor instanceof Group)
            best = findNextTextField(((Group) actor).getChildren(), best, bestCoords, currentCoords, up);
    }
    return best;
}

From source file:com.redtoorange.game.components.physics.PhysicsComponent.java

License:Open Source License

/** @return The center position of the Box2D Body attached to this Component. */
public Vector2 getBodyPosition() {
    Vector2 position = new Vector2();

    if (body != null)
        position.set(body.getPosition());

    return position;
}

From source file:com.sertaogames.cactus2d.components.TileMapPhysics.java

License:Open Source License

private void createBodyFromBlocks(int begin, int last, int i) {
    Vector2 tileSize = new Vector2(tm.tilemap.tileWidth, tm.tilemap.tileHeight);

    Body rigidbody;//from www.ja v  a  2  s.  com
    PolygonShape groundPoly = new PolygonShape();
    int width = last - begin + 1;
    float height = tileSize.y;
    width *= tileSize.x;
    // System.out.println("i: "+i+" width: "+width);
    Vector2 temp = new Vector2(width, tileSize.y);
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE * 0.5f);
    groundPoly.setAsBox(temp.x, temp.y);

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.fixedRotation = true;
    groundBodyDef.type = BodyType.StaticBody;
    rigidbody = gameObject.world.createBody(groundBodyDef);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    fixtureDef.filter.categoryBits = 0x4;
    rigidbody.createFixture(fixtureDef);
    groundPoly.dispose();

    temp.set(
            new Vector2(begin * tileSize.x + width / 2, (tm.tilemap.height - i) * tileSize.y - tileSize.y / 2));
    temp.add(transform.getPosition());
    temp.mul(Cactus2DApplication.INV_PHYSICS_SCALE);
    rigidbody.setTransform(temp, transform.getAngle());
    rigidbody.setUserData(gameObject);
    bodies.add(rigidbody);
}

From source file:com.t0ast.parkour.training.ParkourEnvironment.java

private void calcFinalPos(Vector2 finalPos, ParkourEntity entity, float scl) {
    finalPos.set(entity.getPosition()).add(entity.getDirection().cpy().scl(scl));

    finalPos.x = MathUtils.clamp(finalPos.x, 0, this.width);
    finalPos.y = MathUtils.clamp(finalPos.y, 0, this.height);
}

From source file:com.tnf.ptm.common.CollisionMeshLoader.java

License:Apache License

private Vector2 newVec(Vector2 v) {
    Vector2 res = vectorPool.isEmpty() ? new Vector2() : vectorPool.remove(0);
    if (v != null) {
        res.set(v);
    }/*w ww.  j  av a  2s  . co  m*/
    return res;
}