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.planet.PlanetObjectsBuilder.java

License:Apache License

private void addDeco0(SolGame game, float groundHeight, Vector2 planetPos, Map<Vector2, List<Dra>> collector,
        DecoConfig dc) {//from   ww  w.j  av a2  s.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 * SolMath.PI * groundHeight * dc.density);
    for (int i = 0; i < decoCount; i++) {
        float decoSz = SolMath.rnd(dc.szMin, dc.szMax);
        float angularHalfWidth = SolMath.angularWidthOfSphere(decoSz / 2, groundHeight);

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

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

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

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

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

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

License:Apache License

private void updatePos(SolGame game) {
    Vector2 camPos = game.getCam().getPos();
    Vector2 planetPos = myPlanet.getPos();
    if (planetPos.dst(camPos) < myPlanet.getGroundHeight() + Const.MAX_SKY_HEIGHT_FROM_GROUND) {
        myPos.set(camPos);//www .  j  a v a  2 s  .co  m
        return;
    }
    myPos.set(planetPos);
}

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

License:Apache License

@Override
public void update(SolGame game) {
    updatePos(game);//from w  w  w.  j a  v  a 2  s . c  o  m

    Vector2 planetPos = myPlanet.getPos();
    SolCam cam = game.getCam();
    Vector2 camPos = cam.getPos();
    float distPerc = 1
            - (planetPos.dst(camPos) - myPlanet.getGroundHeight()) / Const.MAX_SKY_HEIGHT_FROM_GROUND;
    if (distPerc < 0)
        return;
    if (1 < distPerc)
        distPerc = 1;

    Vector2 sysPos = myPlanet.getSys().getPos();
    float angleToCam = SolMath.angle(planetPos, camPos);
    float angleToSun = SolMath.angle(planetPos, sysPos);
    float dayPerc = 1 - SolMath.angleDiff(angleToCam, angleToSun) / 180;
    float skyIntensity = SolMath.clamp(1 - ((1 - dayPerc) / .75f));
    float skyColorPerc = SolMath.clamp((skyIntensity - .5f) * 2f + .5f);
    mySkySpan.set(skyColorPerc, myGrad.tint);
    mySkySpan.set(skyColorPerc, myFill.tint);
    float gradPerc = SolMath.clamp(2 * skyIntensity);
    float fillPerc = SolMath.clamp(2 * (skyIntensity - .5f));
    myGrad.tint.a = gradPerc * distPerc;
    myFill.tint.a = fillPerc * SolMath.clamp(1 - (1 - distPerc) * 2) * .37f;

    float viewDist = cam.getViewDist();
    float sz = 2 * viewDist;
    myGrad.setTexSz(sz);
    myFill.setTexSz(sz);

    float angleCamToSun = angleToCam - angleToSun;
    float relAngle;
    if (SolMath.abs(SolMath.norm(angleCamToSun)) < 90)
        relAngle = angleToCam + 180 + angleCamToSun;
    else
        relAngle = angleToCam - angleCamToSun;
    myGrad.relAngle = relAngle - 90;
}

From source file:org.destinationsol.game.screens.BorderDrawer.java

License:Apache License

private void maybeDrawIcon(UiDrawer drawer, Vector2 pos, SolCam cam, float objSize, float objAngle,
        MapDrawer mapDrawer, FactionManager factionManager, SolShip 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;/* ww  w  .  j  av a 2s .c o  m*/
    float camAngle = cam.getAngle();
    SolMath.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 < SolMath.abs(myTmpVec.x / myTmpVec.y);
    float mul = SolMath.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:org.destinationsol.game.screens.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 = SolMath.angle(camPos, objPos) - camAngle;
        float angularWHalf = SolMath.angularWidthOfSphere(objRad, dst);
        apply(distPerc, angularWHalf, relAngle);
    }/*from  w ww  .  j  av  a  2 s.c o  m*/
}

From source file:org.destinationsol.game.screens.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 ww. ja  v a2s.c o m
    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 = SolMath.clamp(1.5f * y);
    if (maxY < y)
        y = maxY;
    float angle = np.getAngle() - camAngle;
    drawer.draw(myCompassTex, sz, sz, sz / 2, sz / 2, sz / 2, y, angle, myCompassTint);
}

From source file:org.destinationsol.game.ship.EmWave.java

License:Apache License

@Override
public boolean update(SolGame game, SolShip owner, boolean tryToUse) {
    if (!tryToUse)
        return false;
    Vector2 ownerPos = owner.getPosition();
    for (SolObject o : game.getObjMan().getObjs()) {
        if (!(o instanceof SolShip) || o == owner)
            continue;
        SolShip oShip = (SolShip) o;/*from w w  w. j av a2s  . com*/
        if (!game.getFactionMan().areEnemies(oShip, owner))
            continue;
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        float perc = KnockBack.getPerc(dst, MAX_RADIUS);
        if (perc <= 0)
            continue;
        float duration = perc * myConfig.duration;
        oShip.disableControls(duration, game);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

From source file:org.destinationsol.game.ship.KnockBack.java

License:Apache License

@Override
public boolean update(SolGame game, SolShip owner, boolean tryToUse) {
    if (!tryToUse)
        return false;
    Vector2 ownerPos = owner.getPosition();
    for (SolObject o : game.getObjMan().getObjs()) {
        if (o == owner || !o.receivesGravity())
            continue;
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        if (dst == 0)
            continue; // O__o
        float perc = getPerc(dst, MAX_RADIUS);
        if (perc <= 0)
            continue;
        Vector2 toO = SolMath.distVec(ownerPos, oPos);
        float accLen = myConfig.force * perc;
        toO.scl(accLen / dst);//from   w ww .  ja  v  a2  s  .  c  o m
        o.receiveForce(toO, game, false);
        SolMath.free(toO);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

From source file:org.destinationsol.game.ship.UnShield.java

License:Apache License

@Override
public boolean update(SolGame game, SolShip owner, boolean tryToUse) {
    if (!tryToUse)
        return false;
    Vector2 ownerPos = owner.getPosition();
    for (SolObject o : game.getObjMan().getObjs()) {
        if (!(o instanceof SolShip) || o == owner)
            continue;
        SolShip oShip = (SolShip) o;//  ww  w .j a v a2 s .c o  m
        Shield shield = oShip.getShield();
        if (shield == null)
            continue;
        float shieldLife = shield.getLife();
        if (shieldLife <= 0)
            continue;
        if (!game.getFactionMan().areEnemies(oShip, owner))
            continue;
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        float perc = KnockBack.getPerc(dst, MAX_RADIUS);
        if (perc <= 0)
            continue;
        float amount = perc * myConfig.amount;
        if (shieldLife < amount)
            amount = shieldLife;
        oShip.receiveDmg(amount, game, ownerPos, DmgType.ENERGY);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

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

License:Apache License

public void update(SolGame game) {

    float desiredVd = Const.CAM_VIEW_DIST_GROUND;
    float life = 0;

    SolShip hero = game.getHero();/*from  w  w  w . j  a  v  a 2 s  .c om*/
    float ts = game.getTimeStep();
    if (hero == null) {
        StarPort.Transcendent trans = game.getTranscendentHero();
        if (trans == null) {
            if (DebugOptions.DIRECT_CAM_CONTROL) {
                applyInput(game);
            }
        } else {
            desiredVd = Const.CAM_VIEW_DIST_SPACE;
            myPos.set(trans.getPosition());
        }
    } else {
        Vector2 heroPos = hero.getHull().getBody().getWorldCenter();
        if (myZoom * VIEWPORT_HEIGHT < heroPos.dst(myPos)) {
            myPos.set(heroPos);
            game.getObjMan().resetDelays();
        } else {
            Vector2 moveDiff = SolMath.getVec(hero.getSpd());
            moveDiff.scl(ts);
            myPos.add(moveDiff);
            SolMath.free(moveDiff);
            float moveSpd = MOVE_SPD * ts;
            myPos.x = SolMath.approach(myPos.x, heroPos.x, moveSpd);
            myPos.y = SolMath.approach(myPos.y, heroPos.y, moveSpd);
        }
        life = hero.getLife();

        float spd = hero.getSpd().len();

        desiredVd = Const.CAM_VIEW_DIST_SPACE;
        Planet np = game.getPlanetMan().getNearestPlanet(myPos);
        if (np.getFullHeight() < np.getPos().dst(myPos) && MAX_ZOOM_SPD < spd) {
            desiredVd = Const.CAM_VIEW_DIST_JOURNEY;
        } else if (np.isNearGround(myPos) && spd < MED_ZOOM_SPD) {
            desiredVd = Const.CAM_VIEW_DIST_GROUND;
        }
        desiredVd += hero.getHull().config.getApproxRadius();
    }

    if (life < myPrevHeroLife) {
        float shakeDiff = .1f * MAX_SHAKE * (myPrevHeroLife - life);
        myShake = SolMath.approach(myShake, MAX_SHAKE, shakeDiff);
    } else {
        myShake = SolMath.approach(myShake, 0, SHAKE_DAMP * ts);
    }
    myPrevHeroLife = life;

    Vector2 pos = SolMath.fromAl(SolMath.rnd(180), myShake);
    pos.add(myPos);
    applyPos(pos.x, pos.y);
    SolMath.free(pos);

    float desiredAngle = myCamRotStrategy.getRotation(myPos, game);
    float rotSpd = CAM_ROT_SPD * ts;
    myAngle = SolMath.approachAngle(myAngle, desiredAngle, rotSpd);
    applyAngle();

    float desiredZoom = calcZoom(desiredVd);
    myZoom = SolMath.approach(myZoom, desiredZoom, ZOOM_CHG_SPD * ts);
    applyZoom(game.getMapDrawer());
    myCam.update();
}