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.entities.planet.Planet.java

License:Apache License

public void calcSpdAtPos(Vector2 spd, Vector2 pos) {
    Vector2 toPos = PtmMath.distVec(myPos, pos);
    float fromPlanetAngle = PtmMath.angle(toPos);
    float hSpdLen = PtmMath.angleToArc(myRotSpd, toPos.len());
    PtmMath.free(toPos);/*w  w w.  ja  va 2  s  . c  o m*/
    PtmMath.fromAl(spd, fromPlanetAngle + 90, hSpdLen);
    spd.add(mySpd);
}

From source file:com.tnf.ptm.entities.planet.PlanetManager.java

License:Apache License

private boolean recoverObj(PtmObject obj, float toNp, float npMinH) {
    if (npMinH < toNp) {
        return false;
    }//w  ww.  j  a  v a2  s  . c om
    if (!(obj instanceof PtmShip)) {
        return false;
    }
    PtmShip ship = (PtmShip) obj;
    Hull hull = ship.getHull();
    if (hull.config.getType() == HullConfig.Type.STATION) {
        return false;
    }
    float fh = myNearestPlanet.getFullHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 toShip = PtmMath.distVec(npPos, ship.getPosition());
    float len = toShip.len();
    if (len == 0) {
        toShip.set(0, fh);
    } else {
        toShip.scl(fh / len);
    }
    toShip.add(npPos);
    Body body = hull.getBody();
    body.setTransform(toShip, 0);
    body.setLinearVelocity(Vector2.Zero);
    PtmMath.free(toShip);
    return true;
}

From source file:com.tnf.ptm.entities.planet.PlanetObjectsBuilder.java

License:Apache License

private void addDeco0(PtmGame game, float groundHeight, Vector2 planetPos, Map<Vector2, List<Dra>> collector,
        DecoConfig dc) {//  ww  w  .ja  va2s.  c  o m
    World w = game.getObjMan().getWorld();
    ConsumedAngles consumed = new ConsumedAngles();

    final Vector2 rayCasted = new Vector2();
    RayCastCallback rcc = new RayCastCallback() {
        @Override
        public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
            if (!(fixture.getBody().getUserData() instanceof TileObject)) {
                return -1;
            }
            rayCasted.set(point);
            return fraction;
        }
    };

    int decoCount = (int) (2 * PtmMath.PI * groundHeight * dc.density);
    for (int i = 0; i < decoCount; i++) {
        float decoSz = PtmMath.rnd(dc.szMin, dc.szMax);
        float angularHalfWidth = PtmMath.angularWidthOfSphere(decoSz / 2, groundHeight);

        float decoAngle = 0;
        for (int j = 0; j < 5; j++) {
            decoAngle = PtmMath.rnd(180);
            if (!consumed.isConsumed(decoAngle, angularHalfWidth)) {
                consumed.add(decoAngle, angularHalfWidth);
                break;
            }
        }

        PtmMath.fromAl(rayCasted, decoAngle, groundHeight, true);
        rayCasted.add(planetPos);
        w.rayCast(rcc, rayCasted, planetPos);
        float decoDist = rayCasted.dst(planetPos);

        float baseAngle = PtmMath.windowCenter(decoAngle, DECO_PACK_ANGULAR_WIDTH);
        float baseDist = PtmMath.windowCenter(decoDist, DECO_PACK_SZ);
        Vector2 basePos = PtmMath.fromAl(baseAngle, baseDist).add(planetPos);
        Vector2 decoRelPos = new Vector2(rayCasted).sub(basePos);
        PtmMath.rotate(decoRelPos, -baseAngle - 90, true);
        float decoRelAngle = decoAngle - baseAngle;

        TextureAtlas.AtlasRegion decoTex = PtmMath.elemRnd(dc.texs);
        if (dc.allowFlip && PtmMath.test(.5f)) {
            decoTex = game.getTexMan().getFlipped(decoTex);
        }

        RectSprite s = new RectSprite(decoTex, decoSz, dc.orig.x, dc.orig.y, decoRelPos, DraLevel.DECO,
                decoRelAngle, 0, PtmColor.WHITE, false);
        List<Dra> ss = collector.get(basePos);
        if (ss == null) {
            ss = new ArrayList<Dra>();
            collector.put(new Vector2(basePos), ss);
        }
        ss.add(s);
        PtmMath.free(basePos);
    }
}

From source file:com.tnf.ptm.entities.planet.PlanetObjectsBuilder.java

License:Apache License

public FarShip buildOrbitEnemy(PtmGame game, Planet planet, float heightPerc, ShipConfig oe, float detDist) {
    float height = planet.getGroundHeight() + heightPerc * Const.ATM_HEIGHT;
    Vector2 pos = new Vector2();
    PtmMath.fromAl(pos, PtmMath.rnd(180), height);
    Vector2 planetPos = planet.getPos();
    pos.add(planetPos);
    float spdLen = PtmMath.sqrt(planet.getGravConst() / height);
    boolean cw = PtmMath.test(.5f);
    if (!cw) {/*w  w w. j  a v  a 2  s  .  c o m*/
        spdLen *= -1;
    }
    Vector2 spd = new Vector2(0, -spdLen);
    Vector2 v = PtmMath.distVec(pos, planetPos);
    PtmMath.rotate(spd, PtmMath.angle(v));
    PtmMath.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:com.tnf.ptm.entities.projectile.BallProjectileBody.java

License:Apache License

@Override
public float getDesiredAngle(PtmShip ne) {
    float spdLen = mySpd.len();
    if (spdLen < 3) {
        spdLen = 3;//from  ww w. java 2 s.com
    }
    float toNe = PtmMath.angle(myPos, ne.getPosition());
    Vector2 desiredSpd = PtmMath.fromAl(toNe, spdLen);
    desiredSpd.add(ne.getSpd());
    float res = PtmMath.angle(mySpd, desiredSpd);
    PtmMath.free(desiredSpd);
    return res;
}

From source file:com.tnf.ptm.entities.ShardBuilder.java

License:Apache License

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

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

    body.setAngularVelocity(PtmMath.rnd(MAX_ROT_SPD));
    Vector2 spd = PtmMath.fromAl(spdAngle, PtmMath.rnd(MAX_SPD));
    spd.add(baseSpd);//w w  w. j a  v a 2 s . com
    body.setLinearVelocity(spd);
    PtmMath.free(spd);

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

From source file:com.tnf.ptm.entities.ship.ForceBeacon.java

License:Apache License

public static PtmShip pullShips(PtmGame game, PtmObject owner, Vector2 ownPos, Vector2 ownSpd, Faction faction,
        float maxPullDist) {
    PtmShip res = null;/*  w  ww  . j av  a2  s .  c  o m*/
    float minLen = Float.MAX_VALUE;
    List<PtmObject> objs = game.getObjMan().getObjs();
    for (int i = 0, objsSize = objs.size(); i < objsSize; i++) {
        PtmObject o = objs.get(i);
        if (o == owner) {
            continue;
        }
        if (!(o instanceof PtmShip)) {
            continue;
        }
        PtmShip ship = (PtmShip) o;
        Pilot pilot = ship.getPilot();
        if (pilot.isUp() || pilot.isLeft() || pilot.isRight()) {
            continue;
        }
        if (game.getFactionMan().areEnemies(faction, pilot.getFaction())) {
            continue;
        }
        Vector2 toMe = PtmMath.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.getSoundManager().play(game, game.getSpecialSounds().forceBeaconWork, null, ship);
            if (toMeLen < minLen) {
                res = ship;
                minLen = toMeLen;
            }
        }
        PtmMath.free(toMe);
    }
    return res;
}

From source file:com.tnf.ptm.entities.ship.PtmShip.java

License:Apache License

private void throwLoot(PtmGame game, PtmItem item, boolean onDeath) {
    Vector2 lootSpd = new Vector2();
    float spdAngle;
    float spdLen;
    Vector2 pos = new Vector2();
    if (onDeath) {
        spdAngle = PtmMath.rnd(180);//from  w  w  w  .j a  v a  2s. c  om
        spdLen = PtmMath.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?
        PtmMath.fromAl(pos, spdAngle, PtmMath.rnd(myHull.config.getApproxRadius()));
    } else {
        spdAngle = getAngle();
        spdLen = 1f;
        PtmMath.fromAl(pos, spdAngle, myHull.config.getApproxRadius());
    }
    PtmMath.fromAl(lootSpd, spdAngle, spdLen);
    lootSpd.add(myHull.getSpd());
    pos.add(myHull.getPos());
    Loot l = game.getLootBuilder().build(game, pos, item, lootSpd, Loot.MAX_LIFE, PtmMath.rnd(Loot.MAX_ROT_SPD),
            this);
    game.getObjMan().addObjDelayed(l);
    if (!onDeath) {
        game.getSoundManager().play(game, game.getSpecialSounds().lootThrow, pos, this);
    }
}

From source file:com.tnf.ptm.entities.StarPort.java

License:Apache License

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

From source file:com.tnf.ptm.gfx.particle.PartMan.java

License:Apache License

public void blinks(Vector2 pos, PtmGame game, float sz) {
    int count = (int) (SZ_TO_BLINK_COUNT * sz * sz);
    for (int i = 0; i < count; i++) {
        Vector2 lightPos = new Vector2();
        PtmMath.fromAl(lightPos, PtmMath.rnd(180), PtmMath.rnd(0, sz / 2));
        lightPos.add(pos);
        float lightSz = PtmMath.rnd(.5f, 1) * EXPL_LIGHT_MAX_SZ;
        float fadeTime = PtmMath.rnd(.5f, 1) * EXPL_LIGHT_MAX_FADE_TIME;
        LightObject light = new LightObject(game, lightSz, true, 1, lightPos, fadeTime, game.getCols().fire);
        game.getObjMan().addObjDelayed(light);
    }/*w w w  .  j a  v  a2s. c  om*/
}