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

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

Introduction

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

Prototype

@Override
    public Vector2 scl(Vector2 v) 

Source Link

Usage

From source file:org.ams.prettypaint.TextureAligner.java

License:Open Source License

private Vector2 getOrigin(Array<TexturePolygon> texturePolygons, int origin) {
    Rectangle boundingRectangle = new Rectangle();
    boolean initialized = false;
    for (TexturePolygon texturePolygon : texturePolygons) {
        if (!initialized) {
            initialized = true;/*from  w  w w . j av a  2  s .c o m*/
            boundingRectangle.set(texturePolygon.getBoundingRectangle());
        } else
            boundingRectangle.merge(texturePolygon.getBoundingRectangle());
    }

    Vector2 v = boundingRectangle.getCenter(new Vector2());
    v.scl(texturePolygons.first().getTextureScale());

    TextureRegion firstRegion = texturePolygons.first().getTextureRegion();
    float firstTextureScale = texturePolygons.first().getTextureScale();
    v.add(firstRegion.getRegionWidth() * firstTextureScale * 0.5f,
            firstRegion.getRegionHeight() * firstTextureScale * 0.5f);

    return v;
}

From source file:org.destinationsol.game.asteroid.Asteroid.java

License:Apache License

@Override
public void receiveForce(Vector2 force, SolGame game, boolean acc) {
    if (acc)//from   w ww .j a va 2 s .co m
        force.scl(myMass);
    myBody.applyForceToCenter(force, true);
}

From source file:org.destinationsol.game.chunk.ChunkFiller.java

License:Apache License

/**
 * Fill the background of a given chunk with floating junk.
 *
 * @param game    The {@link SolGame} instance to work with
 * @param chunk   The coordinates of the chunk
 * @param remover//from  w  w w  . j  ava  2 s .c om
 * @param farBg   Determines which of the background layers should be filled. <code>true</code> fills the layers furthest away, <code>false</code> fills the closer one.
 */
public void fill(SolGame game, Vector2 chunk, RemoveController remover, boolean farBg) {
    if (DebugOptions.NO_OBJS)
        return;

    // Determine the center of the chunk by multiplying the chunk coordinates with the chunk size and adding half a chunk's size
    Vector2 chCenter = new Vector2(chunk);
    chCenter.scl(Const.CHUNK_SIZE);
    chCenter.add(Const.CHUNK_SIZE / 2, Const.CHUNK_SIZE / 2);

    // Define the density multiplier for different layers of junk in the far background
    float[] densityMul = { 1 };

    // Get the environment configuration
    SpaceEnvConfig conf = getConfig(game, chCenter, densityMul, remover, farBg);

    if (farBg) {
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_3, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_2, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_1, conf, densityMul[0]);
    } else {
        fillDust(game, chCenter, remover);
        fillJunk(game, remover, conf, chCenter);
    }
}

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.ja  v  a2s  .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.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   w ww.j a  v a 2s  .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  www .j  a v  a 2  s.  c  o  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.item.Loot.java

License:Apache License

public void pickedUp(SolGame game, SolShip ship) {
    myLife = 0;//w  w  w.  j ava 2  s  .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.getSoundMan().play(game, myItem.getItemType().pickUpSound, null, this);
}

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;
    }//w ww. j  a  va  2  s.  c om
    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;
}

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

License:Apache License

private void applyGrav(SolGame game, SolSystem 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<SolObject> objs = game.getObjMan().getObjs();
    for (int i = 0, objsSize = objs.size(); i < objsSize; i++) {
        SolObject obj = objs.get(i);/*from w  w w.j  a  v  a  2  s.  co  m*/
        if (!obj.receivesGravity())
            continue;

        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 = SolMath.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);
        SolMath.free(grav);
        if (!onPlanet) {
            mySunSingleton.doDmg(game, obj, toSys);
        }
    }

}

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

License:Apache License

private boolean recoverObj(SolObject obj, float toNp, float npMinH) {
    if (npMinH < toNp)
        return false;
    if (!(obj instanceof SolShip))
        return false;
    SolShip ship = (SolShip) obj;/*from   w ww .  ja  v  a 2  s .co  m*/
    Hull hull = ship.getHull();
    if (hull.config.getType() == HullConfig.Type.STATION)
        return false;
    float fh = myNearestPlanet.getFullHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 toShip = SolMath.distVec(npPos, ship.getPosition());
    float len = toShip.len();
    if (len == 0) {
        toShip.set(0, fh);
    } else {
        toShip.scl(fh / len);
    }
    toShip.add(npPos);
    Body body = hull.getBody();
    body.setTransform(toShip, 0);
    body.setLinearVelocity(Vector2.Zero);
    SolMath.free(toShip);
    return true;
}