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(Vector2 v) 

Source Link

Document

Constructs a vector from the given vector

Usage

From source file:aurelienribon.texturepackergui.PanZoomInputProcessor.java

License:Apache License

@Override
public boolean touchDragged(int x, int y, int pointer) {
    Vector2 p = canvas.screenToWorld(x, y);
    Vector2 delta = new Vector2(p).sub(lastTouch);
    canvas.getCamera().translate(-delta.x, -delta.y, 0);
    canvas.getCamera().update();//from  w  w  w .  j av  a 2s  .  c om
    lastTouch.set(canvas.screenToWorld(x, y));
    return false;
}

From source file:ca.hiphiparray.amazingmaze.Player.java

License:Open Source License

/**
 * Update the player's status./*  w ww  .  ja  v  a  2s . com*/
 *
 * @param deltaTime how much time has passed since the last frame.
 */
protected void update(float deltaTime) {
    Point2D.Float newPos = doObjectCollision(new Vector2(direction).scl(deltaTime));
    setPosition(newPos.x, newPos.y);
    handleDeath();
    collectFish();
    collectCheese();

    updateImage(deltaTime);

    lastHorizontalDir = horizontalDir;
    lastVerticalDir = verticalDir;
}

From source file:com.ahmet.b2d.EarClippingTriangulator.java

License:Apache License

private void cutEarTip(final ArrayList<Vector2> pVertices, final int pEarTipIndex,
        final ArrayList<Vector2> pTriangles) {
    final int previousIndex = EarClippingTriangulator.computePreviousIndex(pVertices, pEarTipIndex);
    final int nextIndex = EarClippingTriangulator.computeNextIndex(pVertices, pEarTipIndex);

    if (!EarClippingTriangulator.isCollinear(pVertices, previousIndex, pEarTipIndex, nextIndex)) {
        pTriangles.add(new Vector2(pVertices.get(previousIndex)));
        pTriangles.add(new Vector2(pVertices.get(pEarTipIndex)));
        pTriangles.add(new Vector2(pVertices.get(nextIndex)));
    }/*from w  w  w  . j  a v  a 2s  . co  m*/

    pVertices.remove(pEarTipIndex);
    if (pVertices.size() >= 3) {
        EarClippingTriangulator.removeCollinearNeighborEarsAfterRemovingEarTip(pVertices, pEarTipIndex);
    }
}

From source file:com.ahsgaming.valleyofbones.ai.AIPlayer.java

License:Apache License

@Override
public void update(GameController controller, float delta) {
    super.update(controller, delta);

    if (genome == null) {
        Json json = new Json();
        genome = json.fromJson(GenomicAI.class, Gdx.files.internal("ai/xjix3xuv").readString());
        Gdx.app.log(LOG, "Loaded ai: " + genome.id);
    }//  w  ww .j  ava2s  . c om

    if (controller.getCurrentPlayer().getPlayerId() == getPlayerId()) {
        timer -= delta;
        if (timer < 0) {
            timer = countdown;

            if (VOBGame.DEBUG_AI)
                Gdx.app.log(LOG, "start...");
            // first, create the visibility matrix
            if (visibilityMatrix == null) {
                if (VOBGame.DEBUG_AI)
                    Gdx.app.log(LOG, "...create visibility matrix");
                visibilityMatrix = createVisibilityMatrix(controller.getMap(),
                        controller.getUnitsByPlayerId(getPlayerId()));
                return;
            }

            // next, create the value matrix and threat matrix
            if (valueMatrix == null) {
                Array<AbstractUnit> visibleUnits = new Array<AbstractUnit>();
                for (AbstractUnit unit : controller.getUnits()) {
                    if (visibilityMatrix[(int) (unit.getView().getBoardPosition().y
                            * controller.getMap().getWidth() + unit.getView().getBoardPosition().x)]) {
                        visibleUnits.add(unit);
                    }
                }
                if (VOBGame.DEBUG_AI)
                    Gdx.app.log(LOG, "...create value/threat matrices");
                valueMatrix = createUnitMatrix(controller.getMap(), visibleUnits, false);
                threatMatrix = createUnitMatrix(controller.getMap(), visibleUnits, true);
                return;
            }

            // create goalMatrix (based on knowledge of the map)
            if (goalMatrix == null) {
                if (VOBGame.DEBUG_AI)
                    Gdx.app.log(LOG, "...create goal matrix");
                goalMatrix = createGoalMatrix(controller.getMap(), controller.getUnits());

                if (VOBGame.DEBUG_AI) {
                    DecimalFormat formatter = new DecimalFormat("00.0");
                    for (int y = 0; y < controller.getMap().getHeight(); y++) {
                        if (y % 2 == 1) {
                            System.out.print("   ");
                        }
                        for (int x = 0; x < controller.getMap().getWidth(); x++) {
                            int i = y * controller.getMap().getWidth() + x;
                            float sum = (visibilityMatrix[i] ? goalMatrix[i] + valueMatrix[i] + threatMatrix[i]
                                    : 0);
                            System.out.print((sum >= 0 ? " " : "") + formatter.format(sum) + " ");
                        }
                        System.out.println("\n");
                    }
                }

                return;
            }

            // move units
            for (AbstractUnit unit : controller.getUnitsByPlayerId(getPlayerId())) {
                if (unit.getData().getMovesLeft() < 1)
                    continue;
                Vector2[] adjacent = HexMap.getAdjacent((int) unit.getView().getBoardPosition().x,
                        (int) unit.getView().getBoardPosition().y);
                Vector2 finalPos = new Vector2(unit.getView().getBoardPosition());
                float finalSum = goalMatrix[(int) finalPos.y * controller.getMap().getWidth()
                        + (int) finalPos.x]
                        + valueMatrix[(int) finalPos.y * controller.getMap().getWidth() + (int) finalPos.x]
                        + threatMatrix[(int) finalPos.y * controller.getMap().getWidth() + (int) finalPos.x];
                for (Vector2 v : adjacent) {
                    if (v.x < 0 || v.x >= controller.getMap().getWidth() || v.y < 0
                            || v.y >= controller.getMap().getHeight())
                        continue;
                    float curSum = goalMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x]
                            + valueMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x]
                            + threatMatrix[(int) v.y * controller.getMap().getWidth() + (int) v.x];
                    if (curSum > finalSum && controller.isBoardPosEmpty(v)) {
                        finalPos.set(v);
                        finalSum = curSum;
                    }
                }
                if (finalPos.x != unit.getView().getBoardPosition().x
                        || finalPos.y != unit.getView().getBoardPosition().y) {
                    // move!
                    Move mv = new Move();
                    mv.owner = getPlayerId();
                    mv.turn = controller.getGameTurn();
                    mv.unit = unit.getId();
                    mv.toLocation = finalPos;
                    netController.sendAICommand(mv);
                    valueMatrix = null;
                    threatMatrix = null;
                    return;
                }
            }

            // attack
            Array<AbstractUnit> visibleUnits = new Array<AbstractUnit>();
            Array<AbstractUnit> friendlyUnits = controller.getUnitsByPlayerId(this.getPlayerId());
            for (AbstractUnit unit : controller.getUnits()) {
                if (unit.getOwner() == this || unit.getOwner() == null)
                    continue;
                if (visibilityMatrix[(int) (unit.getView().getBoardPosition().y * controller.getMap().getWidth()
                        + unit.getView().getBoardPosition().x)] && !unit.getData().isInvisible()
                        || controller.getMap().detectorCanSee(this, friendlyUnits,
                                unit.getView().getBoardPosition())) {
                    visibleUnits.add(unit);
                }
            }
            for (AbstractUnit unit : controller.getUnitsByPlayerId(getPlayerId())) {
                if (unit.getData().getAttacksLeft() < 1)
                    continue;
                AbstractUnit toAttack = null;
                float toAttackThreat = 0;
                for (AbstractUnit o : visibleUnits) {
                    float thisThreat = threatMatrix[controller.getMap().getWidth()
                            * (int) o.getView().getBoardPosition().y + (int) o.getView().getBoardPosition().x];
                    if (controller.getUnitManager().canAttack(unit, o)
                            && (toAttack == null || thisThreat > toAttackThreat)) {
                        toAttack = o;
                        toAttackThreat = thisThreat;
                    }
                }
                if (toAttack != null) {
                    //                        Gdx.app.log(LOG + "$attack", String.format("%s --> %s", unit.getProtoId(), toAttack.getProtoId()));
                    Attack at = new Attack();
                    at.owner = getPlayerId();
                    at.turn = controller.getGameTurn();
                    at.unit = unit.getId();
                    at.target = toAttack.getId();
                    netController.sendAICommand(at);
                    valueMatrix = null;
                    threatMatrix = null;
                    return;
                }
            }

            // build units // TODO build other than marines (ie: implement chromosome 11)

            int positionToBuild = -1;
            for (int i = 0; i < valueMatrix.length; i++) {
                if (visibilityMatrix[i]) {
                    float sum = threatMatrix[i] + valueMatrix[i] + goalMatrix[i];

                    if ((positionToBuild == -1 || valueMatrix[positionToBuild] + valueMatrix[positionToBuild]
                            + goalMatrix[positionToBuild] < sum)) {
                        //                                Gdx.app.log(LOG, i + ":" + controller.isBoardPosEmpty(
                        //                                        i % controller.getMap().getWidth(),
                        //                                        i / controller.getMap().getWidth()));
                        if (controller.isBoardPosEmpty(i % controller.getMap().getWidth(),
                                i / controller.getMap().getWidth())) {
                            positionToBuild = i;
                        }
                    }
                }
            }
            Prototypes.JsonProto unitToBuild = null;

            if (positionToBuild >= 0) {
                Vector2 buildPosition = new Vector2(positionToBuild % controller.getMap().getWidth(),
                        positionToBuild / controller.getMap().getWidth());
                Array<String> protoIds = new Array<String>();
                HashMap<String, Float> buildScores = new HashMap<String, Float>();
                for (Prototypes.JsonProto proto : Prototypes.getAll(getRace())) {
                    protoIds.add(proto.id);
                    if (proto.cost > 0) {
                        buildScores.put(proto.id, 0f);
                    }
                }

                for (AbstractUnit unit : controller.getUnits()) {
                    if (unit.getOwner() == this || !visibilityMatrix[(int) (unit.getView().getBoardPosition().y
                            * controller.getMap().getWidth() + unit.getView().getBoardPosition().x)])
                        continue;

                    int unitIndex = protoIds.indexOf(unit.getProto().id, false);
                    int unitDistance = controller.getMap().getMapDist(unit.getView().getBoardPosition(),
                            buildPosition);
                    for (String key : buildScores.keySet()) {
                        buildScores.put(key, buildScores.get(key)
                                + ((Array<Float>) genome.chromosomes.get(10).genes.get(key)).get(unitIndex)
                                        / unitDistance);
                    }
                }

                String maxScore = null;
                float maxBuildScore = 0;
                while (unitToBuild == null && buildScores.keySet().size() > 0) {
                    for (String id : buildScores.keySet()) {
                        if (maxScore == null || buildScores.get(id) > buildScores.get(maxScore)) {
                            maxScore = id;
                            if (buildScores.get(id) > maxBuildScore) {
                                maxBuildScore = buildScores.get(id);
                            }
                        }
                    }

                    if (!maxScore.equals("saboteur") && buildScores.get(maxScore) > 0
                            && buildScores.get(maxScore) >= maxBuildScore
                                    - (Float) genome.chromosomes.get(10).genes.get("wait")
                            && getBankMoney() >= Prototypes.getProto(getRace(), maxScore).cost) {
                        unitToBuild = Prototypes.getProto(getRace(), maxScore);
                    } else {
                        buildScores.remove(maxScore);
                        maxScore = null;
                    }

                }
            }

            if (unitToBuild != null) {
                Build build = new Build();
                build.owner = getPlayerId();
                build.turn = controller.getGameTurn();
                build.building = unitToBuild.id;
                build.location = new Vector2(positionToBuild % controller.getMap().getWidth(),
                        positionToBuild / controller.getMap().getWidth());
                netController.sendAICommand(build);
                valueMatrix = null;
                return;
            }

            EndTurn et = new EndTurn();
            et.owner = getPlayerId();
            et.turn = controller.getGameTurn();
            netController.sendAICommand(et);
        }
    } else {
        visibilityMatrix = null;
        valueMatrix = null;
        threatMatrix = null;
        goalMatrix = null;
    }

}

From source file:com.ahsgaming.valleyofbones.map.HexMap.java

License:Apache License

public static int getMapDist(Vector2 from, Vector2 to) {
    // completion cases:
    if (from.x == to.x)
        return Math.round(Math.abs(from.y - to.y));
    if (from.y == to.y)
        return Math.round(Math.abs(from.x - to.x));

    // otherwise, move along the gradient
    from = new Vector2(from);
    float dx = to.x - from.x, dy = to.y - from.y;
    if (from.y % 2 == 0 && dx < 0) {
        from.x -= 1;//w ww .  jav  a  2s .  co  m
    } else if (from.y % 2 == 1 && dx > 0) {
        from.x += 1;
    }

    if (dy > 0) {
        from.y += 1;
    } else {
        from.y -= 1;
    }

    return 1 + getMapDist(from, to);
}

From source file:com.ahsgaming.valleyofbones.screens.AbstractScreen.java

License:Apache License

public static Vector2 localToGlobal(Vector2 local, Actor actor) {
    Vector2 coords = new Vector2(local);
    Actor cur = actor;//from  w w  w . jav a2 s . c om
    while (cur.getParent() != null) {
        coords.add(cur.getX(), cur.getY());
        cur = cur.getParent();
    }
    return coords;
}

From source file:com.anythingmachine.gdxwrapper.EarClipTriangulator.java

License:Apache License

private void cutEarTip(final int pEarTipIndex) {
    final int previousIndex = computePreviousIndex(pEarTipIndex);
    final int nextIndex = computeNextIndex(pEarTipIndex);

    triangles.add(new Vector2(vertices.get(previousIndex)));
    triangles.add(new Vector2(vertices.get(pEarTipIndex)));
    triangles.add(new Vector2(vertices.get(nextIndex)));

    vertices.remove(pEarTipIndex);//w w  w . ja  v a2  s. c  o  m
    System.arraycopy(vertexTypes, pEarTipIndex + 1, vertexTypes, pEarTipIndex, vertexCount - pEarTipIndex - 1);
    vertexCount--;
}

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

License:Apache License

@Override
public void startAnimation(String id, Tween.Type repeatType, int count, ActionCallback cb, Vector2 p0,
        Vector2 pf) {//from   w ww .j av  a 2  s.  c  om
    startAnimation(id, repeatType, count, null);

    if (currentAnimation != null) {
        Vector2 tmp = new Vector2(pf);
        float angle = tmp.sub(p0).angle() + 90;
        lookat(angle);
    }
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

    super.touchUp(event, x, y, pointer, button);
    //      EditorLogger.debug("Touch Up - X: " + x + " Y: " + y);

    if (draggingMode == DraggingModes.DRAGGING_ACTOR) {
        Ctx.project.getUndoStack().add(new UndoPosition(selActor, new Vector2(undoOrg)));
    } else if (draggingMode == DraggingModes.DRAGGING_WALKZONE) {
        Ctx.project.getUndoStack().add(new UndoWalkZonePosition(
                scnWidget.getScene().getPolygonalNavGraph().getWalkZone(), new Vector2(undoOrg)));
    }// www .ja  va 2s .c o m

    draggingMode = DraggingModes.NONE;

    return;
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public boolean keyUp(InputEvent event, int keycode) {
    switch (keycode) {
    case Keys.UP:
    case Keys.DOWN:
    case Keys.LEFT:
    case Keys.RIGHT:
        Ctx.project.getUndoStack().add(new UndoPosition(selActor, new Vector2(undoOrg)));
    }/*from   w  w w.  j a  v a  2s . co m*/

    return false;
}