Example usage for com.badlogic.gdx.physics.box2d Shape getType

List of usage examples for com.badlogic.gdx.physics.box2d Shape getType

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Shape getType.

Prototype

public abstract Type getType();

Source Link

Document

Get the type of this shape.

Usage

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DUtils.java

License:Apache License

/** @return the vertices of the given Shape */
public static Vector2[] vertices(Shape shape, Vector2[] output) {
    if (shapeCache.containsKey(shape))
        return output = shapeCache.get(shape).vertices;
    else {/* w  ww .j a  va  2s . c o m*/
        switch (shape.getType()) {
        case Polygon:
            PolygonShape polygonShape = (PolygonShape) shape;

            output = new Vector2[polygonShape.getVertexCount()];

            for (int i = 0; i < output.length; i++) {
                output[i] = new Vector2();
                polygonShape.getVertex(i, output[i]);
            }
            break;
        case Edge:
            EdgeShape edgeShape = (EdgeShape) shape;

            edgeShape.getVertex1(tmpVec);
            edgeShape.getVertex2(tmpVec2);

            output = new Vector2[] { tmpVec, tmpVec2 };
            break;
        case Chain:
            ChainShape chainShape = (ChainShape) shape;

            output = new Vector2[chainShape.getVertexCount()];

            for (int i = 0; i < output.length; i++) {
                output[i] = new Vector2();
                chainShape.getVertex(i, output[i]);
            }
            break;
        case Circle:
            CircleShape circleShape = (CircleShape) shape;

            output = new Vector2[] {
                    new Vector2(circleShape.getPosition().x - circleShape.getRadius(),
                            circleShape.getPosition().y + circleShape.getRadius()), // top left
                    new Vector2(circleShape.getPosition().x - circleShape.getRadius(),
                            circleShape.getPosition().y - circleShape.getRadius()), // bottom left
                    new Vector2(circleShape.getPosition().x + circleShape.getRadius(),
                            circleShape.getPosition().y - circleShape.getRadius()), // bottom right
                    new Vector2(circleShape.getPosition().x + circleShape.getRadius(),
                            circleShape.getPosition().y + circleShape.getRadius()) // top right
            };
            break;
        default:
            throw new IllegalArgumentException(
                    "Shapes of the type '" + shape.getType().name() + "' are not supported");
        }

        if (autoShapeCache && shapeCache.size < autoShapeCacheMaxSize) {
            Vector2[] cachedOutput = new Vector2[output.length];
            System.arraycopy(output, 0, cachedOutput, 0, output.length);
            shapeCache.put(shape, new ShapeCache(cachedOutput, amplitude(filterX(cachedOutput)),
                    amplitude(filterY(cachedOutput)), min(filterX(cachedOutput)), max(filterX(cachedOutput)),
                    min(filterY(cachedOutput)), max(filterY(cachedOutput))));
        }

        return output;
    }
}

From source file:com.indignado.games.smariano.utils.dermetfan.box2d.Box2DUtils.java

License:Apache License

/** @see #size(com.badlogic.gdx.physics.box2d.Shape) */
public static Vector2 size(Shape shape, Vector2 output) {
    if (shape.getType() == Type.Circle) // no call to #vertices(Shape) for performance
        return output.set(shape.getRadius() * 2, shape.getRadius() * 2);
    else if (shapeCache.containsKey(shape))
        return output.set(shapeCache.get(shape).width, shapeCache.get(shape).height);
    return output.set(width(shape), height(shape));
}

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

License:Apache License

/** @param shape the Shape which vertices to get (for circles, the bounding box vertices will be returned)
 *  @return the vertices of the given Shape*/
private static Vector2[] vertices0(Shape shape) {
    Vector2[] vertices;/*from   www. j a va2  s. co m*/
    switch (shape.getType()) {
    case Polygon:
        PolygonShape polygonShape = (PolygonShape) shape;
        vertices = new Vector2[polygonShape.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            vertices[i] = new Vector2();
            polygonShape.getVertex(i, vertices[i]);
        }
        break;
    case Edge:
        EdgeShape edgeShape = (EdgeShape) shape;
        edgeShape.getVertex1(vec2_0);
        edgeShape.getVertex2(vec2_1);
        vertices = new Vector2[] { new Vector2(vec2_0), new Vector2(vec2_1) };
        break;
    case Chain:
        ChainShape chainShape = (ChainShape) shape;
        vertices = new Vector2[chainShape.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            vertices[i] = new Vector2();
            chainShape.getVertex(i, vertices[i]);
        }
        break;
    case Circle:
        CircleShape circleShape = (CircleShape) shape;
        Vector2 position = circleShape.getPosition();
        float radius = circleShape.getRadius();
        vertices = new Vector2[4];
        vertices[0] = new Vector2(position.x - radius, position.y + radius); // top left
        vertices[1] = new Vector2(position.x - radius, position.y - radius); // bottom left
        vertices[2] = new Vector2(position.x + radius, position.y - radius); // bottom right
        vertices[3] = new Vector2(position.x + radius, position.y + radius); // top right
        break;
    default:
        throw new IllegalArgumentException(
                "shapes of the type '" + shape.getType().name() + "' are not supported");
    }
    return vertices;
}

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

License:Apache License

/** @return the width of the given Shape */
private static float width0(Shape shape) {
    if (shape.getType() == Type.Circle)
        return shape.getRadius() * 2;
    return amplitude(filterX(vertices0(shape)));
}

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

License:Apache License

/** @return the height of the given Shape */
private static float height0(Shape shape) {
    if (shape.getType() == Type.Circle)
        return shape.getRadius() * 2;
    return amplitude(filterY(vertices0(shape)));
}

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

License:Apache License

/** {@link #vertices(Shape)} without caching */
private static Vector2[] vertices0(Shape shape, Vector2[] output) {
    switch (shape.getType()) {
    case Polygon:
        PolygonShape polygonShape = (PolygonShape) shape;

        if (output == null || output.length != polygonShape.getVertexCount())
            output = new Vector2[polygonShape.getVertexCount()];

        for (int i = 0; i < output.length; i++) {
            if (output[i] == null)
                output[i] = new Vector2();
            polygonShape.getVertex(i, output[i]);
        }/* w w w.j a  v  a 2s.c  o m*/
        break;
    case Edge:
        EdgeShape edgeShape = (EdgeShape) shape;

        edgeShape.getVertex1(vec2_0);
        edgeShape.getVertex2(vec2_1);

        if (output == null || output.length != 2)
            output = new Vector2[] { vec2_0, vec2_1 };
        else {
            if (output[0] == null)
                output[0] = new Vector2(vec2_0);
            else
                output[0].set(vec2_0);
            if (output[1] == null)
                output[1] = new Vector2(vec2_1);
            else
                output[1].set(vec2_1);
        }
        break;
    case Chain:
        ChainShape chainShape = (ChainShape) shape;

        if (output == null || output.length != chainShape.getVertexCount())
            output = new Vector2[chainShape.getVertexCount()];

        for (int i = 0; i < output.length; i++) {
            if (output[i] == null)
                output[i] = new Vector2();
            chainShape.getVertex(i, output[i]);
        }
        break;
    case Circle:
        CircleShape circleShape = (CircleShape) shape;

        if (output == null || output.length != 4)
            output = new Vector2[4];
        vec2_0.set(circleShape.getPosition().x - circleShape.getRadius(),
                circleShape.getPosition().y + circleShape.getRadius()); // top left
        output[0] = output[0] != null ? output[0].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x - circleShape.getRadius(),
                circleShape.getPosition().y - circleShape.getRadius()); // bottom left
        output[1] = output[1] != null ? output[1].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x + circleShape.getRadius(),
                circleShape.getPosition().y - circleShape.getRadius()); // bottom right
        output[2] = output[2] != null ? output[2].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x + circleShape.getRadius(),
                circleShape.getPosition().y + circleShape.getRadius()); // top right
        output[3] = output[3] != null ? output[3].set(vec2_0) : new Vector2(vec2_0);
        break;
    default:
        throw new IllegalArgumentException(
                "Shapes of the type '" + shape.getType().name() + "' are not supported");
    }
    return output;
}

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

License:Apache License

/** @see #size(Shape) */
public static Vector2 size(Shape shape, Vector2 output) {
    if (shape.getType() == Type.Circle) // no call to #vertices(Shape) for performance
        return output.set(shape.getRadius() * 2, shape.getRadius() * 2);
    else if (cache.containsKey(shape))
        return output.set(cache.get(shape).width, cache.get(shape).height);
    return output.set(width(shape), height(shape));
}

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

License:Apache License

/** creates a deep copy of a {@link Shape}<br/>
 *  <strong>Note: The {@link ChainShape#setPrevVertex(float, float) previous} and {@link ChainShape#setNextVertex(float, float) next} vertex of a {@link ChainShape} will not be copied since this is not possible due to the API.</strong>
 *  @param shape the {@link Shape} to copy
 *  @return a {@link Shape} exactly like the one passed in */
public static Shape copy(Shape shape) {
    Shape copy;//  ww w  .j  a v  a2 s. c o  m
    switch (shape.getType()) {
    case Circle:
        CircleShape circleCopy = (CircleShape) (copy = new CircleShape());
        circleCopy.setPosition(((CircleShape) shape).getPosition());
        break;
    case Polygon:
        PolygonShape polyCopy = (PolygonShape) (copy = new PolygonShape()), poly = (PolygonShape) shape;
        float[] vertices = new float[poly.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            poly.getVertex(i, vec2_0);
            vertices[i++] = vec2_0.x;
            vertices[i] = vec2_0.y;
        }
        polyCopy.set(vertices);
        break;
    case Edge:
        EdgeShape edgeCopy = (EdgeShape) (copy = new EdgeShape()), edge = (EdgeShape) shape;
        edge.getVertex1(vec2_0);
        edge.getVertex2(vec2_1);
        edgeCopy.set(vec2_0, vec2_1);
        break;
    case Chain:
        ChainShape chainCopy = (ChainShape) (copy = new ChainShape()), chain = (ChainShape) shape;
        vertices = new float[chain.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            chain.getVertex(i, vec2_0);
            vertices[i++] = vec2_0.x;
            vertices[i] = vec2_0.y;
        }
        if (chain.isLooped())
            chainCopy.createLoop(GeometryUtils.toVector2Array(vertices));
        else
            chainCopy.createChain(vertices);
        break;
    default:
        return shape;
    }
    copy.setRadius(shape.getRadius());
    return copy;
}