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.editor.view.widgets.groupeditor.GroupEditorDragListener.java

License:Open Source License

/**
 * @return the directly children of the current edited group that are inside
 *         the given selection rectangle
 *//*  w ww . j av  a2 s . c  o m*/
private Array<Actor> getActorsInside(Rectangle selection) {
    if (selection.width < 0) {
        selection.x += selection.width;
        selection.width = Math.abs(selection.width);
    }

    if (selection.height < 0) {
        selection.y += selection.height;
        selection.height = Math.abs(selection.height);
    }

    Vector2 o = new Vector2();
    Vector2 t = new Vector2();
    Vector2 n = new Vector2();
    Vector2 d = new Vector2();
    Array<Actor> actors = new Array<Actor>();
    for (Actor a : editedGroup.getChildren()) {
        o.set(0, 0);
        t.set(a.getWidth(), 0);
        n.set(0, a.getHeight());
        d.set(a.getWidth(), a.getHeight());
        a.localToAscendantCoordinates(groupEditor, o);
        a.localToAscendantCoordinates(groupEditor, t);
        a.localToAscendantCoordinates(groupEditor, n);
        a.localToAscendantCoordinates(groupEditor, d);
        if (selection.contains(o) && selection.contains(t) && selection.contains(n) && selection.contains(d)
                && a.isTouchable() && a.isVisible()) {
            actors.add(a);
        }
    }
    return actors;
}

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

License:Open Source License

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

    float minX = Float.POSITIVE_INFINITY;
    float minY = Float.POSITIVE_INFINITY;
    float maxX = Float.NEGATIVE_INFINITY;
    float maxY = Float.NEGATIVE_INFINITY;
    for (Actor actor : actors) {
        // Ignore the modifier itself to calculate bounds
        if (actor != this) {
            tmp1.set(0, 0);
            tmp2.set(actor.getWidth(), 0);
            tmp3.set(0, actor.getHeight());
            tmp4.set(actor.getWidth(), actor.getHeight());
            actor.localToParentCoordinates(tmp1);
            actor.localToParentCoordinates(tmp2);
            actor.localToParentCoordinates(tmp3);
            actor.localToParentCoordinates(tmp4);

            minX = Math.min(minX, Math.min(tmp1.x, Math.min(tmp2.x, Math.min(tmp3.x, tmp4.x))));
            minY = Math.min(minY, Math.min(tmp1.y, Math.min(tmp2.y, Math.min(tmp3.y, tmp4.y))));
            maxX = Math.max(maxX, Math.max(tmp1.x, Math.max(tmp2.x, Math.max(tmp3.x, tmp4.x))));
            maxY = Math.max(maxY, Math.max(tmp1.y, Math.max(tmp2.y, Math.max(tmp3.y, tmp4.y))));
        }
    }
    resultOrigin.set(minX, minY);
    resultSize.set(maxX - minX, maxY - minY);
}

From source file:es.eucm.ead.engine.android.AndroidImageUtils.java

License:Open Source License

@Override
public boolean imageSize(FileHandle fileHandle, Vector2 size) {
    BitmapFactory.Options options = new Options();
    options.inJustDecodeBounds = true;//w  w w .  j a v  a 2s.  c o m
    BitmapFactory.decodeStream(fileHandle.read(), null, options);
    size.set(options.outWidth, options.outHeight);
    return true;
}

From source file:es.eucm.ead.engine.android.EngineAndroidApplicationListener.java

License:Open Source License

@Override
public void create() {
    imageUtils = new GameAssets.ImageUtils() {
        @Override/*from w  ww. j  a  va 2s. c  om*/
        public boolean imageSize(FileHandle fileHandle, Vector2 size) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            InputStream decodedFis = fileHandle.read();
            BitmapFactory.decodeStream(decodedFis, null, options);
            int imageHeight = options.outHeight;
            int imageWidth = options.outWidth;
            size.set(imageWidth, imageHeight);
            return true;
        }

        @Override
        public boolean validSize(Vector2 size) {
            return true;
        }

        @Override
        public float scale(FileHandle src, FileHandle target) {
            src.copyTo(target);
            return 1.0f;
        }
    };
    super.create();
}

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

License:Open Source License

/**
 * Creates a minimum rectangle that contains the given entity. The algorithm
 * takes into account renderers' colliders (if available), and children. The
 * algorithm is simple: search for min and max x and y coordinates to build
 * the rectangle./*  www  .j  av a 2 s .  c  o  m*/
 * 
 * @param entity
 *            The entity to build a bounding rectangle for.
 * @return The bounding rectangle, in coordinates of the
 *         {@link Layer#SCENE_CONTENT} layer. May return {@code null} if
 *         this entity is not a descendant of {@link Layer#SCENE_CONTENT}.
 */
public static RectangleWrapper getBoundingRectangle(EngineEntity entity) {
    RectangleWrapper rectangleWrapper = Pools.obtain(RectangleWrapper.class);
    Rectangle rectangle = Pools.obtain(Rectangle.class);
    rectangleWrapper.set(rectangle);
    /*
     * Vectors x and y will hold min and max coordinates: x.x = minX, x.y =
     * maxX y.x = minY, y.y = maxY
     */
    Vector2 x = Pools.obtain(Vector2.class);
    Vector2 y = Pools.obtain(Vector2.class);
    x.set(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY);
    y.set(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY);
    Group sceneContentGroup = getSceneContentAncestor(entity).getGroup();
    if (sceneContentGroup == null) {
        return null;
    }

    processRenderer(entity, sceneContentGroup, entity.getGroup(), x, y);

    for (Actor actor : entity.getGroup().getChildren()) {
        if (actor.getUserObject() != null && actor.getUserObject() instanceof EngineEntity) {
            EngineEntity child = (EngineEntity) actor.getUserObject();
            RectangleWrapper childWrapper = getBoundingRectangle(child);
            Rectangle childRect = childWrapper.rectangle;
            Vector2 bottomLeft = Pools.obtain(Vector2.class);
            Vector2 topRight = Pools.obtain(Vector2.class);
            bottomLeft.set(childRect.getX(), childRect.getY());
            topRight.set(childRect.getX() + childRect.getWidth(), childRect.getY() + childRect.getHeight());
            entity.getGroup().localToAscendantCoordinates(sceneContentGroup, bottomLeft);
            entity.getGroup().localToAscendantCoordinates(sceneContentGroup, topRight);
            x.set(Math.min(bottomLeft.x, x.x), Math.max(topRight.x, x.y));
            y.set(Math.min(bottomLeft.y, y.x), Math.max(topRight.y, y.y));
            Pools.free(bottomLeft);
            Pools.free(topRight);
            Pools.free(childWrapper);
        }
    }

    rectangle.x = x.x;
    rectangle.y = y.x;
    rectangle.width = x.y - x.x;
    rectangle.height = y.y - y.x;
    Pools.free(x);
    Pools.free(y);
    return rectangleWrapper;
}

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

License:Open Source License

private static boolean processRenderer(EngineEntity entity, Group sceneContentGroup, Group group, Vector2 x,
        Vector2 y) {//from   w  w  w .j  a v  a  2 s .  co m
    boolean hasRenderer = false;
    Vector2 tmp = Pools.obtain(Vector2.class);
    for (Polygon polygon : getColliders(entity)) {
        for (int i = 0; i < polygon.getVertices().length; i += 2) {
            tmp.set(polygon.getVertices()[i], polygon.getVertices()[i + 1]);
            group.localToAscendantCoordinates(sceneContentGroup, tmp);
            x.set(Math.min(tmp.x, x.x), Math.max(tmp.x, x.y));
            y.set(Math.min(tmp.y, y.x), Math.max(tmp.y, y.y));
            hasRenderer = true;
        }
    }
    Pools.free(tmp);
    return hasRenderer;
}

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

License:Open Source License

private static Polygon getBoundingPolygon(EngineEntity entity, Group sceneContentGroup, Group group) {
    SnapshotArray<Vector2> allPoints = new SnapshotArray<Vector2>();

    for (Polygon polygon : getColliders(entity)) {
        for (int i = 0; i < polygon.getVertices().length; i += 2) {
            Vector2 tmp = Pools.obtain(Vector2.class);
            tmp.set(polygon.getVertices()[i], polygon.getVertices()[i + 1]);
            group.localToAscendantCoordinates(sceneContentGroup, tmp);
            allPoints.add(tmp);//from www . j a v a2s  .co  m
        }
    }

    if (allPoints.size == 0) {
        return null;
    }

    // Remove duplicates, if any. Algorithm works better this way
    Object[] pointsToIterate = allPoints.begin();
    int size = allPoints.size;
    for (int i = 0; i < size; i++) {
        Vector2 pointA = (Vector2) pointsToIterate[i];
        for (int j = 0; j < size; j++) {
            Vector2 pointB = (Vector2) pointsToIterate[j];
            if (j != i && pointA.equals(pointB)) {
                allPoints.removeValue(pointB, true);
            }
        }
    }
    allPoints.end();

    // To array
    float[] points = toSimpleArray(allPoints);
    FloatArray floatArray = convexHull.computePolygon(points, false);
    // Remove the last point, since its the first one (duplicate)
    floatArray.removeRange(floatArray.size - 2, floatArray.size - 1);
    Polygon polygon = new Polygon();
    polygon.setVertices(floatArray.toArray());
    return polygon;
}

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

License:Open Source License

/**
 * Creates a minimum circle that contains the given entity. The algorithm
 * takes into account renderers' colliders (if available) and children. The
 * algorithm uses {@link #getBoundingPolygon(EngineEntity, Group, Group)}
 * and then calculates radius and center by finding the pair of vertex with
 * longest distance./* w  w w.  j a v a 2 s .co m*/
 * 
 * @param entity
 *            The entity to build a bounding circle for.
 * @return The bounding circle, in coordinates of the
 *         {@link Layer#SCENE_CONTENT} layer. May return {@code null} if
 *         this entity is not a descendant of {@link Layer#SCENE_CONTENT}.
 */
public static CircleWrapper getBoundingCircle(EngineEntity entity) {
    Polygon pol = getBoundingPolygon(entity);
    // Calculate pair of vertex with longest distance
    Vector2 center = Pools.obtain(Vector2.class);
    float maxDistance = Float.NEGATIVE_INFINITY;
    for (int i = 0; i < pol.getVertices().length; i += 2) {
        Vector2 vertex1 = Pools.obtain(Vector2.class);
        Vector2 vertex2 = Pools.obtain(Vector2.class);

        Vector2 vertex2toVertex1 = Pools.obtain(Vector2.class);
        vertex1.set(pol.getVertices()[i], pol.getVertices()[i + 1]);
        for (int j = 0; j < pol.getVertices().length - 1; j += 2) {
            if (i == j) {
                continue;
            }
            vertex2.set(pol.getVertices()[j], pol.getVertices()[j + 1]);
            vertex2toVertex1.set(vertex1);
            float distance = vertex2toVertex1.sub(vertex2).len();
            if (distance > maxDistance) {
                maxDistance = distance;
                center.set(vertex2).add(vertex2toVertex1.scl(0.5f));
            }
        }
        Pools.free(vertex1);
        Pools.free(vertex2);
        Pools.free(vertex2toVertex1);
    }
    Circle circle = Pools.obtain(Circle.class);
    circle.set(center.x, center.y, maxDistance / 2.0F);
    Pools.free(center);
    CircleWrapper circleWrapper = Pools.obtain(CircleWrapper.class);
    circleWrapper.set(circle);
    return circleWrapper;
}

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

License:Open Source License

static void toVector2Array(float[] from, Array<Vector2> to) {
    for (int i = 0; i < from.length; i += 2) {
        Vector2 vector = Pools.obtain(Vector2.class);
        vector.set(from[i], from[i + 1]);
        to.add(vector);// w  w w.ja  v a  2 s  .c om
    }
}

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

License:Open Source License

@Override
protected void getCenter(Vector2 center) {
    center.set(circle.x, circle.y);
}