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:jordanlw.gdxGame.Game.java

License:Open Source License

private void handlePlayersBeingAttacked(Character victim, Character attacker) {
    Vector2 relativeEnemyPosition = new Vector2(victim.position.x - attacker.position.x,
            victim.position.y - attacker.position.y);
    if (relativeEnemyPosition.len() <= 10) {
        if (attacker.lastAttack + attacker.attackDelay > totalTime) {
            return;
        }//from w  w  w.  ja  v  a  2s.  co m
        attacker.lastAttack = totalTime;

        victim.health -= 10 * Gdx.graphics.getDeltaTime();
        victim.secondsDamaged = 1;
        aMusicLibrary.hurtSound.play(0.5f);
    }
}

From source file:MeshBoneUtil.CreatureManager.java

License:Open Source License

public String ProcessContactBone(Vector2 pt_in, float radius, MeshBone bone_in) {
    String ret_name = "";
    Vector3 diff_vec = bone_in.getWorldEndPt().cpy().sub(bone_in.getWorldStartPt());

    Vector2 cur_vec = new Vector2(diff_vec.x, diff_vec.y);
    float cur_length = (float) cur_vec.len();

    Vector2 unit_vec = cur_vec.cpy();
    unit_vec.nor();//from   w w  w  .  j  a  va 2 s .  c o m

    Vector2 norm_vec = new Vector2(unit_vec.y, unit_vec.x);

    Vector2 src_pt = new Vector2(bone_in.getWorldStartPt().x, bone_in.getWorldStartPt().y);
    Vector2 rel_vec = pt_in.cpy().sub(src_pt);
    float proj = (float) rel_vec.dot(unit_vec);

    if ((proj >= 0) && (proj <= cur_length)) {
        float norm_proj = (float) rel_vec.dot(norm_vec);
        if (norm_proj <= radius) {
            return bone_in.getKey();
        }
    }

    Vector<MeshBone> cur_children = bone_in.getChildren();
    for (MeshBone cur_child : cur_children) {
        ret_name = ProcessContactBone(pt_in, radius, cur_child);
        if (!(ret_name.equals(""))) {
            break;
        }
    }

    return ret_name;
}

From source file:org.destinationsol.common.SolMath.java

License:Apache License

/**
 * @return a length of a projection of a vector onto a line defined by angle
 */// ww w .j a  v  a 2s.c o m
public static float project(Vector2 v, float angle) {
    float angleDiff = angle - SolMath.angle(v);
    return v.len() * cos(angleDiff);
}

From source file:org.destinationsol.CommonDrawer.java

License:Apache License

public void drawLine(TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) {
    Vector2 v = SolMath.getVec(p2);
    v.sub(p1);//  w  ww  .  ja  v a 2 s .com
    drawLine(tex, p1.x, p1.y, SolMath.angle(v, precise), v.len(), col, width);
    SolMath.free(v);
}

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

License:Apache License

@Override
public void update(SolGame game, SolShip ship, SolShip nearestEnemy) {
    myAbilityUpdater.update(ship, nearestEnemy);
    myPlanetBind = null;/*from www. j av  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:org.destinationsol.game.input.AiPilot.java

License:Apache License

@Override
public void updateFar(SolGame 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();
    EngineItem engine = farShip.getEngine();
    float ts = game.getTimeStep();
    if (dest == null || engine == null) {
        if (myPlanetBind == null) {
            if (myBindAwait > 0) {
                myBindAwait -= ts;/* w  w w  . java 2s .  co  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 = SolMath.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 = SolMath.approach(spd.len(), desiredSpdLen, spdLenDiff);
            if (toDestLen < spdLen)
                spdLen = toDestLen;
            SolMath.fromAl(spd, desiredAngle, spdLen);
        }
        angle = SolMath.approachAngle(angle, desiredAngle, engine.getMaxRotSpd() * ts);
    }

    farShip.setSpd(spd);
    farShip.setAngle(angle);

    Vector2 newPos = SolMath.getVec(spd);
    newPos.scl(ts);
    newPos.add(shipPos);
    farShip.setPos(newPos);
    SolMath.free(newPos);
}

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.  ja  v  a 2  s  .  com*/
    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.Shooter.java

License:Apache License

public static float calcShootAngle(Vector2 gunPos, Vector2 gunSpd, Vector2 ePos, Vector2 eSpd, float projSpd,
        boolean sharp) {
    Vector2 eSpdShortened = SolMath.getVec(eSpd);
    if (!sharp)/*from   www  .  j av a  2 s.  c  o m*/
        eSpdShortened.scl(E_SPD_PERC);
    Vector2 relESpd = SolMath.distVec(gunSpd, eSpdShortened);
    SolMath.free(eSpdShortened);
    float rotAngle = SolMath.angle(relESpd);
    float v = relESpd.len();
    float v2 = projSpd;
    SolMath.free(relESpd);
    Vector2 toE = SolMath.distVec(gunPos, ePos);
    SolMath.rotate(toE, -rotAngle);
    float x = toE.x;
    float y = toE.y;
    float a = v * v - v2 * v2;
    float b = 2 * x * v;
    float c = x * x + y * y;
    float t = SolMath.genQuad(a, b, c);
    float res;
    if (t != t) {
        res = Float.NaN;
    } else {
        toE.x += t * v;
        res = SolMath.angle(toE) + rotAngle;
    }
    SolMath.free(toE);
    return res;
}

From source file:org.destinationsol.game.item.Loot.java

License:Apache License

public void maybePulled(SolShip ship, Vector2 pullerPos, float radius) {
    if (ship == myOwner)
        return;//from   w w  w  .  jav a  2s .  co  m
    Vector2 toPuller = SolMath.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 = SolMath.distVec(spd, toPuller);
        float spdDiffLen = spdDiff.len();
        if (spdDiffLen > 0) {
            spdDiff.scl(PULL_FORCE / spdDiffLen);
            myBody.applyForceToCenter(spdDiff, true);
        }
        SolMath.free(spdDiff);
    }
    SolMath.free(toPuller);
}

From source file:org.destinationsol.game.planet.Planet.java

License:Apache License

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