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:com.tnf.ptm.handler.input.Guardian.java

License:Apache License

@Override
public Boolean shouldManeuver(boolean canShoot, PtmShip nearestEnemy, boolean nearGround) {
    if (!canShoot) {
        return null;
    }//  ww w.ja  va  2s. com
    Vector2 targetPos = null;
    if (myTarget != null) {
        targetPos = myTarget.getPosition();
    } else if (myFarTarget != null) {
        targetPos = myFarTarget.getPos();
    }
    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:com.tnf.ptm.handler.input.Mover.java

License:Apache License

public void update(PtmGame game, PtmShip ship, Vector2 dest, Planet np, float maxIdleDist, boolean hasEngine,
        boolean avoidBigObjs, float desiredSpdLen, boolean stopNearDest, Vector2 destSpd) {
    myUp = false;//from  www . jav a2s  .c  o 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 = PtmMath.angle(shipSpd, myDesiredSpd);
    float angleDiff = PtmMath.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:com.tnf.ptm.handler.input.Mover.java

License:Apache License

public void rotateOnIdle(PtmShip ship, Planet np, Vector2 dest, boolean stopNearDest, float maxIdleDist) {
    if (isActive() || dest == null) {
        return;//from  w  w  w. j av  a  2s . co m
    }
    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 = PtmMath.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 = PtmMath.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:com.tnf.ptm.handler.input.Shooter.java

License:Apache License

public void update(PtmShip ship, Vector2 enemyPos, boolean dontRotate, boolean canShoot, Vector2 enemySpd,
        float enemyApproxRad) {
    myLeft = false;/*from  w  ww . j av a  2  s  . com*/
    myRight = false;
    myShoot = false;
    myShoot2 = false;
    Vector2 shipPos = ship.getPosition();
    if (enemyPos == null || !canShoot) {
        return;
    }
    float toEnemyDst = enemyPos.dst(shipPos);

    Gun g1 = processGun(ship, false);
    Gun g2 = processGun(ship, true);
    if (g1 == null && g2 == null) {
        return;
    }

    float projSpd = 0;
    Gun 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 = PtmMath.toWorld(gunRelPos, ship.getAngle(), shipPos);
    float shootAngle = calcShootAngle(gunPos, ship.getSpd(), enemyPos, enemySpd, projSpd, false);
    PtmMath.free(gunPos);
    if (shootAngle != shootAngle) {
        return;
    }
    {
        // ok this is a hack
        float toShip = PtmMath.angle(enemyPos, shipPos);
        float toGun = PtmMath.angle(enemyPos, gunPos);
        shootAngle += toGun - toShip;
    }
    float shipAngle = ship.getAngle();
    float maxAngleDiff = PtmMath.angularWidthOfSphere(enemyApproxRad, toEnemyDst) + 10f;
    ProjectileConfig projConfig = g.config.clipConf.projConfig;
    if (projSpd > 0 && projConfig.guideRotSpd > 0) {
        maxAngleDiff += projConfig.guideRotSpd * toEnemyDst / projSpd;
    }
    if (PtmMath.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:com.tnf.ptm.screens.controlers.BorderDrawer.java

License:Apache License

private void maybeDrawIcon(UiDrawer drawer, Vector2 pos, PtmCam cam, float objSize, float objAngle,
        MapDrawer mapDrawer, FactionManager factionManager, PtmShip hero, Faction objFac, Object shipHack,
        float heroDmgCap, TextureAtlas.AtlasRegion icon) {
    Vector2 camPos = cam.getPos();
    float closeness = 1 - pos.dst(camPos) / MAX_ICON_DIST;
    if (closeness < 0) {
        return;/*www .  ja v  a2  s . co m*/
    }
    float camAngle = cam.getAngle();
    PtmMath.toRel(pos, myTmpVec, camAngle, camPos);
    float len = myTmpVec.len();
    float newLen = len - .25f * objSize;
    myTmpVec.scl(newLen / len);

    if (cam.isRelVisible(myTmpVec)) {
        return;
    }

    float sz = BORDER_ICON_SZ * closeness;
    float prefX = drawer.r / 2 - sz / 2;
    float prefY = .5f - sz / 2;
    float r = prefX / prefY;
    boolean prefXAxis = myTmpVec.y == 0 || r < PtmMath.abs(myTmpVec.x / myTmpVec.y);
    float mul = PtmMath.abs(prefXAxis ? (prefX / myTmpVec.x) : (prefY / myTmpVec.y));
    myTmpVec.scl(mul);
    myTmpVec.add(drawer.r / 2, .5f);

    mapDrawer.drawObjIcon(sz, myTmpVec, objAngle - camAngle, factionManager, hero, objFac, heroDmgCap, shipHack,
            icon, drawer);
}

From source file:com.tnf.ptm.screens.controlers.BorderDrawer.java

License:Apache License

private void apply0(Vector2 camPos, float camAngle, Vector2 objPos, float objRad) {
    float dst = objPos.dst(camPos);
    float distPerc = (dst - objRad) / MAX_DRAW_DIST;
    if (distPerc < 1) {
        float relAngle = PtmMath.angle(camPos, objPos) - camAngle;
        float angularWHalf = PtmMath.angularWidthOfSphere(objRad, dst);
        apply(distPerc, angularWHalf, relAngle);
    }//from   w  ww .j  a v  a 2 s  .c o m
}

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

License:Apache License

private void drawHeight(UiDrawer drawer, Planet np, Vector2 camPos, float camAngle) {
    float toPlanet = camPos.dst(np.getPos());
    toPlanet -= np.getGroundHeight();/*from  w  w  w .ja va 2 s. c om*/
    if (Const.ATM_HEIGHT < toPlanet) {
        return;
    }
    float perc = toPlanet / Const.ATM_HEIGHT;
    float sz = .08f;
    float maxY = 1 - sz / 2;
    float y = 1 - perc;
    myCompassTint.a = PtmMath.clamp(1.5f * y);
    if (maxY < y) {
        y = maxY;
    }
    float angle = np.getAngle() - camAngle;
    drawer.draw(compassTex, sz, sz, sz / 2, sz / 2, sz / 2, y, angle, myCompassTint);
}

From source file:com.tnf.ptm.sound.OggSoundManager.java

License:Apache License

/**
 * Plays a sound. Source must not be null.
 *
 * @param position         position of a sound. If null, source.getPosition() will be used
 * @param source           bearer of a sound. Must not be null for looped sounds
 * @param volumeMultiplier multiplier for sound volume
 *///from  w ww .j  a v a2 s  .c  om
public void play(PtmGame game, PlayableSound playableSound, @Nullable Vector2 position,
        @Nullable PtmObject source, float volumeMultiplier) {
    if (playableSound == null) {
        return;
    }

    OggSound sound = playableSound.getOggSound();
    // logger.debug("Playing sound: {}", sound.getUrn().toString());

    // Perform some initial argument validation
    if (source == null && position == null) {
        throw new AssertionError("Either position or source must be non-null");
    }
    if (source == null && sound.getLoopTime() > 0) {
        throw new AssertionError("Attempted to loop a sound without a parent object: " + sound.getUrn());
    }
    if (position == null) {
        position = source.getPosition();
    }

    // Calculate the volume multiplier for the sound
    float globalVolumeMultiplier = game.getCmp().getOptions().sfxVolumeMultiplier;
    if (globalVolumeMultiplier == 0) {
        return;
    }

    Vector2 cameraPosition = game.getCam().getPos();
    Planet nearestPlanet = game.getPlanetMan().getNearestPlanet();

    float airPerc = 0;
    if (nearestPlanet.getConfig().skyConfig != null) {
        float distanceToAtmosphere = cameraPosition.dst(nearestPlanet.getPos())
                - nearestPlanet.getGroundHeight() - Const.ATM_HEIGHT / 2;
        airPerc = PtmMath.clamp(1 - distanceToAtmosphere / (Const.ATM_HEIGHT / 2));
    }
    if (DebugOptions.SOUND_IN_SPACE) {
        airPerc = 1;
    }

    float maxSoundDist = 1 + 1.5f * Const.CAM_VIEW_DIST_GROUND * airPerc;

    PtmShip hero = game.getHero();
    float soundRadius = hero == null ? 0 : hero.getHull().config.getApproxRadius();
    float distance = position.dst(cameraPosition) - soundRadius;
    float distanceMultiplier = PtmMath.clamp(1 - distance / maxSoundDist);

    float volume = sound.getBaseVolume() * volumeMultiplier * distanceMultiplier * globalVolumeMultiplier;

    if (volume <= 0) {
        return;
    }

    // Calculate the pitch for the sound
    float pitch = PtmMath.rnd(.97f, 1.03f) * game.getTimeFactor() * playableSound.getBasePitch();

    if (skipLooped(source, sound, game.getTime())) {
        return;
    }

    if (DebugOptions.SOUND_INFO) {
        debugHintDrawer.add(source, position, sound.toString());
    }

    Sound gdxSound = sound.getSound();
    gdxSound.play(volume, pitch, 0);
}

From source file:com.vlaaad.dice.game.actions.imp.Antidote.java

License:Open Source License

private Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation, int x, int y,
        World world, float distance) {
    Vector2 creaturePos = tmp1.set(x, y);
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : world) {
        if (!(object instanceof Creature))
            continue;
        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > distance)
            continue;
        if (!check.hasEffect(PoisonEffect.class))
            continue;
        result.add(check);// w  ww .j ava2  s. c  o  m
    }
    return result;
}

From source file:com.vlaaad.dice.game.actions.imp.AreaOfAttack.java

License:Open Source License

protected Array<Creature> findTargets(Creature creature, Creature.CreatureRelation relation) {
    Vector2 creaturePos = tmp1.set(creature.getX(), creature.getY());
    World world = creature.world;/*from   w w w  .j  a  v  a2  s .c  om*/
    Array<Creature> result = new Array<Creature>();
    for (WorldObject object : world) {
        if (!(object instanceof Creature))
            continue;
        Creature check = (Creature) object;
        if (!check.get(Attribute.canBeSelected) || !creature.inRelation(relation, check))
            continue;
        Vector2 checkPos = tmp2.set(check.getX(), check.getY());
        if (checkPos.dst(creaturePos) > radius)
            continue;
        result.add(check);
    }
    return result;
}