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.vlaaad.dice.game.world.view.ViewScroller.java

License:Open Source License

public void centerOnImmediately(float x, float y) {
    Vector2 viewCoordinate = tmp1.set(x * ViewController.CELL_SIZE, y * ViewController.CELL_SIZE);
    Vector2 currentCenter = tmp2.set(stage.getWidth(), stage.getHeight()).scl(0.5f);
    Vector2 targetPosition = currentCenter.sub(viewCoordinate);
    clamp(targetPosition);/*from w  w  w.  j  a va2  s . co m*/
    root.setPosition(hScrollEnabled ? targetPosition.x : root.getX(),
            vScrollEnabled ? targetPosition.y : root.getY());
}

From source file:DungeonCleanerGame.CombatManager.java

public void computeStrikeToMonster(Body player, Body monster, WorldManifold mani) {
    Player p = map.getPlayer();/*  ww w.  jav a 2s .  c o m*/
    if (p.isStriking) {

        Enemy e = map.getEnemy((Integer) monster.getUserData());
        e.disableControls(0.3f);

        int attack = p.getAttack();
        int stamina = p.getStamina();

        int life = e.getLife();
        int defense = e.getDefense();

        e.setLife(newLife(life, defense, attack, stamina));

        monster.setLinearVelocity(new Vector2(0f, 0f));
        Vector2 dir = new Vector2(monster.getPosition().x, monster.getPosition().y);
        dir.sub(player.getPosition());
        dir.x = dir.x * 7;
        dir.y = dir.y * 7;

        monster.setLinearVelocity(dir);

        e.playSoundDamage();
        if (e.getLife() <= 0) {
            map.killEnemy((Integer) monster.getUserData());
            e.killEnemy();
        }

    }

}

From source file:DungeonCleanerGame.CombatManager.java

public void computeStrikeToPlayer(Body monster, Body player, WorldManifold mani) {
    Player p = map.getPlayer();/*w  w w .j av  a2  s. com*/
    Enemy e = map.getEnemy((Integer) monster.getUserData());

    p.disableControls(0.3f);

    int attack = e.getAttack();
    int stamina = e.getStamina();

    int life = p.getLife();
    int defense = p.getDefense();

    p.setLife(newLife(life, defense, attack, stamina));

    player.setLinearVelocity(new Vector2(0f, 0f));

    p.playSoundDamage();
    Vector2 dir = new Vector2(player.getPosition().x, player.getPosition().y);
    dir.sub(monster.getPosition());
    dir.x = dir.x * 10;
    dir.y = dir.y * 10;

    player.setLinearVelocity(dir);
}

From source file:DungeonCleanerGame.CombatManager.java

private void computeBulletToPlayer(Body bullet, Body player, WorldManifold mani) {
    Player p = map.getPlayer();//w  w w.  j a va2  s .co  m

    p.disableControls(0.3f);

    int attack = 10;
    int stamina = 10;

    int life = p.getLife();
    int defense = p.getDefense();

    p.setLife(newLife(life, defense, attack, stamina));

    player.setLinearVelocity(new Vector2(0f, 0f));
    Vector2 dir = new Vector2(player.getPosition().x, player.getPosition().y);
    dir.sub(bullet.getPosition());
    dir.x = dir.x * 10;
    dir.y = dir.y * 10;

    p.playSoundDamage();
    player.setLinearVelocity(dir);
}

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 w w  w. ja  v a2s  . 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.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 ww .  j  a  v a  2 s  . c o 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.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);// ww w .  j  a  va 2 s .  c  om
        v.sub(start);
    } 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:es.eucm.ead.engine.utils.EngineUtils.java

License:Open Source License

/**
 * Adjusts the position and size of the given group to its children
 *///  ww  w . j av a  2  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:halive.shootinoutside.util.VectorUtils.java

public static Vector2 getDirectionUnitVector(Vector2 v1, Vector2 v2) {
    Vector2 direction = v2.sub(v1);
    return getUnitVector(direction);
}

From source file:halive.shootinoutside.util.VectorUtils.java

public static Vector2 getDirectionVector(Vector2 v1, Vector2 v2) {
    Vector2 direction = v2.sub(v1);
    return direction;
}