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

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

Introduction

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

Prototype

@Override
    public Vector2 set(Vector2 v) 

Source Link

Usage

From source file:es.eucm.ead.editor.view.widgets.groupeditor.Modifier.java

License:Open Source License

/**
 * Applies the current transformation represented by the handles to given
 * actor/* w w w  . j av  a2  s  . com*/
 */
public void applyTransformation(Actor influencedActor, Vector2 origin, Vector2 tangent, Vector2 normal) {
    /*
     * We are going to calculate the affine transformation for the actor to
     * fit the bounds represented by the handles. The affine transformation
     * is defined as follows:
     */
    // |a b tx|
    // |c d ty|=|Translation Matrix| x |Scale Matrix| x |Rotation
    // Matrix|
    // |0 0 1 |
    /*
     * More info about affine transformations:
     * https://people.gnome.org/~mathieu
     * /libart/libart-affine-transformation-matrices.html, To obtain the
     * matrix, we want to resolve the following equation system:
     */
    // | a b tx| |0| |o.x|
    // | c d ty|*|0|=|o.y|
    // | 0 0 1 | |1| | 1 |
    //
    // | a b tx| |w| |t.x|
    // | c d ty|*|0|=|t.y|
    // | 0 0 1 | |1| | 1 |
    //
    // | a b tx| |0| |n.x|
    // | c d ty|*|h|=|n.y|
    // | 0 0 1 | |1| | 1 |
    /*
     * where o is handles[0] (origin), t is handles[2] (tangent) and n is
     * handles[6] (normal), w is actor.getWidth() and h is
     * actor.getHeight().
     * 
     * This matrix defines that the 3 points defining actor bounds are
     * transformed to the 3 points defining modifier bounds. E.g., we want
     * that actor origin (0,0) is transformed to (handles[0].x,
     * handles[0].y), and that is expressed in the first equation.
     * 
     * Resolving these equations is obtained:
     */
    // a = (t.x - o.y) / w
    // b = (t.y - o.y) / w
    // c = (n.x - o.x) / h
    // d = (n.y - o.y) / h
    /*
     * Values for translation, scale and rotation contained by the matrix
     * can be obtained directly making operations over a, b, c and d:
     */
    // tx = o.x
    // ty = o.y
    // sx = sqrt(a^2+b^2)
    // sy = sqrt(c^2+d^2)
    // rotation = atan(c/d)
    // or
    // rotation = atan(-b/a)
    /*
     * Rotation can give two different values (this happens when there is
     * more than one way of obtaining the same transformation). To avoid
     * that, we ignore the rotation to obtain the final values.
     */

    Vector2 o = tmp1.set(origin.x, origin.y);
    Vector2 t = tmp2.set(tangent.x, tangent.y);
    Vector2 n = tmp3.set(normal.x, normal.y);

    Vector2 vt = tmp4.set(t).sub(o);
    Vector2 vn = tmp5.set(n).sub(o);

    // Ignore rotation
    float rotation = influencedActor.getRotation();
    vt.rotate(-rotation);
    vn.rotate(-rotation);

    t.set(vt).add(o);
    n.set(vn).add(o);

    float a = (t.x - o.x) / influencedActor.getWidth();
    float c = (t.y - o.y) / influencedActor.getWidth();
    float b = (n.x - o.x) / influencedActor.getHeight();
    float d = (n.y - o.y) / influencedActor.getHeight();

    // Math.sqrt gives a positive value, but it also have a negatives.
    // The
    // signum is calculated computing the current rotation
    float signumX = vt.angle() > 90.0f && vt.angle() < 270.0f ? -1.0f : 1.0f;
    float signumY = vn.angle() > 180.0f ? -1.0f : 1.0f;

    float scaleX = (float) Math.sqrt(a * a + b * b) * signumX;
    float scaleY = (float) Math.sqrt(c * c + d * d) * signumY;

    influencedActor.setScale(scaleX, scaleY);

    /*
     * To obtain the correct translation value we need to subtract the
     * amount of translation due to the origin.
     */
    tmpMatrix.setToTranslation(influencedActor.getOriginX(), influencedActor.getOriginY());
    tmpMatrix.rotate(influencedActor.getRotation());
    tmpMatrix.scale(influencedActor.getScaleX(), influencedActor.getScaleY());
    tmpMatrix.translate(-influencedActor.getOriginX(), -influencedActor.getOriginY());

    /*
     * Now, the matrix has how much translation is due to the origin
     * involved in the rotation and scaling operations
     */
    float x = o.x - tmpMatrix.getValues()[Matrix3.M02];
    float y = o.y - tmpMatrix.getValues()[Matrix3.M12];
    influencedActor.setPosition(x, y);
}

From source file:es.eucm.ead.engine.collision.AreaWrapper.java

License:Open Source License

/**
 * Calculates distance from {@code this area} to {@code end}, stores the
 * resulting vector in {@code intersection}, and returns the float value of
 * the distance calculated./*from  www . j  a  v  a2  s.c o  m*/
 * 
 * @param end
 *            Area to calculate distance to
 * @param intersection
 *            Vector to store distance. If distance cannot be calculated,
 *            this vector is not updated. Resulting vector points from this
 *            area to {@code end}.
 * @param fromBorder
 *            True if distance is to be calculated between areas' borders,
 *            false if distance must be calculated between centers. If
 *            {@code fromBorder} is true, and one of the areas contains the
 *            other, this method may return {@code -1} if each area contains
 *            the other's center and {@code intersection} is not updated. If
 *            {@code fromBorder} is true but each area does not contain the
 *            other's center (but both areas overlap), distance is
 *            calculated although it is not representative, and
 *            {@code intersection} points the other way (from end to start).
 * @return The float value of the distance calculated, or -1 (see comment
 *         above).
 */
public float getDistance(AreaWrapper end, Vector2 intersection, boolean fromBorder) {
    Vector2 c1 = Pools.obtain(Vector2.class);
    Vector2 c2 = Pools.obtain(Vector2.class);
    end.getCenter(c1);
    getCenter(c2);

    if (fromBorder) {
        Vector2 i1 = Pools.obtain(Vector2.class);
        Vector2 i2 = Pools.obtain(Vector2.class);

        boolean intersected = end.intersectToSegment(c1, c2, i1) && intersectToSegment(c1, c2, i2);
        if (!intersected) {
            return -1;
        }

        i1.sub(i2);
        intersection.set(i1);

        Pools.free(i1);
        Pools.free(i2);
    } else {
        intersection.set(c1.sub(c2));
    }
    Pools.free(c1);
    Pools.free(c2);

    return intersection.len();
}

From source file:es.eucm.ead.engine.collision.CircleWrapper.java

License:Open Source License

@Override
protected boolean intersectToSegment(Vector2 start, Vector2 end, Vector2 intersection) {
    Vector2 v = Pools.obtain(Vector2.class);
    if (circle.contains(start) && !circle.contains(end)) {
        v.set(end);
        v.sub(start);//from www  .j a v  a2 s  .  c o  m
    } else if (!circle.contains(start) && circle.contains(end)) {
        v.set(start);
        v.sub(end);
    } else {
        return false;
    }

    v.scl(circle.radius / v.len());

    getCenter(intersection);
    intersection.add(v);
    Pools.free(v);
    return true;
}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @see #position(Shape, Body) */
public static Vector2 position(Shape shape, Body body, Vector2 output) {
    return output.set(position(shape, body));
}

From source file:org.ams.core.Util.java

License:Open Source License

/**
 * Puts the coordinates of the endpoints of the edge with index i into
 * v1 and v2./*  w w  w .j a  va 2  s . c om*/
 */
public static void getEdge(Array<Vector2> vertices, int i, Vector2 v1, Vector2 v2) {
    v1.set(vertices.get(i % vertices.size));
    v2.set(vertices.get((i + 1) % vertices.size));
}

From source file:org.ams.physics.things.Polygon.java

License:Open Source License

public Array<Vector2> getVerticesRotatedAndTranslated() {

    for (int i = 0; i < vertices.size; i++) {

        Vector2 v = verticesRotatedAndTranslated.items[i];

        v.set(vertices.items[i]).rotateRad(body.getAngle()).add(body.getPosition());

    }//from   w w w .j a  v  a  2  s.c  om

    return verticesRotatedAndTranslated;
}

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

License:Open Source License

@Override
public Array<Vector2> getVerticesRotatedScaledAndTranslated(float rotation, float scale, float transX,
        float transY) {

    for (int i = 0; i < vertices.size; i++) {
        Vector2 w = verticesRotatedAndTranslated.items[i];
        w.set(vertices.items[i]);
        w.rotateRad(rotation);//from   www  .j  a va  2s . c o  m
        w.scl(scale);
        w.add(transX, transY);
    }
    return verticesRotatedAndTranslated;
}

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

License:Apache License

private boolean maybeUpdateTargetPos(SolGame game) {
    updateTarget(game);//from  ww  w.j  av  a2 s .  com
    if (myTargetPilot == null)
        return false;
    Vector2 beaconPos = getPos0();
    if (myTarget != null) {
        SolMath.toWorld(beaconPos, myTargetRelPos, myTarget.getAngle(), myTarget.getPosition(), false);
        mySpd.set(myTarget.getSpd());
    } else {
        beaconPos.set(myFarTarget.getPos());
    }
    return true;
}

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

License:Apache License

public Vector2 getPlayerSpawnPos(SolGame game) {
    Vector2 pos = new Vector2(Const.SUN_RADIUS * 2, 0);

    if ("planet".equals(DebugOptions.SPAWN_PLACE)) {
        Planet p = game.getPlanetMan().getPlanets().get(0);
        pos.set(p.getPos());
        pos.x += p.getFullHeight();/*from w  w w .j ava  2 s .c o m*/
    } else if (DebugOptions.SPAWN_PLACE.isEmpty() && myMainStationPos != null) {
        SolMath.fromAl(pos, 90, myMainStationHc.getSize() / 2);
        pos.add(myMainStationPos);
    } else if ("maze".equals(DebugOptions.SPAWN_PLACE)) {
        Maze m = game.getPlanetMan().getMazes().get(0);
        pos.set(m.getPos());
        pos.x += m.getRadius();
    } else if ("trader".equals(DebugOptions.SPAWN_PLACE)) {
        HullConfig cfg = game.getHullConfigs().getConfig("bus");
        for (FarObjData fod : game.getObjMan().getFarObjs()) {
            FarObj fo = fod.fo;
            if (!(fo instanceof FarShip))
                continue;
            if (((FarShip) fo).getHullConfig() != cfg)
                continue;
            pos.set(fo.getPos());
            pos.add(cfg.getApproxRadius() * 2, 0);
            break;
        }

    }
    return pos;
}

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  .j  ava2  s  . c  om*/
            } 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);
}