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

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

Introduction

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

Prototype

@Override
    public float dst(Vector2 v) 

Source Link

Usage

From source file:org.destinationsol.game.input.BeaconDestProvider.java

License:Apache License

@Override
public void update(SolGame game, Vector2 shipPos, float maxIdleDist, HullConfig hullConfig,
        SolShip nearestEnemy) {//from  w  ww  .j  a v a2s  .c om
    BeaconHandler bh = game.getBeaconHandler();
    myDest.set(bh.getPos());
    myShouldManeuver = null;
    BeaconHandler.Action a = bh.getCurrAction();
    if (nearestEnemy != null && a == BeaconHandler.Action.ATTACK) {
        if (shipPos.dst(myDest) < shipPos.dst(nearestEnemy.getPosition()) + .1f)
            myShouldManeuver = true;
    }
    myShouldStopNearDest = STOP_AWAIT < game.getTime() - bh.getClickTime();
    myDestSpd.set(bh.getSpd());
}

From source file:org.destinationsol.game.input.BigObjAvoider.java

License:Apache License

public float avoid(SolGame game, Vector2 from, Vector2 dest, float toDestAngle) {
    float toDestLen = from.dst(dest);
    if (toDestLen > MAX_DIST_LEN)
        toDestLen = MAX_DIST_LEN;/* w  ww .j  a v  a  2  s.com*/
    float res = toDestAngle;
    Planet p = game.getPlanetMan().getNearestPlanet(from);
    Vector2 pPos = p.getPos();
    float pRad = p.getFullHeight();
    if (dest.dst(pPos) < pRad)
        pRad = p.getGroundHeight();
    myProj.set(pPos);
    myProj.sub(from);
    SolMath.rotate(myProj, -toDestAngle);
    if (0 < myProj.x && myProj.x < toDestLen) {
        if (SolMath.abs(myProj.y) < pRad) {
            toDestLen = myProj.x;
            res = toDestAngle + 45 * SolMath.toInt(myProj.y < 0);
        }
    }
    Vector2 sunPos = p.getSys().getPos();
    float sunRad = Const.SUN_RADIUS;
    myProj.set(sunPos);
    myProj.sub(from);
    SolMath.rotate(myProj, -toDestAngle);
    if (0 < myProj.x && myProj.x < toDestLen) {
        if (SolMath.abs(myProj.y) < sunRad) {
            res = toDestAngle + 45 * SolMath.toInt(myProj.y < 0);
        }
    }
    return res;
}

From source file:org.destinationsol.game.input.Guardian.java

License:Apache License

@Override
public Boolean shouldManeuver(boolean canShoot, SolShip nearestEnemy, boolean nearGround) {
    if (!canShoot)
        return null;
    Vector2 targetPos = null;
    if (myTarget != null) {
        targetPos = myTarget.getPosition();
    } else if (myFarTarget != null) {
        targetPos = myFarTarget.getPos();
    }//  www. j a va2  s .c  o  m
    float maxManeuverDist = 2 * (nearGround ? Const.CAM_VIEW_DIST_GROUND : Const.CAM_VIEW_DIST_SPACE);
    if (targetPos != null && maxManeuverDist < targetPos.dst(nearestEnemy.getPosition()))
        return null;
    return true;
}

From source file:org.destinationsol.game.input.Mover.java

License:Apache License

public void update(SolGame game, SolShip ship, Vector2 dest, Planet np, float maxIdleDist, boolean hasEngine,
        boolean avoidBigObjs, float desiredSpdLen, boolean stopNearDest, Vector2 destSpd) {
    myUp = false;/*from   w w  w.j av  a  2 s  .  co  m*/
    myLeft = false;
    myRight = false;

    if (!hasEngine || dest == null)
        return;

    Vector2 shipPos = ship.getPosition();

    float toDestLen = shipPos.dst(dest);

    if (toDestLen < maxIdleDist) {
        if (!stopNearDest)
            return;
        myDesiredSpd.set(destSpd);
    } else {
        updateDesiredSpd(game, ship, dest, toDestLen, stopNearDest, np, avoidBigObjs, desiredSpdLen, destSpd);
    }

    Vector2 shipSpd = ship.getSpd();
    float spdDeviation = shipSpd.dst(myDesiredSpd);
    if (spdDeviation < MAX_ABS_SPD_DEV || spdDeviation < MAX_REL_SPD_DEV * shipSpd.len())
        return;

    float shipAngle = ship.getAngle();
    float rotSpd = ship.getRotSpd();
    float rotAcc = ship.getRotAcc();

    float desiredAngle = SolMath.angle(shipSpd, myDesiredSpd);
    float angleDiff = SolMath.angleDiff(desiredAngle, shipAngle);
    myUp = angleDiff < MIN_ANGLE_TO_ACC;
    Boolean ntt = needsToTurn(shipAngle, desiredAngle, rotSpd, rotAcc, MIN_MOVE_AAD);
    if (ntt != null) {
        if (ntt)
            myRight = true;
        else
            myLeft = true;
    }
}

From source file:org.destinationsol.game.input.Mover.java

License:Apache License

public void rotateOnIdle(SolShip ship, Planet np, Vector2 dest, boolean stopNearDest, float maxIdleDist) {
    if (isActive() || dest == null)
        return;/*from   w w  w .  ja  v a  2 s  .  com*/
    Vector2 shipPos = ship.getPosition();
    float shipAngle = ship.getAngle();
    float toDestLen = shipPos.dst(dest);
    float desiredAngle;
    float allowedAngleDiff;
    boolean nearFinalDest = stopNearDest && toDestLen < maxIdleDist;
    float dstToPlanet = np.getPos().dst(shipPos);
    if (nearFinalDest) {
        if (np.getFullHeight() < dstToPlanet)
            return; // stopping in space, don't care of angle
        // stopping on planet
        desiredAngle = SolMath.angle(np.getPos(), shipPos);
        allowedAngleDiff = MIN_PLANET_MOVE_AAD;
    } else {
        // flying somewhere
        if (dstToPlanet < np.getFullHeight() + Const.ATM_HEIGHT)
            return; // near planet, don't care of angle
        desiredAngle = SolMath.angle(ship.getSpd());
        allowedAngleDiff = MIN_MOVE_AAD;
    }

    Boolean ntt = needsToTurn(shipAngle, desiredAngle, ship.getRotSpd(), ship.getRotAcc(), allowedAngleDiff);
    if (ntt != null) {
        if (ntt)
            myRight = true;
        else
            myLeft = true;
    }
}

From source file:org.destinationsol.game.input.Shooter.java

License:Apache License

public void update(SolShip ship, Vector2 enemyPos, boolean dontRotate, boolean canShoot, Vector2 enemySpd,
        float enemyApproxRad) {
    myLeft = false;//  w w  w  .ja  v a  2s.c o m
    myRight = false;
    myShoot = false;
    myShoot2 = false;
    Vector2 shipPos = ship.getPosition();
    if (enemyPos == null || !canShoot)
        return;
    float toEnemyDst = enemyPos.dst(shipPos);

    GunItem g1 = processGun(ship, false);
    GunItem g2 = processGun(ship, true);
    if (g1 == null && g2 == null)
        return;

    float projSpd = 0;
    GunItem g = null;
    if (g1 != null) {
        ProjectileConfig projConfig = g1.config.clipConf.projConfig;
        projSpd = projConfig.spdLen + projConfig.acc; // for simplicity
        g = g1;
    }
    if (g2 != null) {
        ProjectileConfig projConfig = g2.config.clipConf.projConfig;
        float g2PS = projConfig.spdLen + projConfig.acc; // for simplicity
        if (projSpd < g2PS) {
            projSpd = g2PS;
            g = g2;
        }
    }

    Vector2 gunRelPos = ship.getHull().getGunMount(g == g2).getRelPos();
    Vector2 gunPos = SolMath.toWorld(gunRelPos, ship.getAngle(), shipPos);
    float shootAngle = calcShootAngle(gunPos, ship.getSpd(), enemyPos, enemySpd, projSpd, false);
    SolMath.free(gunPos);
    if (shootAngle != shootAngle)
        return;
    {
        // ok this is a hack
        float toShip = SolMath.angle(enemyPos, shipPos);
        float toGun = SolMath.angle(enemyPos, gunPos);
        shootAngle += toGun - toShip;
    }
    float shipAngle = ship.getAngle();
    float maxAngleDiff = SolMath.angularWidthOfSphere(enemyApproxRad, toEnemyDst) + 10f;
    ProjectileConfig projConfig = g.config.clipConf.projConfig;
    if (projSpd > 0 && projConfig.guideRotSpd > 0)
        maxAngleDiff += projConfig.guideRotSpd * toEnemyDst / projSpd;
    if (SolMath.angleDiff(shootAngle, shipAngle) < maxAngleDiff) {
        myShoot = true;
        myShoot2 = true;
        return;
    }

    if (dontRotate)
        return;
    Boolean ntt = Mover.needsToTurn(shipAngle, shootAngle, ship.getRotSpd(), ship.getRotAcc(), MIN_SHOOT_AAD);
    if (ntt != null) {
        if (ntt)
            myRight = true;
        else
            myLeft = true;
    }
}

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

License:Apache License

private void drawMazes(GameDrawer drawer, SolGame game, float viewDist, Planet np, Vector2 camPos,
        float heroDmgCap, float camAngle) {
    ArrayList<Maze> mazes = game.getPlanetMan().getMazes();
    for (int i = 0, mazesSize = mazes.size(); i < mazesSize; i++) {
        Maze maze = mazes.get(i);//from ww  w.  j  a va  2 s . c  o m
        Vector2 mazePos = maze.getPos();
        float outerRad = maze.getRadius();
        float rad = outerRad - MazeBuilder.BORDER;
        if (viewDist < camPos.dst(mazePos) - rad)
            continue;
        drawer.draw(myMazeTex, 2 * rad, 2 * rad, rad, rad, mazePos.x, mazePos.y, 45, SolColor.W);
        if (HardnessCalc.isDangerous(heroDmgCap, maze.getDps())) {
            drawAreaDanger(drawer, outerRad, mazePos, 1, camAngle);
        }
    }

}

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

License:Apache License

private void drawPlanets(GameDrawer drawer, SolGame game, float viewDist, Planet np, Vector2 camPos,
        float heroDmgCap, float camAngle) {
    ArrayList<SolSystem> systems = game.getPlanetMan().getSystems();
    SolCam cam = game.getCam();//w w w .jav a2  s  .co  m
    float circleWidth = cam.getRealLineWidth() * 6;
    float vh = cam.getViewHeight(myZoom);
    for (int i3 = 0, systemsSize1 = systems.size(); i3 < systemsSize1; i3++) {
        SolSystem sys = systems.get(i3);
        drawer.drawCircle(myLineTex, sys.getPos(), sys.getRadius(), SolColor.UI_MED, circleWidth, vh);
    }
    for (int i2 = 0, systemsSize = systems.size(); i2 < systemsSize; i2++) {
        SolSystem sys = systems.get(i2);
        float dangerRad = HardnessCalc.isDangerous(heroDmgCap, sys.getDps()) ? sys.getRadius() : 0;
        Vector2 sysPos = sys.getPos();
        float rad = Const.SUN_RADIUS;
        if (camPos.dst(sysPos) - rad < viewDist) {
            drawer.draw(myStarTex, 2 * rad, 2 * rad, rad, rad, sysPos.x, sysPos.y, 0, SolColor.W);
        }

        Vector2 beltIconPos = SolMath.getVec();
        ArrayList<SystemBelt> belts = sys.getBelts();
        for (int i1 = 0, beltsSize = belts.size(); i1 < beltsSize; i1++) {
            SystemBelt belt = belts.get(i1);
            float beltRad = belt.getRadius();
            float halfWidth = belt.getHalfWidth();
            int beltIconCount = (int) (.12f * beltRad);
            for (int i = 0; i < beltIconCount; i++) {
                float angle = 360f * i / beltIconCount;
                SolMath.fromAl(beltIconPos, angle, beltRad);
                beltIconPos.add(sysPos);
                drawer.draw(myBeltTex, 2 * halfWidth, 2 * halfWidth, halfWidth, halfWidth, beltIconPos.x,
                        beltIconPos.y, angle * 3, SolColor.W);
            }
            float outerRad = beltRad + halfWidth;
            if (dangerRad < outerRad && HardnessCalc.isDangerous(heroDmgCap, belt.getDps()))
                dangerRad = outerRad;
        }
        SolMath.free(beltIconPos);
        if (dangerRad < sys.getInnerRad() && HardnessCalc.isDangerous(heroDmgCap, sys.getInnerDps())) {
            dangerRad = sys.getInnerRad();
        }
        if (dangerRad > 0) {
            drawAreaDanger(drawer, dangerRad, sysPos, .5f, camAngle);
        }
    }

    ArrayList<Planet> planets = game.getPlanetMan().getPlanets();
    for (int i = 0, planetsSize = planets.size(); i < planetsSize; i++) {
        Planet planet = planets.get(i);
        Vector2 planetPos = planet.getPos();
        float fh = planet.getFullHeight();
        float dstToPlanetAtm = camPos.dst(planetPos) - fh;
        if (viewDist < dstToPlanetAtm)
            continue;
        drawer.draw(myAtmTex, 2 * fh, 2 * fh, fh, fh, planetPos.x, planetPos.y, 0, SolColor.UI_DARK);
        float gh;
        if (dstToPlanetAtm < 0) {
            gh = planet.getMinGroundHeight() + .5f;
            drawer.draw(myPlanetCoreTex, 2 * gh, 2 * gh, gh, gh, planetPos.x, planetPos.y, planet.getAngle(),
                    SolColor.W);
            drawNpGround(drawer, game, viewDist, np, camPos);
        } else {
            gh = planet.getGroundHeight();
            drawer.draw(myPlanetTex, 2 * gh, 2 * gh, gh, gh, planetPos.x, planetPos.y, camAngle, SolColor.W);
        }
        float dangerRad = HardnessCalc.isDangerous(heroDmgCap, planet.getGroundDps())
                ? gh + Const.ATM_HEIGHT / 2
                : 0;
        //      if (dangerRad < gh && HardnessCalc.isDangerous(heroDmgCap, planet.getGroundDps())) dangerRad = gh;
        if (dangerRad > 0) {
            drawAreaDanger(drawer, dangerRad, planetPos, 1, camAngle);
        }
    }
}

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

License:Apache License

private void drawIcons(GameDrawer drawer, SolGame game, float iconSz, float viewDist,
        FactionManager factionManager, SolShip hero, Vector2 camPos, float heroDmgCap) {
    List<SolObject> objs = game.getObjMan().getObjs();
    for (int i1 = 0, objsSize = objs.size(); i1 < objsSize; i1++) {
        SolObject o = objs.get(i1);/*from w  w  w  .  j a v a2 s  .c  om*/
        Vector2 oPos = o.getPosition();
        if (viewDist < camPos.dst(oPos))
            continue;
        if ((o instanceof SolShip)) {
            SolShip ship = (SolShip) o;
            String hint = ship.getPilot().getMapHint();
            if (hint == null && !DebugOptions.DETAILED_MAP)
                continue;
            drawObjIcon(iconSz, oPos, ship.getAngle(), factionManager, hero, ship.getPilot().getFaction(),
                    heroDmgCap, o, ship.getHull().config.getIcon(), drawer);
        }
        if ((o instanceof StarPort)) {
            StarPort sp = (StarPort) o;
            drawStarPortIcon(drawer, iconSz, sp.getFrom(), sp.getTo());
        }
        // Fix for when the player is in hyper. Hero is null and replaced in ObjMan with a StarPort.Transcendent
        if ((o instanceof StarPort.Transcendent)) {
            StarPort.Transcendent t = (StarPort.Transcendent) o;
            if (t.getShip().getPilot().isPlayer()) {
                FarShip ship = game.getTranscendentHero().getShip();
                drawObjIcon(iconSz, oPos, t.getAngle(), factionManager, hero, ship.getPilot().getFaction(),
                        heroDmgCap, o, ship.getHullConfig().getIcon(), drawer);
            }

        }
    }

    List<FarShip> farShips = game.getObjMan().getFarShips();
    for (int i = 0, sz = farShips.size(); i < sz; i++) {
        FarShip ship = farShips.get(i);
        Vector2 oPos = ship.getPos();
        if (viewDist < camPos.dst(oPos))
            continue;
        String hint = ship.getPilot().getMapHint();
        if (hint == null && !DebugOptions.DETAILED_MAP)
            continue;
        drawObjIcon(iconSz, oPos, ship.getAngle(), factionManager, hero, ship.getPilot().getFaction(),
                heroDmgCap, ship, ship.getHullConfig().getIcon(), drawer);
    }

    List<StarPort.MyFar> farPorts = game.getObjMan().getFarPorts();
    for (int i = 0, sz = farPorts.size(); i < sz; i++) {
        StarPort.MyFar sp = farPorts.get(i);
        drawStarPortIcon(drawer, iconSz, sp.getFrom(), sp.getTo());
    }
    BeaconHandler bh = game.getBeaconHandler();
    BeaconHandler.Action bhAction = bh.getCurrAction();
    if (bhAction != null) {
        Vector2 beaconPos = bh.getPos();
        TextureRegion icon = myBeaconMoveTex;
        if (bhAction == BeaconHandler.Action.ATTACK)
            icon = myBeaconAttackTex;
        else if (bhAction == BeaconHandler.Action.FOLLOW)
            icon = myBeaconFollowTex;
        float beaconSz = iconSz * 1.5f;
        //      drawer.draw(icon, beaconSz, beaconSz, beaconSz/2, beaconSz/2, beaconPos.x, beaconPos.y, 0, SolColor.W); interleaving
    }
}

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

License:Apache License

private void drawStarNodes(GameDrawer drawer, SolGame game, float viewDist, Vector2 camPos, float starNodeW) {
    List<SolObject> objs = game.getObjMan().getObjs();
    for (int i1 = 0, objsSize = objs.size(); i1 < objsSize; i1++) {
        SolObject o = objs.get(i1);//from  ww w .  j  a  v  a  2s. c  om
        if (!(o instanceof StarPort))
            continue;
        Vector2 oPos = o.getPosition();
        if (viewDist < camPos.dst(oPos))
            continue;
        StarPort sp = (StarPort) o;
        drawStarNode(drawer, sp.getFrom(), sp.getTo(), starNodeW);
    }

    List<StarPort.MyFar> farPorts = game.getObjMan().getFarPorts();
    for (int i = 0, sz = farPorts.size(); i < sz; i++) {
        StarPort.MyFar sp = farPorts.get(i);
        Vector2 oPos = sp.getPos();
        if (viewDist < camPos.dst(oPos))
            continue;
        if (!sp.isSecondary())
            drawStarNode(drawer, sp.getFrom(), sp.getTo(), starNodeW);
    }
}