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:org.destinationsol.game.planet.PlanetObjectsBuilder.java

License:Apache License

public FarShip buildOrbitEnemy(SolGame game, Planet planet, float heightPerc, ShipConfig oe, float detDist) {
    float height = planet.getGroundHeight() + heightPerc * Const.ATM_HEIGHT;
    Vector2 pos = new Vector2();
    SolMath.fromAl(pos, SolMath.rnd(180), height);
    Vector2 planetPos = planet.getPos();
    pos.add(planetPos);
    float spdLen = SolMath.sqrt(planet.getGravConst() / height);
    boolean cw = SolMath.test(.5f);
    if (!cw)//from   w w  w  . ja  v  a  2  s .  co m
        spdLen *= -1;
    Vector2 spd = new Vector2(0, -spdLen);
    Vector2 v = SolMath.distVec(pos, planetPos);
    SolMath.rotate(spd, SolMath.angle(v));
    SolMath.free(v);

    OrbiterDestProvider dp = new OrbiterDestProvider(planet, height, cw);
    Pilot provider = new AiPilot(dp, false, Faction.EHAR, true, null, detDist);

    int money = oe.money;

    return game.getShipBuilder().buildNewFar(game, pos, spd, 0, 0, provider, oe.items, oe.hull, null, false,
            money, null, true);
}

From source file:org.destinationsol.game.projectile.BallProjectileBody.java

License:Apache License

@Override
public float getDesiredAngle(SolShip ne) {
    float spdLen = mySpd.len();
    if (spdLen < 3)
        spdLen = 3;/*from w w w.  j  av a2 s  .  co  m*/
    float toNe = SolMath.angle(myPos, ne.getPosition());
    Vector2 desiredSpd = SolMath.fromAl(toNe, spdLen);
    desiredSpd.add(ne.getSpd());
    float res = SolMath.angle(mySpd, desiredSpd);
    SolMath.free(desiredSpd);
    return res;
}

From source file:org.destinationsol.game.screens.CollisionWarnDrawer.java

License:Apache License

public boolean shouldWarn(SolGame game) {
    myHero = game.getHero();//from   www.jav  a  2s . c  o m
    if (myHero == null)
        return false;
    Vector2 pos = myHero.getPosition();
    Vector2 spd = myHero.getSpd();
    float acc = myHero.getAcc();
    float spdLen = spd.len();
    float spdAngle = SolMath.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 = SolMath.getVec(0, 0);
    SolMath.fromAl(finalPos, spdAngle, breakWay);
    finalPos.add(pos);
    myWarnCallback.show = false;
    game.getObjMan().getWorld().rayCast(myWarnCallback, pos, finalPos);
    SolMath.free(finalPos);
    return myWarnCallback.show;
}

From source file:org.destinationsol.game.screens.HireShips.java

License:Apache License

private Vector2 getPos(SolGame game, SolShip 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 = SolMath.angle(np.getPos(), heroPos);
    for (int i = 0; i < 50; i++) {
        float relAngle;
        if (nearGround) {
            relAngle = fromPlanet;/*from  w  ww.  jav  a 2 s .  co m*/
        } else {
            relAngle = SolMath.rnd(180);
        }
        SolMath.fromAl(pos, relAngle, dist);
        pos.add(heroPos);
        if (game.isPlaceEmpty(pos, false))
            return pos;
        dist += Guardian.DIST;
    }
    return null;
}

From source file:org.destinationsol.game.ShardBuilder.java

License:Apache License

public Shard build(SolGame game, Vector2 basePos, Vector2 baseSpd, float size) {

    ArrayList<Dra> dras = new ArrayList<Dra>();
    float scale = SolMath.rnd(MIN_SCALE, MAX_SCALE);
    TextureAtlas.AtlasRegion tex = SolMath.elemRnd(myTexs);
    float spdAngle = SolMath.rnd(180);
    Vector2 pos = new Vector2();
    SolMath.fromAl(pos, spdAngle, SolMath.rnd(size));
    pos.add(basePos);
    Body body = myPathLoader.getBodyAndSprite(game, "smallGameObjs",
            AsteroidBuilder.removePath(tex.name) + "_" + tex.index, scale, BodyDef.BodyType.DynamicBody, pos,
            SolMath.rnd(180), dras, ShipBuilder.SHIP_DENSITY, DraLevel.PROJECTILES, tex);

    body.setAngularVelocity(SolMath.rnd(MAX_ROT_SPD));
    Vector2 spd = SolMath.fromAl(spdAngle, SolMath.rnd(MAX_SPD));
    spd.add(baseSpd);//w  w  w.j  a v a2 s.c  o m
    body.setLinearVelocity(spd);
    SolMath.free(spd);

    Shard shard = new Shard(body, dras);
    body.setUserData(shard);
    return shard;
}

From source file:org.destinationsol.game.ship.ForceBeacon.java

License:Apache License

public static SolShip pullShips(SolGame game, SolObject owner, Vector2 ownPos, Vector2 ownSpd, Faction faction,
        float maxPullDist) {
    SolShip res = null;//from  w  w w .  ja va  2  s .  c  om
    float minLen = Float.MAX_VALUE;
    List<SolObject> objs = game.getObjMan().getObjs();
    for (int i = 0, objsSize = objs.size(); i < objsSize; i++) {
        SolObject o = objs.get(i);
        if (o == owner)
            continue;
        if (!(o instanceof SolShip))
            continue;
        SolShip ship = (SolShip) o;
        Pilot pilot = ship.getPilot();
        if (pilot.isUp() || pilot.isLeft() || pilot.isRight())
            continue;
        if (game.getFactionMan().areEnemies(faction, pilot.getFaction()))
            continue;
        Vector2 toMe = SolMath.distVec(ship.getPosition(), ownPos);
        float toMeLen = toMe.len();
        if (toMeLen < maxPullDist) {
            if (toMeLen > 1)
                toMe.scl(1 / toMeLen);
            if (ownSpd != null)
                toMe.add(ownSpd);
            ship.getHull().getBody().setLinearVelocity(toMe);
            game.getSoundMan().play(game, game.getSpecialSounds().forceBeaconWork, null, ship);
            if (toMeLen < minLen) {
                res = ship;
                minLen = toMeLen;
            }
        }
        SolMath.free(toMe);
    }
    return res;
}

From source file:org.destinationsol.game.ship.SolShip.java

License:Apache License

private void throwLoot(SolGame game, SolItem item, boolean onDeath) {
    Vector2 lootSpd = new Vector2();
    float spdAngle;
    float spdLen;
    Vector2 pos = new Vector2();
    if (onDeath) {
        spdAngle = SolMath.rnd(180);/*from w w w  .  jav a 2 s  .  c  om*/
        spdLen = SolMath.rnd(0, Loot.MAX_SPD);
        // TODO: This statement previously caused a crash as getApproxRadius returned 0 - where is it meant to be set / loaded from?
        SolMath.fromAl(pos, spdAngle, SolMath.rnd(myHull.config.getApproxRadius()));
    } else {
        spdAngle = getAngle();
        spdLen = 1f;
        SolMath.fromAl(pos, spdAngle, myHull.config.getApproxRadius());
    }
    SolMath.fromAl(lootSpd, spdAngle, spdLen);
    lootSpd.add(myHull.getSpd());
    pos.add(myHull.getPos());
    Loot l = game.getLootBuilder().build(game, pos, item, lootSpd, Loot.MAX_LIFE, SolMath.rnd(Loot.MAX_ROT_SPD),
            this);
    game.getObjMan().addObjDelayed(l);
    if (!onDeath) {
        game.getSoundMan().play(game, game.getSpecialSounds().lootThrow, pos, this);
    }
}

From source file:org.destinationsol.game.SolCam.java

License:Apache License

public void update(SolGame game) {

    float desiredVd = Const.CAM_VIEW_DIST_GROUND;
    float life = 0;

    SolShip hero = game.getHero();//from  w  ww .j  av  a  2s  .  com
    float ts = game.getTimeStep();
    if (hero == null) {
        StarPort.Transcendent trans = game.getTranscendentHero();
        if (trans == null) {
            if (DebugOptions.DIRECT_CAM_CONTROL) {
                applyInput(game);
            }
        } else {
            desiredVd = Const.CAM_VIEW_DIST_SPACE;
            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 = SolMath.getVec(hero.getSpd());
            moveDiff.scl(ts);
            myPos.add(moveDiff);
            SolMath.free(moveDiff);
            float moveSpd = MOVE_SPD * ts;
            myPos.x = SolMath.approach(myPos.x, heroPos.x, moveSpd);
            myPos.y = SolMath.approach(myPos.y, heroPos.y, moveSpd);
        }
        life = hero.getLife();

        float spd = hero.getSpd().len();

        desiredVd = Const.CAM_VIEW_DIST_SPACE;
        Planet np = game.getPlanetMan().getNearestPlanet(myPos);
        if (np.getFullHeight() < np.getPos().dst(myPos) && MAX_ZOOM_SPD < spd) {
            desiredVd = Const.CAM_VIEW_DIST_JOURNEY;
        } else if (np.isNearGround(myPos) && spd < MED_ZOOM_SPD) {
            desiredVd = Const.CAM_VIEW_DIST_GROUND;
        }
        desiredVd += hero.getHull().config.getApproxRadius();
    }

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

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

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

    float desiredZoom = calcZoom(desiredVd);
    myZoom = SolMath.approach(myZoom, desiredZoom, ZOOM_CHG_SPD * ts);
    applyZoom(game.getMapDrawer());
    myCam.update();
}

From source file:org.destinationsol.game.SolCam.java

License:Apache License

public void drawDebug(GameDrawer drawer) {
    float hOver2 = VIEWPORT_HEIGHT * myZoom / 2;
    float wOver2 = hOver2 * drawer.r;
    Vector2 dr = SolMath.getVec(wOver2, hOver2);
    SolMath.rotate(dr, myAngle);//ww w . ja v a  2s. c o m
    Vector2 dl = SolMath.getVec(-wOver2, hOver2);
    SolMath.rotate(dl, myAngle);
    Vector2 ul = SolMath.getVec(dr);
    ul.scl(-1);
    Vector2 ur = SolMath.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, SolColor.W, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, dl, ul, SolColor.W, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, ul, ur, SolColor.W, lw, false);
    drawer.drawLine(drawer.debugWhiteTex, ur, dr, SolColor.W, lw, false);

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

From source file:org.destinationsol.game.StarPort.java

License:Apache License

@Bound
public static Vector2 getDesiredPos(Planet from, Planet to, boolean percise) {
    Vector2 fromPos = from.getPos();
    float angle = SolMath.angle(fromPos, to.getPos(), percise);
    Vector2 pos = SolMath.getVec();
    SolMath.fromAl(pos, angle, from.getFullHeight() + DIST_FROM_PLANET);
    pos.add(fromPos);
    return pos;//from  w  w w. jav a  2s  .com
}