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

public Vector2 set(float x, float y) 

Source Link

Document

Sets the components of this vector

Usage

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

License:Open Source License

@Override
public boolean intersectToSegment(Vector2 start, Vector2 end, Vector2 intersection) {
    boolean intersected = false;
    Vector2 v1 = Pools.obtain(Vector2.class);
    Vector2 v2 = Pools.obtain(Vector2.class);
    Polygon rectPol = Pools.obtain(Polygon.class);
    toPolygon(rectPol);/*w w  w.ja v  a  2  s  .c o  m*/
    for (int i = 0; i < 4 && !intersected; i++) {
        v1.set(rectPol.getVertices()[(2 * i) % 8], rectPol.getVertices()[(2 * i + 1) % 8]);
        v2.set(rectPol.getVertices()[(2 * i + 2) % 8], rectPol.getVertices()[(2 * i + 3) % 8]);
        intersected = Intersector.intersectSegments(v1, v2, start, end, intersection);
    }
    Pools.free(v1);
    Pools.free(v2);
    Pools.free(rectPol);
    return intersected;
}

From source file:es.eucm.ead.engine.mock.MockImageUtils.java

License:Open Source License

@Override
public boolean imageSize(FileHandle fileHandle, Vector2 size) {
    Pixmap pixmap = new Pixmap(fileHandle);
    size.set(pixmap.getWidth(), pixmap.getHeight());
    return true;//from  w  w  w.j  a  v  a  2  s  .c  om
}

From source file:es.eucm.ead.engine.systems.positiontracking.MoveByEntitySystem.java

License:Open Source License

protected void getSpeedOfTrackedEntity(MoveByEntityComponent moveByEntity, Vector2 trackedEntitySpeed) {
    trackedEntitySpeed
            .set(moveByEntity.getTrackedEntity().getGroup().getX(),
                    moveByEntity.getTrackedEntity().getGroup().getY())
            .sub(moveByEntity.getLastX(), moveByEntity.getLastY());
}

From source file:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * Calculate the bounds of the possible children of the given actor. If
 * actor has no children, then resultOrigin and resultSize are set to
 * actor's bounds.//from w ww  .ja  v a2  s.  c  o  m
 */
public static void calculateBounds(Actor actor, Vector2 resultOrigin, Vector2 resultSize) {
    resultOrigin.set(0, 0);
    resultSize.set(actor.getWidth(), actor.getHeight());
    if (actor instanceof Group && ((Group) actor).getChildren().size > 0) {
        calculateBounds(((Group) actor).getChildren(), resultOrigin, resultSize);
    }
}

From source file:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * Calculate the bounds of the given actors as a group
 * //from ww w. j a v a 2  s .  c o  m
 * @param actors
 *            the actors
 * @param resultOrigin
 *            result origin of the bounds
 * @param resultSize
 *            result size of the bounds
 */
public static void calculateBounds(Array<Actor> actors, Vector2 resultOrigin, Vector2 resultSize) {
    resultOrigin.set(0, 0);
    resultSize.set(0, 0);
    if (actors.size == 0) {
        return;
    }

    Vector2 origin = Pools.obtain(Vector2.class);
    Vector2 size = Pools.obtain(Vector2.class);
    Vector2 leftTop = Pools.obtain(Vector2.class);
    Vector2 rightBottom = Pools.obtain(Vector2.class);
    float minX = Float.POSITIVE_INFINITY;
    float minY = Float.POSITIVE_INFINITY;
    float maxX = Float.NEGATIVE_INFINITY;
    float maxY = Float.NEGATIVE_INFINITY;
    for (Actor actor : actors) {
        calculateBounds(actor, origin, size);
        size.add(origin);
        leftTop.set(origin.x, size.y);
        rightBottom.set(size.x, origin.y);
        actor.localToParentCoordinates(origin);
        actor.localToParentCoordinates(size);
        actor.localToParentCoordinates(leftTop);
        actor.localToParentCoordinates(rightBottom);

        minX = Math.min(minX, Math.min(origin.x, Math.min(size.x, Math.min(leftTop.x, rightBottom.x))));
        minY = Math.min(minY, Math.min(origin.y, Math.min(size.y, Math.min(leftTop.y, rightBottom.y))));
        maxX = Math.max(maxX, Math.max(origin.x, Math.max(size.x, Math.max(leftTop.x, rightBottom.x))));
        maxY = Math.max(maxY, Math.max(origin.y, Math.max(size.y, Math.max(leftTop.y, rightBottom.y))));
    }
    Pools.free(origin);
    Pools.free(size);
    Pools.free(leftTop);
    Pools.free(rightBottom);
    resultOrigin.set(minX, minY);
    resultSize.set(maxX - minX, maxY - minY);
}

From source file:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * Adjusts the position and size of the given group to its children
 *//*www .java2 s  . com*/
public static void adjustGroup(Actor root) {
    if (!(root instanceof Group)) {
        return;
    }

    Group group = (Group) root;
    if (group.getChildren().size == 0) {
        return;
    }

    Vector2 origin = Pools.obtain(Vector2.class);
    Vector2 size = Pools.obtain(Vector2.class);
    Vector2 tmp3 = Pools.obtain(Vector2.class);
    Vector2 tmp4 = Pools.obtain(Vector2.class);

    calculateBounds(group.getChildren(), origin, size);

    /*
     * minX and minY are the new origin (new 0, 0), so everything inside the
     * group must be translated that much.
     */
    for (Actor actor : group.getChildren()) {
        actor.setPosition(actor.getX() - origin.x, actor.getY() - origin.y);
    }

    /*
     * Now, we calculate the current origin (0, 0) and the new origin (minX,
     * minY), and group is translated by that difference.
     */
    group.localToParentCoordinates(tmp3.set(0, 0));
    group.localToParentCoordinates(tmp4.set(origin.x, origin.y));
    tmp4.sub(tmp3);
    group.setBounds(group.getX() + tmp4.x, group.getY() + tmp4.y, size.x, size.y);
    group.setOrigin(size.x / 2.0f, size.y / 2.0f);

    Pools.free(origin);
    Pools.free(size);
    Pools.free(tmp3);
    Pools.free(tmp4);
}

From source file:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * Sets position, rotation, scale and origin in actor to meet the 3 given
 * points/*from ww w. j a  v  a 2s  .c  o  m*/
 */
public static void applyTransformation(Actor actor, 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 tmp1 = Pools.obtain(Vector2.class);
    Vector2 tmp2 = Pools.obtain(Vector2.class);
    Vector2 tmp3 = Pools.obtain(Vector2.class);
    Vector2 tmp4 = Pools.obtain(Vector2.class);
    Vector2 tmp5 = Pools.obtain(Vector2.class);

    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 = actor.getRotation();
    vt.rotate(-rotation);
    vn.rotate(-rotation);

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

    Vector2 bottomLeft = Pools.obtain(Vector2.class);
    Vector2 size = Pools.obtain(Vector2.class);

    calculateBounds(actor, bottomLeft, size);

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

    Pools.free(tmp1);
    Pools.free(tmp2);
    Pools.free(tmp3);
    Pools.free(tmp4);
    Pools.free(tmp5);
    Pools.free(bottomLeft);
    Pools.free(size);

    // 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;

    actor.setScale(scaleX, scaleY);

    /*
     * To obtain the correct translation value we need to subtract the
     * amount of translation due to the origin.
     */
    tmpMatrix.setToTranslation(actor.getOriginX(), actor.getOriginY());
    tmpMatrix.rotate(actor.getRotation());
    tmpMatrix.scale(actor.getScaleX(), actor.getScaleY());
    tmpMatrix.translate(-actor.getOriginX(), -actor.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];
    actor.setPosition(x, y);
}

From source file:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * For a given actor computes and applies the transformation to keep the
 * same screen transformation in a new group
 * /*  ww  w .  j  a  v a2  s .  c o  m*/
 * @param actor
 * @param parent
 */
public static void computeTransformFor(Actor actor, Group parent) {
    Vector2 tmp1 = Pools.obtain(Vector2.class);
    Vector2 tmp2 = Pools.obtain(Vector2.class);
    Vector2 tmp3 = Pools.obtain(Vector2.class);
    Vector2 tmp4 = Pools.obtain(Vector2.class);
    Vector2 tmp5 = Pools.obtain(Vector2.class);

    calculateBounds(actor, tmp4, tmp5);
    Vector2 o = tmp1.set(tmp4.x, tmp4.y);
    Vector2 t = tmp2.set(tmp4.x + tmp5.x, tmp4.y);
    Vector2 n = tmp3.set(tmp4.x, tmp4.y + tmp5.y);
    actor.localToAscendantCoordinates(parent, o);
    actor.localToAscendantCoordinates(parent, t);
    actor.localToAscendantCoordinates(parent, n);
    actor.setRotation(actor.getRotation() + actor.getParent().getRotation());
    applyTransformation(actor, o, t, n);

    Pools.free(tmp1);
    Pools.free(tmp2);
    Pools.free(tmp3);
    Pools.free(tmp4);
    Pools.free(tmp5);
}

From source file:es.eucm.ead.engine.utils.ShapeToCollider.java

License:Open Source License

/**
 * Builds a circumscribed polygon for a {@code circle} that can be used as a
 * collider.// w ww .  j  a va2s  .c  om
 * 
 * The number of sides of the polygon is specified through param
 * {@code nSides}.
 * 
 * The algorithm to calculate the minimum nSides polygon that covers a
 * circle (circumscribed) is as follows:
 * 
 * (To see what a circumscribed polygon is, visit: <a href=
 * "http://www.vitutor.com/geometry/plane/circumscribed_polygons.html"
 * >http://www.vitutor.com/geometry/plane/circumscribed_polygons.html</a>)
 * 
 * 1) Distance from circle's center to each vertex (equidistant) is
 * calculated (d).
 * 
 * 2) A vector with length d is calculated.
 * 
 * 3) Vector is added to the circle's center to get one vertex.
 * 
 * 4) The vector is rotated "a" degrees. "a" is calculated by dividing 360
 * by the number of sides of the polygon.
 * 
 * (3) and (4) are repeated until all the vertices are calculated.
 * 
 * Note: d=r/cos(a/2), where: r: circle's radius a: angle
 * 
 * See <a href="https://github.com/e-ucm/ead/wiki/Shape-renderer">This wiki
 * page</a> for more details
 */
public static Polygon buildCircleCollider(Circle circle, int nSides) {
    Polygon polygon = new Polygon();
    float vertices[] = new float[nSides * 2];
    // The radius
    float r = circle.getRadius();
    // The center of the circle. Since we work on a coordinate system where
    // the origin is in the bottom-left corner of the element, the center is
    // located in (radius, radius).
    float cx = r, cy = r;

    // How much we must rotate the radius vector (normal to the circle)
    float angleInc = 360.0F / nSides;
    double halfAngleIncRad = Math.PI / nSides;

    // Distance from center to each of the vertices
    float d = (float) (r / Math.cos(halfAngleIncRad));

    // Initialization of the vector used to calculate each vertex.
    Vector2 centerToVertex = new Vector2();
    centerToVertex.set(0, d);
    centerToVertex.rotateRad((float) halfAngleIncRad);

    for (int i = 0; i < nSides; i++) {
        // Calculate vertex
        vertices[2 * i] = cx + centerToVertex.x;
        vertices[2 * i + 1] = cy + centerToVertex.y;
        // Rotate for the next vertex
        centerToVertex.rotate(angleInc);
    }

    polygon.setVertices(vertices);
    return polygon;
}

From source file:fr.plafogaj.screens.Game.java

License:Open Source License

private void initLifeArtifacts() {
    int nbArtifacts = 30;
    Vector2 pos = new Vector2();
    int widthMap = m_tiledMapConfig.getMap().getProperties().get("width", Integer.class);

    for (int i = 0; i < nbArtifacts; ++i) {
        pos.set(MathUtils.random(0, widthMap), MathUtils.random(0, 30));
        m_artifactsLife.add(new ArtifactLife(MathUtils.random(1, 5), pos));
    }//w  w w.  j  a  va 2 s.  c  o  m
}