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.entities.planet.Planet.java

License:Apache License

public void update(PtmGame game) {
    float ts = game.getTimeStep();
    myAngleToSys += myToSysRotSpd * ts;//from w w w.j av a2 s .c  o m
    myAngle += myRotSpd * ts;

    setSecondaryParams();
    Vector2 camPos = game.getCam().getPos();
    if (!myObjsCreated && camPos.dst(myPos) < getGroundHeight() + Const.MAX_SKY_HEIGHT_FROM_GROUND) {
        myMinGroundHeight = new PlanetObjectsBuilder().createPlanetObjs(game, this);
        fillLangingPlaces(game);
        myObjsCreated = true;
    }
}

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

License:Apache License

public void draw(PtmGame game, GameDrawer drawer) {
    PtmCam cam = game.getCam();//w  w w. j a  v a  2 s  .c o  m
    Vector2 camPos = cam.getPos();
    Planet p = game.getPlanetMan().getNearestPlanet();
    Vector2 pPos = p.getPos();
    float toCamLen = camPos.dst(pPos);
    float vd = cam.getViewDist();
    float gh = p.getMinGroundHeight();
    if (toCamLen < gh + vd) {
        float sz = gh;
        drawer.draw(myTex, sz * 2, sz * 2, sz, sz, pPos.x, pPos.y, p.getAngle(), PtmColor.WHITE);
    }
}

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

License:Apache License

public Planet getNearestPlanet(Vector2 pos) {
    float minDst = Float.MAX_VALUE;
    Planet res = null;//  w w w.j a v a 2  s  . co  m
    for (Planet planet : myPlanets) {
        float dst = pos.dst(planet.getPos());
        if (dst < minDst) {
            minDst = dst;
            res = planet;
        }
    }
    return res;
}

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 .  ja  va  2s.c  o m

        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

public PtmSystem getNearestSystem(Vector2 pos) {
    float minDst = Float.MAX_VALUE;
    PtmSystem res = null;/*from w ww  . j  av a2 s. c  om*/
    for (PtmSystem system : mySystems) {
        float dst = pos.dst(system.getPos());
        if (dst < minDst) {
            minDst = dst;
            res = system;
        }
    }
    return res;
}

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

License:Apache License

public Maze getNearestMaze(Vector2 pos) {
    float minDst = Float.MAX_VALUE;
    Maze res = null;//www . java  2  s.c om
    for (Maze maze : myMazes) {
        float dst = pos.dst(maze.getPos());
        if (dst < minDst) {
            minDst = dst;
            res = maze;
        }
    }
    return res;
}

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

License:Apache License

public void createDeco(PtmGame game, Planet planet) {
    float groundHeight = planet.getGroundHeight();
    Vector2 planetPos = planet.getPos();
    float planetAngle = planet.getAngle();
    Map<Vector2, List<Dra>> collector = new HashMap<Vector2, List<Dra>>();
    PlanetConfig config = planet.getConfig();
    for (DecoConfig dc : config.deco) {
        addDeco0(game, groundHeight, planetPos, collector, dc);
    }//  w  ww .  ja  va2 s.com

    for (Map.Entry<Vector2, List<Dra>> e : collector.entrySet()) {
        Vector2 packPos = e.getKey();
        List<Dra> ss = e.getValue();
        float packAngle = PtmMath.angle(planetPos, packPos, true) - planetAngle;
        float packDist = packPos.dst(planetPos);
        FarPlanetSprites ps = new FarPlanetSprites(planet, packAngle, packDist, ss, 0);
        game.getObjMan().addFarObjNow(ps);
    }
}

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

License:Apache License

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

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

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

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

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

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

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

License:Apache License

private void updatePos(PtmGame 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);/*  w w w  . j a v a  2 s . c om*/
        return;
    }
    myPos.set(planetPos);
}

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

License:Apache License

@Override
public void update(PtmGame game) {
    updatePos(game);/* w ww .j  av  a 2  s .co  m*/

    Vector2 planetPos = myPlanet.getPos();
    PtmCam 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 = PtmMath.angle(planetPos, camPos);
    float angleToSun = PtmMath.angle(planetPos, sysPos);
    float dayPerc = 1 - PtmMath.angleDiff(angleToCam, angleToSun) / 180;
    float skyIntensity = PtmMath.clamp(1 - ((1 - dayPerc) / .75f));
    float skyColorPerc = PtmMath.clamp((skyIntensity - .5f) * 2f + .5f);
    mySkySpan.set(skyColorPerc, myGrad.tint);
    mySkySpan.set(skyColorPerc, myFill.tint);
    float gradPerc = PtmMath.clamp(2 * skyIntensity);
    float fillPerc = PtmMath.clamp(2 * (skyIntensity - .5f));
    myGrad.tint.a = gradPerc * distPerc;
    myFill.tint.a = fillPerc * PtmMath.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 (PtmMath.abs(PtmMath.norm(angleCamToSun)) < 90) {
        relAngle = angleToCam + 180 + angleCamToSun;
    } else {
        relAngle = angleToCam - angleCamToSun;
    }
    myGrad.relAngle = relAngle - 90;
}