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

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

Introduction

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

Prototype

@Override
    public Vector2 add(Vector2 v) 

Source Link

Usage

From source file:com.tnf.ptm.gfx.PtmCam.java

License:Apache License

public void update(PtmGame game) {
    float life = 0;

    PtmShip hero = game.getHero();/*from  w  ww .ja v a  2  s  .c om*/
    float ts = game.getTimeStep();
    if (hero == null) {
        StarPort.Transcendent trans = game.getTranscendentHero();
        if (trans == null) {
            if (DebugOptions.DIRECT_CAM_CONTROL) {
                applyInput(game);
            }
        } else {
            myPos.set(trans.getPosition());
        }
    } else {
        Vector2 heroPos = hero.getHull().getBody().getWorldCenter();
        if (myZoom * VIEWPORT_HEIGHT < heroPos.dst(myPos)) {
            myPos.set(heroPos);
            game.getObjMan().resetDelays();
        } else {
            Vector2 moveDiff = PtmMath.getVec(hero.getSpd());
            moveDiff.scl(ts);
            myPos.add(moveDiff);
            PtmMath.free(moveDiff);
            float moveSpd = MOVE_SPD * ts;
            myPos.x = PtmMath.approach(myPos.x, heroPos.x, moveSpd);
            myPos.y = PtmMath.approach(myPos.y, heroPos.y, moveSpd);
        }
        life = hero.getLife();
    }

    if (life < myPrevHeroLife) {
        float shakeDiff = .1f * MAX_SHAKE * (myPrevHeroLife - life);
        myShake = PtmMath.approach(myShake, MAX_SHAKE, shakeDiff);
    } else {
        myShake = PtmMath.approach(myShake, 0, SHAKE_DAMP * ts);
    }
    myPrevHeroLife = life;

    Vector2 pos = PtmMath.fromAl(PtmMath.rnd(180), myShake);
    pos.add(myPos);
    applyPos(pos.x, pos.y);
    PtmMath.free(pos);

    float desiredAngle = myCamRotStrategy.getRotation(myPos, game);
    float rotSpd = CAM_ROT_SPD * ts;
    myAngle = PtmMath.approachAngle(myAngle, desiredAngle, rotSpd);
    applyAngle();

    updateMap(game);
}

From source file:com.tnf.ptm.gfx.PtmCam.java

License:Apache License

public void drawDebug(GameDrawer drawer) {
    float hOver2 = VIEWPORT_HEIGHT * myZoom / 2;
    float wOver2 = hOver2 * drawer.r;
    Vector2 dr = PtmMath.getVec(wOver2, hOver2);
    PtmMath.rotate(dr, myAngle);/*from  www  .ja v  a  2  s .c o m*/
    Vector2 dl = PtmMath.getVec(-wOver2, hOver2);
    PtmMath.rotate(dl, myAngle);
    Vector2 ul = PtmMath.getVec(dr);
    ul.scl(-1);
    Vector2 ur = PtmMath.getVec(dl);
    ur.scl(-1);
    dr.add(myPos);
    dl.add(myPos);
    ul.add(myPos);
    ur.add(myPos);

    float lw = getRealLineWidth();
    drawer.drawLine(drawer.debugWhiteTex, dr, dl, PtmColor.WHITE, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, dl, ul, PtmColor.WHITE, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, ul, ur, PtmColor.WHITE, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, ur, dr, PtmColor.WHITE, lw, false);

    PtmMath.free(dr);
    PtmMath.free(dl);
    PtmMath.free(ul);
    PtmMath.free(ur);
}

From source file:com.tnf.ptm.handler.BeaconHandler.java

License:Apache License

private void maybeUpdatePlanetPos(PtmGame game) {
    Vector2 beaconPos = getPos0();
    if (myPlanetBind == null) {
        myPlanetBind = PlanetBind.tryBind(game, beaconPos, 0);
        return;/* w  ww  .  j  a  v  a  2  s . com*/
    }
    Vector2 vec = PtmMath.getVec();
    myPlanetBind.setDiff(vec, beaconPos, false);
    beaconPos.add(vec);
    PtmMath.free(vec);
    myPlanetBind.getPlanet().calcSpdAtPos(mySpd, beaconPos);
}

From source file:com.tnf.ptm.handler.input.AiPilot.java

License:Apache License

@Override
public void updateFar(PtmGame game, FarShip farShip) {
    Vector2 shipPos = farShip.getPos();
    HullConfig hullConfig = farShip.getHullConfig();
    float maxIdleDist = getMaxIdleDist(hullConfig);
    myDestProvider.update(game, shipPos, maxIdleDist, hullConfig, null);
    Vector2 dest = myDestProvider.getDest();

    Vector2 spd = farShip.getSpd();
    float angle = farShip.getAngle();
    Engine engine = farShip.getEngine();
    float ts = game.getTimeStep();
    if (dest == null || engine == null) {
        if (myPlanetBind == null) {
            if (myBindAwait > 0) {
                myBindAwait -= ts;//from w  w  w  .j a v  a 2 s.  c  om
            } else {
                myPlanetBind = PlanetBind.tryBind(game, shipPos, angle);
                myBindAwait = MAX_BIND_AWAIT;
            }
        }
        if (myPlanetBind != null) {
            myPlanetBind.setDiff(spd, shipPos, false);
            spd.scl(1 / ts);
            angle = myPlanetBind.getDesiredAngle();
        }
    } else {
        float toDestLen = shipPos.dst(dest);
        float desiredAngle;
        float maxIdleDistHack = .05f; // to avoid StillGuards from getting stuck inside ground
        if (myDestProvider.shouldStopNearDest() && toDestLen < maxIdleDistHack) {
            spd.set(myDestProvider.getDestSpd());
            desiredAngle = angle; // can be improved
        } else {
            desiredAngle = PtmMath.angle(shipPos, dest);
            if (myDestProvider.shouldAvoidBigObjs()) {
                desiredAngle = myMover.getBigObjAvoider().avoid(game, shipPos, dest, desiredAngle);
            }
            float desiredSpdLen = myDestProvider.getDesiredSpdLen();
            float spdLenDiff = engine.getAcc() * ts;
            float spdLen = PtmMath.approach(spd.len(), desiredSpdLen, spdLenDiff);
            if (toDestLen < spdLen) {
                spdLen = toDestLen;
            }
            PtmMath.fromAl(spd, desiredAngle, spdLen);
        }
        angle = PtmMath.approachAngle(angle, desiredAngle, engine.getMaxRotSpd() * ts);
    }

    farShip.setSpd(spd);
    farShip.setAngle(angle);

    Vector2 newPos = PtmMath.getVec(spd);
    newPos.scl(ts);
    newPos.add(shipPos);
    farShip.setPos(newPos);
    PtmMath.free(newPos);
}

From source file:com.tnf.ptm.screens.game.CollisionWarnDrawer.java

License:Apache License

public boolean shouldWarn(PtmGame game) {
    hero = game.getHero();/*w ww .jav  a 2s  .  c om*/
    if (hero == null) {
        return false;
    }
    Vector2 pos = hero.getPosition();
    Vector2 spd = hero.getSpd();
    float acc = hero.getAcc();
    float spdLen = spd.len();
    float spdAngle = PtmMath.angle(spd);
    if (acc <= 0 || spdLen < 2 * acc) {
        return false;
    }
    // t = v/a;
    // s = att/2 = vv/a/2;
    float breakWay = spdLen * spdLen / acc / 2;
    breakWay += 2 * spdLen;
    Vector2 finalPos = PtmMath.getVec(0, 0);
    PtmMath.fromAl(finalPos, spdAngle, breakWay);
    finalPos.add(pos);
    warnCallback.show = false;
    game.getObjMan().getWorld().rayCast(warnCallback, pos, finalPos);
    PtmMath.free(finalPos);
    return warnCallback.show;
}

From source file:com.tnf.ptm.screens.game.HireShips.java

License:Apache License

private Vector2 getPos(PtmGame game, PtmShip hero, HullConfig hull) {
    Vector2 pos = new Vector2();
    float dist = hero.getHull().config.getApproxRadius() + Guardian.DIST + hull.getApproxRadius();
    Vector2 heroPos = hero.getPosition();
    Planet np = game.getPlanetMan().getNearestPlanet();
    boolean nearGround = np.isNearGround(heroPos);
    float fromPlanet = PtmMath.angle(np.getPos(), heroPos);
    for (int i = 0; i < 50; i++) {
        float relAngle;
        if (nearGround) {
            relAngle = fromPlanet;/* ww w.  java  2  s.  com*/
        } else {
            relAngle = PtmMath.rnd(180);
        }
        PtmMath.fromAl(pos, relAngle, dist);
        pos.add(heroPos);
        if (game.isPlaceEmpty(pos, false)) {
            return pos;
        }
        dist += Guardian.DIST;
    }
    return null;
}

From source file:com.ukos.logics.Point.java

public Point add(Point v) {
    Vector2 aux = point.cpy();
    return new Point(aux.add(v.point));
}

From source file:edu.lehigh.cse.lol.Hero.java

License:Open Source License

/**
 * Make the hero jump, unless it is in the air and not multijump
 *///from   w  w  w  . j  ava 2  s  .  co m
void jump() {
    // nb: multijump prevents us from ever setting mInAir, so this is safe:
    if (mInAir)
        return;
    Vector2 v = mBody.getLinearVelocity();
    v.add(mJumpImpulses);
    updateVelocity(v.x, v.y);
    if (!mAllowMultiJump)
        mInAir = true;
    if (mJumpAnimation != null)
        mAnimator.setCurrentAnimation(mJumpAnimation);
    if (mJumpSound != null)
        mJumpSound.play(Facts.getGameFact("volume", 1));
    // break any sticky joints, so the hero can actually move
    mStickyDelay = System.currentTimeMillis() + 10;
}

From source file:es.eucm.ead.editor.view.widgets.groupeditor.Handles.java

License:Open Source License

/**
 * Updates the handles scale to keep them at the same stage-relative size
 *///from  w w w.j  av  a 2 s  .co m
public void updateHandlesScale() {
    for (Handle handle : handles) {
        handle.stageToLocalCoordinates(tmp2.set(0, 0));
        if (handle instanceof OriginHandle || handle instanceof RotationHandle) {
            handle.stageToLocalCoordinates(tmp3.set(handleCircleSize, handleCircleSize));
        } else {
            handle.stageToLocalCoordinates(tmp3.set(handleSquareSize, handleSquareSize));
        }
        tmp3.sub(tmp2);
        handle.setRadius(tmp3.len());
    }

    // Set rotation handle
    Vector2 o = localToStageCoordinates(tmp1.set(handles[0].getX(), handles[0].getY()));
    Vector2 n = localToStageCoordinates(tmp2.set(handles[6].getX(), handles[6].getY())).sub(o).nor();

    Vector2 top = localToStageCoordinates(tmp3.set(handles[7].getX(), handles[7].getY()));

    top.add(n.scl(rotationHandleOffset));

    stageToLocalCoordinates(top);
    handles[ROTATION_HANDLE_INDEX].setX(top.x);
    handles[ROTATION_HANDLE_INDEX].setY(top.y);
}

From source file:es.eucm.ead.engine.collision.CircleWrapper.java

License:Open Source License

@Override
protected boolean intersectToSegment(Vector2 start, Vector2 end, Vector2 intersection) {
    Vector2 v = Pools.obtain(Vector2.class);
    if (circle.contains(start) && !circle.contains(end)) {
        v.set(end);/* ww w.j  a va 2 s  .  com*/
        v.sub(start);
    } else if (!circle.contains(start) && circle.contains(end)) {
        v.set(start);
        v.sub(end);
    } else {
        return false;
    }

    v.scl(circle.radius / v.len());

    getCenter(intersection);
    intersection.add(v);
    Pools.free(v);
    return true;
}