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

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

Introduction

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

Prototype

@Override
    public float len() 

Source Link

Usage

From source file:com.tnf.ptm.entities.item.Loot.java

License:Apache License

public void maybePulled(PtmShip ship, Vector2 pullerPos, float radius) {
    if (ship == myOwner) {
        return;//  ww w . j  av a 2s .  co  m
    }
    Vector2 toPuller = PtmMath.getVec(pullerPos);
    toPuller.sub(getPosition());
    float pullerDist = toPuller.len();
    if (0 < pullerDist && pullerDist < radius) {
        toPuller.scl(PULL_DESIRED_SPD / pullerDist);
        Vector2 spd = myBody.getLinearVelocity();
        Vector2 spdDiff = PtmMath.distVec(spd, toPuller);
        float spdDiffLen = spdDiff.len();
        if (spdDiffLen > 0) {
            spdDiff.scl(PULL_FORCE / spdDiffLen);
            myBody.applyForceToCenter(spdDiff, true);
        }
        PtmMath.free(spdDiff);
    }
    PtmMath.free(toPuller);
}

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

License:Apache License

@Bound
public Vector2 getAdjustedEffectSpd(Vector2 pos, Vector2 spd) {
    Vector2 r = PtmMath.getVec(spd);
    if (myConfig.skyConfig == null) {
        return r;
    }//from www  . jav a  2s. c om
    Vector2 up = PtmMath.distVec(myPos, pos);
    float dst = up.len();
    if (dst == 0 || getFullHeight() < dst) {
        PtmMath.free(up);
        return r;
    }
    float smokeConst = 1.2f * myGravConst;
    if (dst < myGroundHeight) {
        up.scl(smokeConst / dst / myGroundHeight / myGroundHeight);
        r.set(up);
        PtmMath.free(up);
        return r;
    }
    float spdPerc = (dst - myGroundHeight) / Const.ATM_HEIGHT;
    r.scl(spdPerc);
    up.scl(smokeConst / dst / dst / dst);
    r.add(up);
    PtmMath.free(up);
    return r;
}

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);/*from w w  w.  java 2  s. com*/
    PtmMath.fromAl(spd, fromPlanetAngle + 90, hSpdLen);
    spd.add(mySpd);
}

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

License:Apache License

private void applyGrav(PtmGame game, PtmSystem nearestSys) {
    float npGh = myNearestPlanet.getGroundHeight();
    float npFh = myNearestPlanet.getFullHeight();
    float npMinH = myNearestPlanet.getMinGroundHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 sysPos = nearestSys.getPos();
    float npGravConst = myNearestPlanet.getGravConst();

    List<PtmObject> objs = game.getObjMan().getObjs();
    for (PtmObject obj : objs) {
        if (!obj.receivesGravity()) {
            continue;
        }//ww  w. jav a 2  s.com

        Vector2 objPos = obj.getPosition();
        float minDist;
        Vector2 srcPos;
        float gravConst;
        boolean onPlanet;
        float toNp = npPos.dst(objPos);
        float toSys = sysPos.dst(objPos);
        if (toNp < npFh) {
            if (recoverObj(obj, toNp, npMinH)) {
                continue;
            }
            minDist = npGh;
            srcPos = npPos;
            gravConst = npGravConst;
            onPlanet = true;
        } else if (toSys < Const.SUN_RADIUS) {
            minDist = SunSingleton.SUN_HOT_RAD;
            srcPos = sysPos;
            gravConst = SunSingleton.GRAV_CONST;
            onPlanet = false;
        } else {
            continue;
        }

        Vector2 grav = PtmMath.getVec(srcPos);
        grav.sub(objPos);
        float len = grav.len();
        grav.nor();
        if (len < minDist) {
            len = minDist;
        }
        float g = gravConst / len / len;
        grav.scl(g);
        obj.receiveForce(grav, game, true);
        PtmMath.free(grav);
        if (!onPlanet) {
            mySunSingleton.doDmg(game, obj, toSys);
        }
    }

}

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  w  w .ja  v  a  2s. c o  m
    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

public FarShip buildGroundShip(PtmGame game, Planet planet, ShipConfig ge, TradeConfig tc, Faction faction,
        ConsumedAngles takenAngles, String mapHint) {
    Vector2 pos = game.getPlanetMan().findFlatPlace(game, planet, takenAngles, ge.hull.getApproxRadius());
    boolean station = ge.hull.getType() == HullConfig.Type.STATION;
    String ic = ge.items;/*w w w .  jav a2 s .  c  om*/
    boolean hasRepairer;
    hasRepairer = faction == Faction.LAANI;
    int money = ge.money;
    float height = pos.len();
    float aboveGround;
    if (station) {
        aboveGround = ge.hull.getSize() * .75f - ge.hull.getOrigin().y;
    } else {
        aboveGround = ge.hull.getSize();
    }
    pos.scl((height + aboveGround) / height);
    PtmMath.toWorld(pos, pos, planet.getAngle(), planet.getPos(), false);

    Vector2 toPlanet = PtmMath.getVec(planet.getPos()).sub(pos);
    float angle = PtmMath.angle(toPlanet) - 180;
    if (station) {
        angle += 90;
    }
    Vector2 spd = new Vector2(toPlanet).nor();
    PtmMath.free(toPlanet);

    Pilot provider = new AiPilot(new StillGuard(pos, game, ge), false, faction, true, mapHint,
            Const.AI_DET_DIST);

    return game.getShipBuilder().buildNewFar(game, pos, spd, angle, 0, provider, ic, ge.hull, null, hasRepairer,
            money, tc, true);
}

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

License:Apache License

public void draw(PtmGame game, GameDrawer drawer) {
    Vector2 camPos = game.getCam().getPos();
    PtmSystem sys = game.getPlanetMan().getNearestSystem(camPos);
    Vector2 toCam = PtmMath.getVec(camPos);
    toCam.sub(sys.getPos());// w  w w .  j a  va 2  s . c  o  m
    float toCamLen = toCam.len();
    if (toCamLen < Const.SUN_RADIUS) {
        float closeness = 1 - toCamLen / Const.SUN_RADIUS;
        myGradTint.a = PtmMath.clamp(closeness * 4, 0, 1);
        myFillTint.a = PtmMath.clamp((closeness - .25f) * 4, 0, 1);

        float sz = 2 * game.getCam().getViewDist();
        float gradAngle = PtmMath.angle(toCam) + 90;
        drawer.draw(myWhiteTex, sz * 2, sz * 2, sz, sz, camPos.x, camPos.y, 0, myFillTint);
        drawer.draw(myGradTex, sz * 2, sz * 2, sz, sz, camPos.x, camPos.y, gradAngle, myGradTint);
    }
    PtmMath.free(toCam);
}

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;/*from  w  w w  . ja  v a 2  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.handler.input.AiPilot.java

License:Apache License

@Override
public void update(PtmGame game, PtmShip ship, PtmShip nearestEnemy) {
    myAbilityUpdater.update(ship, nearestEnemy);
    myPlanetBind = null;// w ww . ja  v  a2  s  .c  o m
    Vector2 shipPos = ship.getPosition();
    HullConfig hullConfig = ship.getHull().config;
    float maxIdleDist = getMaxIdleDist(hullConfig);
    myDestProvider.update(game, shipPos, maxIdleDist, hullConfig, nearestEnemy);

    Boolean canShoot = canShoot0(ship);
    boolean canShootUnfixed = canShoot == null;
    if (canShootUnfixed) {
        canShoot = true;
    }
    Planet np = game.getPlanetMan().getNearestPlanet();
    boolean nearGround = np.isNearGround(shipPos);

    Vector2 dest = null;
    Vector2 destSpd = null;
    boolean shouldStopNearDest = false;
    boolean avoidBigObjs = false;
    float desiredSpdLen = myDestProvider.getDesiredSpdLen();
    boolean hasEngine = ship.getHull().getEngine() != null;
    if (hasEngine) {
        Boolean battle = null;
        if (nearestEnemy != null) {
            battle = myDestProvider.shouldManeuver(canShoot, nearestEnemy, nearGround);
        }
        if (battle != null) {
            dest = myBattleDestProvider.getDest(ship, nearestEnemy, np, battle, game.getTimeStep(),
                    canShootUnfixed, nearGround);
            shouldStopNearDest = myBattleDestProvider.shouldStopNearDest();
            destSpd = nearestEnemy.getSpd();
            boolean big = hullConfig.getType() == HullConfig.Type.BIG;
            float maxBattleSpd = nearGround ? MAX_GROUND_BATTLE_SPD : big ? MAX_BATTLE_SPD_BIG : MAX_BATTLE_SPD;
            if (maxBattleSpd < desiredSpdLen) {
                desiredSpdLen = maxBattleSpd;
            }
            if (!big) {
                desiredSpdLen += destSpd.len();
            }
        } else {
            dest = myDestProvider.getDest();
            destSpd = myDestProvider.getDestSpd();
            shouldStopNearDest = myDestProvider.shouldStopNearDest();
            avoidBigObjs = myDestProvider.shouldAvoidBigObjs();
        }
    }

    myMover.update(game, ship, dest, np, maxIdleDist, hasEngine, avoidBigObjs, desiredSpdLen,
            shouldStopNearDest, destSpd);
    boolean moverActive = myMover.isActive();

    Vector2 enemyPos = nearestEnemy == null ? null : nearestEnemy.getPosition();
    Vector2 enemySpd = nearestEnemy == null ? null : nearestEnemy.getSpd();
    float enemyApproxRad = nearestEnemy == null ? 0 : nearestEnemy.getHull().config.getApproxRadius();
    myShooter.update(ship, enemyPos, moverActive, canShoot, enemySpd, enemyApproxRad);
    if (hasEngine && !moverActive && !isShooterRotated()) {
        myMover.rotateOnIdle(ship, np, dest, shouldStopNearDest, maxIdleDist);
    }

    if (myReEquipAwait <= 0) {
        myReEquipAwait = MAX_RE_EQUIP_AWAIT;
    } else {
        myReEquipAwait -= game.getTimeStep();
    }
}

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  ava 2 s.c o m
            } 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);
}