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

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

Introduction

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

Prototype

public void set(float x, float y, float radius) 

Source Link

Document

Sets a new location and radius for this circle.

Usage

From source file:domain.World.java

License:Open Source License

/**
 * Update items.//from   www. ja v a 2  s. com
 * 
 * @param delta
 *            is the time passing.
 */
public void itemsUpdate(float delta) {
    Rectangle heroRect = Utils.obtainRectangle();
    Circle itemCircle = Utils.obtainCircle();
    // Obtain rectangle of hero.
    heroRect.set(hero.position.x, hero.position.y, hero.getWidth(), hero.getHeight());
    for (Iterator<Item> it = items.iterator(); it.hasNext();) {
        Item item = it.next();
        if (!item.isDoneForRemove(delta)) {
            item.calculatePositionAndVelocity(delta);

            // Y axis bounds
            if (!bodyGroundCollision(item, delta)) {
                item.fall();
            }

            // World bounds
            // Floor bound
            if (item.position.y <= floor) {
                item.position.y = floor;
                item.stopMoveY();
                item.stopMoveX();
            }
            // X axis bounds
            if (item.position.x <= 0) {
                item.position.x = 0;
                item.bounceX();
            } else if (item.getRightX() >= WIDTH) {
                item.position.x = WIDTH - item.getWidth();
                item.bounceX();
            }
            // Collisions with Hero
            if (!hero.isDead()) {
                itemCircle.set(item.getMiddleX(), item.getMiddleY(), item.getRadius());
                if (Intersector.overlaps(itemCircle, heroRect)) {
                    item.applyEffect(hero);
                    it.remove();
                    removeBodyFromGounds(item);
                }
            }
        } else {
            it.remove();
            removeBodyFromGounds(item);
        }
    }

}

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