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

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

Introduction

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

Prototype

@Override
    public Vector2 sub(Vector2 v) 

Source Link

Usage

From source file:com.tnf.ptm.common.CommonDrawer.java

License:Apache License

public void drawLine(TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) {
    Vector2 v = PtmMath.getVec(p2);
    v.sub(p1);
    drawLine(tex, p1.x, p1.y, PtmMath.angle(v, precise), v.len(), col, width);
    PtmMath.free(v);/*from  ww  w .  ja v  a2  s  .co  m*/
}

From source file:com.tnf.ptm.common.PtmMath.java

License:Apache License

/**
 * converts pos (a position in an absolute coordinate system) to the position in the relative system of coordinates
 * (defined by baseAngle and basePos) (which is written to relPos)
 *///  ww  w.  jav  a  2 s  .  co m
public static void toRel(Vector2 pos, Vector2 relPos, float baseAngle, Vector2 basePos) {
    relPos.set(pos);
    relPos.sub(basePos);
    rotate(relPos, -baseAngle);
}

From source file:com.tnf.ptm.common.PtmMath.java

License:Apache License

/**
 * @return a new bound vector that is a substraction (to - from)
 *///from www.ja v  a2s  .  co  m
@Bound
public static Vector2 distVec(Vector2 from, Vector2 to) {
    Vector2 v = getVec(to);
    v.sub(from);
    return v;
}

From source file:com.tnf.ptm.entities.chunk.ChunkFiller.java

License:Apache License

/**
 * Add a bunch of a certain type of junk to the background layers furthest away.
 * <p/>//from  ww  w  .  j  a v  a  2  s .  c  o  m
 * This type of junk does not move on its own, it merely changes position as the camera moves, simulating different
 * depths relative to the camera.
 *
 * @param game       The {@link PtmGame} instance to work with
 * @param chCenter   The center of the chunk
 * @param remover
 * @param draLevel   The depth of the junk
 * @param conf       The environment configuration
 * @param densityMul A density multiplier. This will be multiplied with the density defined in the environment configuration
 */
private void fillFarJunk(PtmGame game, Vector2 chCenter, RemoveController remover, DraLevel draLevel,
        SpaceEnvConfig conf, float densityMul) {
    if (conf == null) {
        return;
    }
    int count = getEntityCount(conf.farJunkDensity * densityMul);
    if (count == 0) {
        return;
    }

    ArrayList<Dra> dras = new ArrayList<Dra>();
    TextureManager textureManager = game.getTexMan();

    for (int i = 0; i < count; i++) {
        // Select a random far junk texture
        TextureAtlas.AtlasRegion tex = PtmMath.elemRnd(conf.farJunkTexs);
        // Flip atlas for every other piece of junk
        if (PtmMath.test(.5f)) {
            tex = textureManager.getFlipped(tex);
        }
        // Choose a random size (within a range)
        float sz = PtmMath.rnd(.3f, 1) * FAR_JUNK_MAX_SZ;
        // Apply a random rotation speed
        float rotSpd = PtmMath.rnd(FAR_JUNK_MAX_ROT_SPD);
        // Select a random position in the chunk centered around chCenter, relative to the position of the chunk.
        Vector2 junkPos = getRndPos(chCenter);
        junkPos.sub(chCenter);

        // Create the resulting sprite and add it to the list
        RectSprite s = new RectSprite(tex, sz, 0, 0, junkPos, draLevel, PtmMath.rnd(180), rotSpd, PtmColor.DDG,
                false);
        dras.add(s);
    }

    // Create a common FarDras instance for the pieces of junk and only allow the junk to be drawn when it's not hidden by a planet
    FarDras so = new FarDras(dras, new Vector2(chCenter), new Vector2(), remover, true);
    // Add the collection of objects to the object manager
    game.getObjMan().addFarObjNow(so);
}

From source file:com.tnf.ptm.entities.chunk.ChunkFiller.java

License:Apache License

/**
 * Add specks of dust to the background layer closest to the front.
 * <p/>//from   ww w.  j  av a  2s . c o m
 * Dust is fixed in the world and therefore moves opposite to the cameras movement.
 *
 * @param game     The {@link PtmGame} instance to work with
 * @param chCenter The center of the chunk
 * @param remover
 */
private void fillDust(PtmGame game, Vector2 chCenter, RemoveController remover) {
    ArrayList<Dra> dras = new ArrayList<Dra>();
    int count = getEntityCount(DUST_DENSITY);
    if (count == 0) {
        return;
    }

    TextureAtlas.AtlasRegion tex = myDustTex;
    for (int i = 0; i < count; i++) {
        // Select a random position in the chunk centered around chCenter, relative to the position of the chunk.
        Vector2 dustPos = getRndPos(chCenter);
        dustPos.sub(chCenter);
        // Create the resulting sprite and add it to the list
        RectSprite s = new RectSprite(tex, DUST_SZ, 0, 0, dustPos, DraLevel.DECO, 0, 0, PtmColor.WHITE, false);
        dras.add(s);
    }

    // Create a common FarDras instance for the specks of dust and only allow the dust to be drawn when it's not hidden by a planet
    FarDras so = new FarDras(dras, chCenter, new Vector2(), remover, true);
    game.getObjMan().addFarObjNow(so);
}

From source file:com.tnf.ptm.entities.item.Loot.java

License:Apache License

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

From source file:com.tnf.ptm.entities.item.Loot.java

License:Apache License

public void pickedUp(PtmGame game, PtmShip ship) {
    myLife = 0;// w  w w  .  j  av a  2s.c  om
    Vector2 spd = new Vector2(ship.getPosition());
    spd.sub(myPos);
    float fadeTime = .25f;
    spd.scl(1 / fadeTime);
    spd.add(ship.getSpd());
    game.getPartMan().blip(game, myPos, myAngle, myItem.getItemType().sz, fadeTime, spd, myItem.getIcon(game));
    game.getSoundManager().play(game, myItem.getItemType().pickUpSound, null, this);
}

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

License:Apache License

public Vector2 find(PtmGame game, Planet p, ConsumedAngles takenAngles, float objHalfWidth) {
    Vector2 pPos = p.getPos();//w w w  .  j  a v  a  2s  .  c  om

    Vector2 res = new Vector2(pPos);
    float minDeviation = 90;
    float resAngle = 0;
    float objAngularHalfWidth = PtmMath.angularWidthOfSphere(objHalfWidth, p.getGroundHeight());

    for (int i = 0; i < 20; i++) {
        float angle = PtmMath.rnd(180);
        if (takenAngles != null && takenAngles.isConsumed(angle, objAngularHalfWidth)) {
            continue;
        }
        myDeviation = angle;
        PtmMath.fromAl(myVec, angle, p.getFullHeight());
        myVec.add(pPos);
        game.getObjMan().getWorld().rayCast(myRayBack, myVec, pPos);
        if (myDeviation < minDeviation) {
            res.set(myVec);
            minDeviation = myDeviation;
            resAngle = angle;
        }
    }

    if (takenAngles != null) {
        takenAngles.add(resAngle, objAngularHalfWidth);
    }
    res.sub(pPos);
    PtmMath.rotate(res, -p.getAngle());
    return res;
}

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

License:Apache License

public void setDiff(Vector2 diff, Vector2 pos, boolean precise) {
    PtmMath.toWorld(diff, myRelPos, myPlanet.getAngle(), myPlanet.getPos(), precise);
    diff.sub(pos);
}

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;
        }/*  w w w  .  j av a2  s .c om*/

        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);
        }
    }

}